]> err.no Git - scalable-opengroupware.org/blob - SOGo/UI/SOGoUI/SOGoDateFormatter.m
Added "AnaisUI" bundle, new formatter for SOGoUI, various enhancements
[scalable-opengroupware.org] / SOGo / UI / SOGoUI / SOGoDateFormatter.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OGo
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 // $Id$
22
23
24 #import "SOGoDateFormatter.h"
25 #import <NGExtensions/NGExtensions.h>
26
27
28 @interface SOGoDateFormatter (PrivateAPI)
29 - (NSString *)shortDayOfWeek:(int)_day;
30 - (NSString *)fullDayOfWeek:(int)_day;
31 - (NSString *)shortMonthOfYear:(int)_month;
32 - (NSString *)fullMonthOfYear:(int)_month;
33
34 - (NSString *)isoDateFormatForDate:(NSCalendarDate *)_date;
35 - (NSString *)fullWeekdayNameAndDetailsForDate:(NSCalendarDate *)_date;
36 @end
37
38
39 @implementation SOGoDateFormatter
40
41 - (id)initWithLocale:(NSDictionary *)_locale {
42     self = [super init];
43     if(self) {
44         ASSIGN(self->locale, _locale);
45         [self setISODateFormat];
46     }
47     return self;
48 }
49
50 - (void)dealloc {
51     [self->locale release];
52     [super dealloc];
53 }
54
55 - (void)setISODateFormat {
56     self->formatAction = @selector(isoDateFormatForDate:);
57 }
58
59 - (void)setFullWeekdayNameAndDetails {
60     self->formatAction = @selector(fullWeekdayNameAndDetailsForDate:);
61 }
62
63 - (NSString *)stringForObjectValue:(id)_obj {
64     return [self performSelector:self->formatAction
65                       withObject:_obj];
66 }
67
68
69 /* Helpers */
70
71 - (NSString *)shortDayOfWeek:(int)_day {
72     return [[self->locale objectForKey:@"NSShortWeekDayNameArray"]
73                           objectAtIndex:_day];
74 }
75
76 - (NSString *)fullDayOfWeek:(int)_day {
77     return [[self->locale objectForKey:@"NSWeekDayNameArray"]
78                           objectAtIndex:_day];
79 }
80
81 - (NSString *)shortMonthOfYear:(int)_month {
82     return [[self->locale objectForKey:@"NSShortMonthNameArray"]
83                           objectAtIndex:_month - 1];
84 }
85
86 - (NSString *)fullMonthOfYear:(int)_month {
87     return [[self->locale objectForKey:@"NSMonthNameArray"]
88                           objectAtIndex:_month - 1];
89 }
90
91
92 /* Private API */
93
94 - (NSString *)isoDateFormatForDate:(NSCalendarDate *)_date {
95     return [NSString stringWithFormat:@"%d-%02d-%02d",
96         [_date yearOfCommonEra],
97         [_date monthOfYear],
98         [_date dayOfMonth]];
99 }
100
101 - (NSString *)fullWeekdayNameAndDetailsForDate:(NSCalendarDate *)_date {
102     NSMutableString *desc;
103     
104     desc = [[NSMutableString alloc] init];
105     [desc appendString:[self fullDayOfWeek:[_date dayOfWeek]]];
106     [desc appendString:@", "];
107     [desc appendString:[self isoDateFormatForDate:_date]];
108     [desc appendString:@" "];
109     [desc appendFormat:@"%02d:%02d ",
110         [_date hourOfDay],
111         [_date minuteOfHour]];
112     [desc appendString:[[_date timeZone] name]];
113     return [desc autorelease];
114 }
115
116 @end