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 #include "UIxMailFormatter.h"
25 static Class StrClass = Nil;
26 static Class CalDateClass = Nil;
28 @implementation UIxMailFormatter
30 static BOOL debugOn = YES;
33 StrClass = [NSString class];
34 CalDateClass = [NSCalendarDate class];
39 - (NSString *)labelForKey:(NSString *)_key {
40 // TODO: fetch labels from context
46 - (BOOL)isDebuggingEnabled {
50 @end /* UIxMailFormatter */
52 @implementation UIxMailDateFormatter
55 if ((self = [super init])) {
56 static NSTimeZone *met = nil;
57 if (met == nil) met = [[NSTimeZone timeZoneWithName:@"MET"] retain];
59 self->timeZone = [met retain];
60 self->dfFlags.showOnlyTimeForToday = 1;
61 self->dfFlags.showLabelsForNearDays = 1;
67 [self->timeZone release];
74 - (NSTimeZone *)timeZone {
75 return self->timeZone;
78 - (BOOL)showOnlyTimeForToday {
79 return self->dfFlags.showOnlyTimeForToday ? YES : NO;
81 - (BOOL)showLabelsForNearDays {
82 return self->dfFlags.showLabelsForNearDays ? YES : NO;
85 /* formatting dates */
87 - (NSString *)stringForTime:(NSCalendarDate *)_d prefix:(unsigned char *)_p {
88 /* Note: prefix is not allowed to be long! */
89 unsigned char buf[32];
91 if (_p == NULL) _p = "";
92 sprintf(buf, "%s%02i:%02i", _p, [_d hourOfDay], [_d minuteOfHour]);
93 return [StrClass stringWithCString:buf];
96 - (NSString *)stringForCalendarDate:(NSCalendarDate *)_date {
97 unsigned char buf[32];
99 if (self->now == nil) {
100 self->now = [[NSCalendarDate alloc] init];
101 [self->now setTimeZone:[self timeZone]];
103 [_date setTimeZone:[self timeZone]];
105 if ([self showOnlyTimeForToday] && [_date isDateOnSameDay:self->now])
106 return [self stringForTime:_date prefix:NULL];
108 if ([self showLabelsForNearDays]) {
111 if ([_date isDateOnSameDay:self->now])
112 label = [self labelForKey:@"today"];
113 else if ([_date isDateOnSameDay:[self->now yesterday]])
114 label = [self labelForKey:@"yesterday"];
119 return [label stringByAppendingString:
120 [self stringForTime:_date prefix:", "]];
124 /* 26.08.2004 13:24 */
125 sprintf(buf, "%02d.%02d.%04d %02d:%02d",
126 [_date dayOfMonth], [_date monthOfYear], [_date yearOfCommonEra],
127 [_date hourOfDay], [_date minuteOfHour]);
128 return [StrClass stringWithCString:buf];
131 /* formatter entry function */
133 - (NSString *)stringForObjectValue:(id)_date {
134 if (![_date isNotNull])
137 if ([_date isKindOfClass:StrClass]) /* already formatted */
140 if ([_date isKindOfClass:CalDateClass])
141 return [self stringForCalendarDate:_date];
143 [self debugWithFormat:
144 @"NOTE: unexpected object for date formatter: %@<%@>",
145 _date, NSStringFromClass([_date class])];
146 return [_date description];
149 @end /* UIxMailDateFormatter */