File: HourFormatter.m

package info (click to toggle)
agenda.app 0.44-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 1,012 kB
  • sloc: objc: 7,820; makefile: 18
file content (61 lines) | stat: -rw-r--r-- 1,873 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* emacs buffer mode hint -*- objc -*- */

#import "HourFormatter.h"

@implementation HourFormatter
+ (NSString *)stringForObjectValue:(id)anObject
{
  int hours;
  int minutes;

  if (![anObject isKindOfClass:[NSNumber class]])
    return nil;
  hours = [anObject intValue] / 3600;
  minutes = [anObject intValue] / 60 - hours * 60;
  return [NSString stringWithFormat:@"%dh%02d", hours, minutes];
}

- (NSString *)stringForObjectValue:(id)anObject
{
  return [HourFormatter stringForObjectValue:anObject];
}

- (BOOL)getObjectValue:(id *)anObject forString:(NSString *)string errorDescription:(NSString **)error
{
  NSNumberFormatter *nf;
  NSNumber *hours;
  NSNumber *minutes;
  NSArray *components = [string componentsSeparatedByString:@"h"];

  if (!components || [components count] != 2) {
    if (error)
      *error = [[NSString alloc] initWithString:@"Bad time formatting : cannot find hours and minutes separated by h"];
    return NO;
  }
  nf = AUTORELEASE([[NSNumberFormatter alloc] init]);
  hours = [nf numberFromString:[components objectAtIndex:0]];
  minutes = [nf numberFromString:[components objectAtIndex:1]];
  if (!hours || !minutes) {
    if (error)
      *error = [[NSString alloc] initWithString:@"Bad time formatting"];
    return NO;
  }
  if ([hours intValue] < 0 || [hours intValue] > 23) {
    if (error)
      *error = [[NSString alloc] initWithString:@"Hours must be between 0 and 23"];
    return NO;
  }
  if ([minutes intValue] < 0 || [minutes intValue] > 59) {
    if (error)
      *error = [[NSString alloc] initWithString:@"Minutes must be between 0 and 59"];
    return NO;
  }
  *anObject = [[NSNumber alloc] initWithInt:[hours intValue] * 3600 + [minutes intValue] * 60.0];
  return YES;
}

- (NSAttributedString *)attributedStringForObjectValue:(id)anObject withDefaultAttributes:(NSDictionary *)attributes
{
  return nil;
}
@end