1 /* NSCalendarDate+SOGo.m - this file is part of SOGo
2 Copyright (C) 2000-2004 SKYRIX Software AG
4 This file is part of OGo
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 <Foundation/NSTimeZone.h>
24 #import <NGExtensions/NSCalendarDate+misc.h>
26 #import "NSCalendarDate+SOGo.h"
28 static NSString *rfc822Days[] = {@"Sun", @"Mon", @"Tue", @"Wed", @"Thu",
30 static NSString *rfc822Months[] = {@"", @"Jan", @"Feb", @"Mar", @"Apr",
31 @"May", @"Jun", @"Jul", @"Aug" , @"Sep",
32 @"Oct", @"Nov", @"Dec"};
34 @implementation NSCalendarDate (SOGoExtensions)
36 + (id) dateFromShortDateString: (NSString *) dateString
37 andShortTimeString: (NSString *) timeString
38 inTimeZone: (NSTimeZone *) timeZone
40 unsigned int year, month, day, hour, minute, total;
41 NSCalendarDate *cDate, *tmpDate;
43 if (timeString && [timeString length] == 4)
45 total = [timeString intValue];
47 minute = total - (hour * 100);
55 if (dateString && [dateString length] == 8)
57 total = [dateString intValue];
59 total -= year * 10000;
61 day = total - (month * 100);
62 cDate = [self dateWithYear: year month: month day: day
63 hour: hour minute: minute second: 0
68 tmpDate = [NSCalendarDate calendarDate];
69 [tmpDate setTimeZone: timeZone];
70 cDate = [self dateWithYear: [tmpDate yearOfCommonEra]
71 month: [tmpDate monthOfYear]
72 day: [tmpDate dayOfMonth]
73 hour: hour minute: minute second: 0
80 - (BOOL) isDateInSameMonth: (NSCalendarDate *) _other
82 return (([_other yearOfCommonEra] == [self yearOfCommonEra]) &&
83 ([_other monthOfYear] == [self monthOfYear]));
86 - (NSCalendarDate *) dayOfWeeK: (unsigned) _day
87 offsetFromSunday: (unsigned) _offset
89 unsigned dayOfWeek, distance;
91 /* perform "locale" correction */
92 dayOfWeek = (7 + [self dayOfWeek] - _offset) % 7;
98 distance = _day - dayOfWeek;
99 return [self dateByAddingYears:0 months:0 days:distance];
102 /* this implies that monday is the start of week! */
104 - (NSCalendarDate *) sundayOfWeek
106 return [self dayOfWeeK:6 offsetFromSunday:1];
109 - (NSString *) shortDateString
113 str = [NSString stringWithFormat: @"%.4d%.2d%.2d",
114 [self yearOfCommonEra],
121 - (NSString *) rfc822DateString
123 int timeZoneShift, tzSeconds;
125 tzSeconds = [[self timeZone] secondsFromGMT];
126 timeZoneShift = (tzSeconds / 3600);
127 tzSeconds -= timeZoneShift * 3600;
128 timeZoneShift *= 100;
129 timeZoneShift += tzSeconds / 60;
132 [NSString stringWithFormat: @"%@, %.2d %@ %d %.2d:%.2d:%.2d %+.4d",
133 rfc822Days[[self dayOfWeek]], [self dayOfMonth],
134 rfc822Months[[self monthOfYear]], [self yearOfCommonEra],
135 [self hourOfDay], [self minuteOfHour], [self secondOfMinute],
139 #define secondsOfDistantFuture 63113990400.0
140 #define secondsOfDistantPast -63113817600.0
144 static NSCalendarDate *date = nil;
148 = [[self alloc] initWithTimeIntervalSinceReferenceDate: secondsOfDistantFuture];
155 static NSCalendarDate *date = nil;
159 = [[self alloc] initWithTimeIntervalSinceReferenceDate: secondsOfDistantPast];