]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/NSCalendarDate+SOGo.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1050 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SoObjects / SOGo / NSCalendarDate+SOGo.m
1 /* NSCalendarDate+SOGo.m - this file is part of SOGo
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
22 #import <Foundation/NSCalendarDate.h>
23 #import <Foundation/NSTimeZone.h>
24 #import <NGExtensions/NSCalendarDate+misc.h>
25
26 #import "NSCalendarDate+SOGo.h"
27
28 static NSString *rfc822Days[] = {@"Sun", @"Mon", @"Tue", @"Wed", @"Thu",
29                                  @"Fri", @"Sat"};
30 static NSString *rfc822Months[] = {@"", @"Jan", @"Feb", @"Mar", @"Apr",
31                                    @"May", @"Jun", @"Jul", @"Aug" , @"Sep",
32                                    @"Oct", @"Nov", @"Dec"};
33
34 @implementation NSCalendarDate (SOGoExtensions)
35
36 + (id) dateFromShortDateString: (NSString *) dateString
37             andShortTimeString: (NSString *) timeString
38                     inTimeZone: (NSTimeZone *) timeZone
39 {
40   unsigned int year, month, day, hour, minute, total;
41   NSCalendarDate *cDate, *tmpDate;
42
43   if (timeString && [timeString length] == 4)
44     {
45       total = [timeString intValue];
46       hour = total / 100;
47       minute = total - (hour * 100);
48     }
49   else
50     {
51       hour = 12;
52       minute = 0;
53     }
54
55   if (dateString && [dateString length] == 8)
56     {
57       total = [dateString intValue];
58       year = total / 10000;
59       total -= year * 10000;
60       month = total / 100;
61       day = total - (month * 100);
62       cDate = [self dateWithYear: year month: month day: day
63                     hour: hour minute: minute second: 0
64                     timeZone: timeZone];
65     }
66   else
67     {
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
74                     timeZone: timeZone];
75     }
76
77   return cDate;
78 }
79
80 - (BOOL) isDateInSameMonth: (NSCalendarDate *) _other
81 {
82   return (([_other yearOfCommonEra] == [self yearOfCommonEra]) &&
83           ([_other monthOfYear] == [self monthOfYear]));
84 }
85
86 - (NSCalendarDate *) dayOfWeeK: (unsigned) _day 
87               offsetFromSunday: (unsigned) _offset
88 {
89   unsigned dayOfWeek, distance;
90     
91   /* perform "locale" correction */
92   dayOfWeek = (7 + [self dayOfWeek] - _offset) % 7;
93
94   _day = (_day % 7);
95   if(_day == dayOfWeek)
96     return self;
97
98   distance = _day - dayOfWeek;
99   return [self dateByAddingYears:0 months:0 days:distance];
100 }
101
102 /* this implies that monday is the start of week! */
103
104 - (NSCalendarDate *) sundayOfWeek
105 {
106   return [self dayOfWeeK:6 offsetFromSunday:1];
107 }
108
109 - (NSString *) shortDateString
110 {
111   NSString *str;
112
113   str = [NSString stringWithFormat: @"%.4d%.2d%.2d",
114                   [self yearOfCommonEra],
115                   [self monthOfYear],
116                   [self dayOfMonth]];
117
118   return str;
119 }
120
121 - (NSString *) rfc822DateString
122 {
123   int timeZoneShift, tzSeconds;
124
125   tzSeconds = [[self timeZone] secondsFromGMT];
126   timeZoneShift = (tzSeconds / 3600);
127   tzSeconds -= timeZoneShift * 3600;
128   timeZoneShift *= 100;
129   timeZoneShift += tzSeconds / 60;
130
131   return
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],
136               timeZoneShift];
137 }
138
139 @end