]> err.no Git - scalable-opengroupware.org/blob - SOGo/UI/SOGoUI/SOGoDateFormatter.m
cf95545501b4ad758476ba05b27bdda264b1eb48
[scalable-opengroupware.org] / SOGo / UI / SOGoUI / SOGoDateFormatter.m
1 /*
2   Copyright (C) 2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include "SOGoDateFormatter.h"
23 #include "common.h"
24
25 @interface SOGoDateFormatter (PrivateAPI)
26 - (NSString *)shortDayOfWeek:(int)_day;
27 - (NSString *)fullDayOfWeek:(int)_day;
28 - (NSString *)shortMonthOfYear:(int)_month;
29 - (NSString *)fullMonthOfYear:(int)_month;
30
31 - (NSString *)isoDateFormatForDate:(NSCalendarDate *)_date;
32 - (NSString *)fullWeekdayNameAndDetailsForDate:(NSCalendarDate *)_date;
33 @end
34
35 @implementation SOGoDateFormatter
36
37 - (id)initWithLocale:(NSDictionary *)_locale {
38   if ((self = [super init])) {
39     self->locale = [_locale retain];
40     [self setISODateFormat];
41   }
42   return self;
43 }
44
45 - (void)dealloc {
46   [self->locale release];
47   [super dealloc];
48 }
49
50 /* accessors */
51
52 - (void)setISODateFormat {
53   self->formatAction = @selector(isoDateFormatForDate:);
54 }
55
56 - (void)setFullWeekdayNameAndDetails {
57   self->formatAction = @selector(fullWeekdayNameAndDetailsForDate:);
58 }
59
60 /* operation */
61
62 - (NSString *)stringForObjectValue:(id)_obj {
63   return [self performSelector:self->formatAction
64                withObject:_obj];
65 }
66
67 /* Helpers */
68
69 - (NSString *)shortDayOfWeek:(int)_day {
70   return [[self->locale objectForKey:@"NSShortWeekDayNameArray"]
71            objectAtIndex:_day];
72 }
73
74 - (NSString *)fullDayOfWeek:(int)_day {
75   return [[self->locale objectForKey:@"NSWeekDayNameArray"]
76            objectAtIndex:_day];
77 }
78
79 - (NSString *)shortMonthOfYear:(int)_month {
80   return [[self->locale objectForKey:@"NSShortMonthNameArray"]
81            objectAtIndex:_month - 1];
82 }
83
84 - (NSString *)fullMonthOfYear:(int)_month {
85   return [[self->locale objectForKey:@"NSMonthNameArray"]
86            objectAtIndex:_month - 1];
87 }
88
89
90 /* Private API */
91
92 - (NSString *)isoDateFormatForDate:(NSCalendarDate *)_date {
93   return [NSString stringWithFormat:@"%04d-%02d-%02d",
94                    [_date yearOfCommonEra],
95                    [_date monthOfYear],
96                    [_date dayOfMonth]];
97 }
98
99 - (NSString *)fullWeekdayNameAndDetailsForDate:(NSCalendarDate *)_date {
100   NSMutableString *desc;
101   
102   desc = [NSMutableString stringWithCapacity:24];
103   [desc appendString:[self fullDayOfWeek:[_date dayOfWeek]]];
104   [desc appendString:@", "];
105   [desc appendString:[self isoDateFormatForDate:_date]];
106   [desc appendString:@" "];
107   [desc appendFormat:@"%02d:%02d ", [_date hourOfDay], [_date minuteOfHour]];
108   [desc appendString:[[_date timeZone] abbreviation]];
109   return desc;
110 }
111
112 @end /* SOGoDateFormatter */