]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/SOGoDateFormatter.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1166 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SoObjects / SOGo / 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 #import <Foundation/NSCalendarDate.h>
23 #import <Foundation/NSUserDefaults.h>
24
25 #import "SOGoDateFormatter.h"
26
27 @implementation SOGoDateFormatter
28
29 - (id) init
30 {
31   if ((self = [super init]))
32     {
33       locale = nil;
34 //       locale = [_locale retain];
35       
36 //       if ([[locale objectForKey:@"NSLocaleCode"] isEqualToString: @"fr"])
37 //      shortDateFormat = SOGoDateDMYFormat;
38 //       else
39 //      shortDateFormat = SOGoDateISOFormat;
40       shortDateFormat = nil;
41       longDateFormat = nil;
42       timeFormat = nil;
43     }
44
45   return self;
46 }
47
48 - (void) dealloc
49 {
50   [longDateFormat release];
51   [shortDateFormat release];
52   [timeFormat release];
53   [locale release];
54   [super dealloc];
55 }
56
57 /* accessors */
58
59 - (void) setLocale: (NSDictionary *) newLocale
60 {
61   ASSIGN (locale, newLocale);
62   ASSIGN (shortDateFormat, [locale objectForKey: NSShortDateFormatString]);
63   ASSIGN (longDateFormat, [locale objectForKey: NSDateFormatString]);
64   ASSIGN (timeFormat, [locale objectForKey: NSTimeFormatString]);
65 }
66
67 - (void) setShortDateFormat: (NSString *) newFormat
68 {
69   ASSIGN (shortDateFormat, newFormat);
70 }
71
72 - (void) setLongDateFormat: (NSString *) newFormat
73 {
74   ASSIGN (longDateFormat, newFormat);
75 }
76
77 - (void) setTimeFormat: (NSString *) newFormat
78 {
79   ASSIGN (timeFormat, newFormat);
80 }
81
82 // - (void) setFullWeekdayNameAndDetails
83 // {
84 //   auxFormatAction = formatAction;
85 //   formatAction = @selector(fullWeekdayNameAndDetailsForDate:);
86 // }
87
88 /* operation */
89
90 - (NSString *) _date: (NSCalendarDate *) date
91           withFormat: (NSString *) format
92 {
93   NSString *formattedDate;
94
95   if (format && locale)
96     formattedDate
97       = [date descriptionWithCalendarFormat: format locale: locale];
98   else
99     formattedDate = nil;
100
101   return formattedDate;
102 }
103
104 - (NSString *) shortFormattedDate: (NSCalendarDate *) date
105 {
106   return [self _date: date withFormat: shortDateFormat];
107 }
108
109 - (NSString *) formattedDate: (NSCalendarDate *) date
110 {
111   return [self _date: date withFormat: longDateFormat];
112 }
113
114 - (NSString *) formattedTime: (NSCalendarDate *) date
115 {
116   return [self _date: date withFormat: timeFormat];
117 }
118
119 - (NSString *) formattedDateAndTime: (NSCalendarDate *) date
120 {
121   NSString *format;
122
123   format = [NSString stringWithFormat: @"%@ %@ %%Z",
124                      longDateFormat, timeFormat];
125
126   return [self _date: date withFormat: format];
127 }
128
129 - (NSString *) stringForObjectValue: (id) object
130 {
131   NSString *formattedString;
132
133   if ([object isKindOfClass: [NSCalendarDate class]])
134     formattedString = [self formattedDateAndTime: object];
135   else
136     formattedString = nil;
137
138   return formattedString;
139 }
140
141 // /* Helpers */
142
143 // - (NSString *)shortDayOfWeek:(int)_day {
144 //   return [[locale objectForKey:@"NSShortWeekDayNameArray"]
145 //         objectAtIndex:_day];
146 // }
147
148 // - (NSString *)fullDayOfWeek:(int)_day {
149 //   return [[locale objectForKey:@"NSWeekDayNameArray"]
150 //         objectAtIndex:_day];
151 // }
152
153 // - (NSString *)shortMonthOfYear:(int)_month {
154 //   return [[locale objectForKey:@"NSShortMonthNameArray"]
155 //         objectAtIndex:_month - 1];
156 // }
157
158 // - (NSString *)fullMonthOfYear:(int)_month {
159 //   return [[locale objectForKey:@"NSMonthNameArray"]
160 //         objectAtIndex:_month - 1];
161 // }
162
163
164 /* Private API */
165
166 // - (NSString *) fullWeekdayNameAndDetailsForDate: (NSCalendarDate *) _date
167 // {
168 //   NSMutableString *desc;
169
170 //   if (_date)
171 //     {  
172 //       desc = [NSMutableString stringWithCapacity:24];
173 //       [desc appendString:[self fullDayOfWeek:[_date dayOfWeek]]];
174 //       [desc appendString:@", "];
175 //       [desc appendString:[self performSelector:auxFormatAction
176 //                            withObject:_date]];
177 //       [desc appendString:@" "];
178 //       [desc appendFormat:@"%02d:%02d ", [_date hourOfDay], [_date minuteOfHour]];
179 //       [desc appendString:[[_date timeZone] abbreviation]];
180 //     }
181 //   else
182 //     desc = nil;
183
184 //   return desc;
185 // }
186
187 // - (NSString *) _separatorForFormat: (unsigned int) format
188 // {
189 //   NSString *separator;
190
191 //   switch (format & (3))
192 //     {
193 //     case SOGoDateDotFormat:
194 //       separator = @".";
195 //       break;
196 //     case SOGoDateDashFormat:
197 //       separator = @".";
198 //       break;
199 //     default:
200 //       separator = @"/";
201 //     }
202
203 //   return separator;
204 // }
205
206 // - (NSString *) _dateFormatForDate: (NSCalendarDate *) date
207 //                     withFormat: (unsigned int) format
208 //                   andSeparator: (NSString *) separator
209 // {
210 //   NSString *day, *month, *year;
211 //   NSString *formattedDate;
212
213 //   day = [NSString stringWithFormat: @"%.2d", [date dayOfMonth]];
214 //   month = [NSString stringWithFormat: @"%.2d", [date monthOfYear]];
215 //   if (format & SOGoDateTwoDigitsYearFormat)
216 //     year = [NSString stringWithFormat: @"%.2d", [date yearOfCommonEra] % 100];
217 //   else
218 //     year = [NSString stringWithFormat: @"%.4d", [date yearOfCommonEra]];
219
220 //   if (format & SOGoDateDMYFormat)
221 //     formattedDate = [NSString stringWithFormat: @"%@%@%@%@%@",
222 //                            day, separator, month, separator, year];
223 //   else if (format & SOGoDateMDYFormat)
224 //     formattedDate = [NSString stringWithFormat: @"%@%@%@%@%@",
225 //                            month, separator, day, separator, year];
226 //   else
227 //     formattedDate = [NSString stringWithFormat: @"%@%@%@%@%@",
228 //                            year, separator, month, separator, day];
229
230 //   return formattedDate;
231 // }
232
233 // - (NSString *) date: (NSCalendarDate *) date
234 //       withFormat: (unsigned int) format
235 // {
236 //   NSString *separator;
237
238 //   separator = [self _separatorForFormat: format];
239   
240 //   return [self _dateFormatForDate: date
241 //             withFormat: format
242 //             andSeparator: separator];
243 // }
244
245 // - (NSString *) date: (NSCalendarDate *) date
246 //        withNSFormat: (NSNumber *) format
247 // {
248 //   return [self date: date withFormat: [format unsignedIntValue]];
249 // }
250
251 @end /* SOGoDateFormatter */