]> err.no Git - scalable-opengroupware.org/blob - UI/SOGoUI/SOGoDateFormatter.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1013 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / 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 @implementation SOGoDateFormatter
26
27 - (id)initWithLocale:(NSDictionary *)_locale {
28   if ((self = [super init])) {
29     self->locale = [_locale retain];
30     
31     if ([[self->locale objectForKey:@"NSLocaleCode"] isEqualToString:@"fr"])
32       [self setFrenchDateFormat];
33     else
34       [self setISODateFormat];
35   }
36   return self;
37 }
38
39 - (void)dealloc {
40   [self->locale release];
41   [super dealloc];
42 }
43
44 /* accessors */
45
46 - (void)setISODateFormat {
47   self->formatAction = @selector(isoDateFormatForDate:);
48 }
49
50 - (void)setFrenchDateFormat {
51   self->formatAction = @selector(frenchDateFormatForDate:);
52 }
53
54 - (void)setFullWeekdayNameAndDetails {
55   self->auxFormatAction = self->formatAction;
56   self->formatAction    = @selector(fullWeekdayNameAndDetailsForDate:);
57 }
58
59 /* operation */
60
61 - (NSString *)stringForObjectValue:(id)_obj {
62   return [self performSelector:self->formatAction
63                withObject:_obj];
64 }
65
66 /* Helpers */
67
68 - (NSString *)shortDayOfWeek:(int)_day {
69   return [[self->locale objectForKey:@"NSShortWeekDayNameArray"]
70            objectAtIndex:_day];
71 }
72
73 - (NSString *)fullDayOfWeek:(int)_day {
74   return [[self->locale objectForKey:@"NSWeekDayNameArray"]
75            objectAtIndex:_day];
76 }
77
78 - (NSString *)shortMonthOfYear:(int)_month {
79   return [[self->locale objectForKey:@"NSShortMonthNameArray"]
80            objectAtIndex:_month - 1];
81 }
82
83 - (NSString *)fullMonthOfYear:(int)_month {
84   return [[self->locale objectForKey:@"NSMonthNameArray"]
85            objectAtIndex:_month - 1];
86 }
87
88
89 /* Private API */
90
91 - (NSString *)isoDateFormatForDate:(NSCalendarDate *)_date {
92   char buf[16];
93   
94   if (_date == nil) return nil;
95   snprintf(buf, sizeof(buf), 
96            "%04d-%02d-%02d",
97            [_date yearOfCommonEra], [_date monthOfYear], [_date dayOfMonth]);
98   return [NSString stringWithCString:buf];
99 }
100
101 - (NSString *)frenchDateFormatForDate:(NSCalendarDate *)_date {
102   char buf[16];
103   
104   if (_date == nil) return nil;
105   snprintf(buf, sizeof(buf), 
106            "%02d/%02d/%04d",
107            [_date dayOfMonth], [_date monthOfYear], [_date yearOfCommonEra]);
108   return [NSString stringWithCString:buf];
109 }
110
111 - (NSString *)fullWeekdayNameAndDetailsForDate:(NSCalendarDate *)_date {
112   NSMutableString *desc;
113   
114   if (_date == nil) return nil;
115   
116   desc = [NSMutableString stringWithCapacity:24];
117   [desc appendString:[self fullDayOfWeek:[_date dayOfWeek]]];
118   [desc appendString:@", "];
119   [desc appendString:[self performSelector:self->auxFormatAction
120                            withObject:_date]];
121   [desc appendString:@" "];
122   [desc appendFormat:@"%02d:%02d ", [_date hourOfDay], [_date minuteOfHour]];
123   [desc appendString:[[_date timeZone] abbreviation]];
124   return desc;
125 }
126
127 @end /* SOGoDateFormatter */