2 Copyright (C) 2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
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
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.
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
22 #import <Foundation/NSCalendarDate.h>
23 #import <NGExtensions/NSCalendarDate+misc.h>
24 #import <NGExtensions/NSNull+misc.h>
25 #import <NGExtensions/NSObject+Logs.h>
26 #import <Foundation/NSTimeZone.h>
28 #import "UIxMailFormatter.h"
30 static Class StrClass = Nil;
31 static Class CalDateClass = Nil;
33 @implementation UIxMailFormatter
35 static BOOL debugOn = YES;
38 StrClass = [NSString class];
39 CalDateClass = [NSCalendarDate class];
44 - (NSString *)labelForKey:(NSString *)_key {
45 // TODO: fetch labels from context
51 - (BOOL)isDebuggingEnabled {
55 @end /* UIxMailFormatter */
57 @implementation UIxMailDateFormatter
60 if ((self = [super init])) {
61 static NSTimeZone *met = nil;
62 if (met == nil) met = [[NSTimeZone timeZoneWithName:@"MET"] retain];
64 self->timeZone = [met retain];
65 self->dfFlags.showOnlyTimeForToday = 1;
66 self->dfFlags.showLabelsForNearDays = 1;
72 [self->timeZone release];
79 - (NSTimeZone *)timeZone
81 return self->timeZone;
84 - (void) setTimeZone: (NSTimeZone *) newTimeZone
89 timeZone = newTimeZone;
95 - (BOOL)showOnlyTimeForToday {
96 return self->dfFlags.showOnlyTimeForToday ? YES : NO;
98 - (BOOL)showLabelsForNearDays {
99 return self->dfFlags.showLabelsForNearDays ? YES : NO;
102 /* formatting dates */
104 - (NSString *)stringForTime:(NSCalendarDate *)_d prefix:(char *)_p {
105 /* Note: prefix is not allowed to be long! */
108 if (_p == NULL) _p = "";
109 sprintf(buf, "%s%02i:%02i", _p, [_d hourOfDay], [_d minuteOfHour]);
110 return [StrClass stringWithCString:buf];
113 - (NSString *)stringForCalendarDate:(NSCalendarDate *)_date {
116 if (self->now == nil) {
117 self->now = [[NSCalendarDate alloc] init];
118 [self->now setTimeZone:[self timeZone]];
120 [_date setTimeZone:[self timeZone]];
122 if ([self showOnlyTimeForToday] && [_date isDateOnSameDay:self->now])
123 return [self stringForTime:_date prefix:NULL];
125 if ([self showLabelsForNearDays]) {
128 if ([_date isDateOnSameDay:self->now])
129 label = [self labelForKey:@"today"];
130 else if ([_date isDateOnSameDay:[self->now yesterday]])
131 label = [self labelForKey:@"yesterday"];
136 return [label stringByAppendingString:
137 [self stringForTime:_date prefix:", "]];
141 /* 26.08.2004 13:24 */
142 sprintf(buf, "%02d.%02d.%04d %02d:%02d",
143 [_date dayOfMonth], [_date monthOfYear], [_date yearOfCommonEra],
144 [_date hourOfDay], [_date minuteOfHour]);
145 return [StrClass stringWithCString:buf];
148 /* formatter entry function */
150 - (NSString *)stringForObjectValue:(id)_date {
151 if (![_date isNotNull])
154 if ([_date isKindOfClass:StrClass]) /* already formatted */
157 if ([_date isKindOfClass:CalDateClass])
158 return [self stringForCalendarDate:_date];
160 [self debugWithFormat:
161 @"NOTE: unexpected object for date formatter: %@<%@>",
162 _date, NSStringFromClass([_date class])];
163 return [_date description];
166 @end /* UIxMailDateFormatter */