1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
#import "Date.h"
@interface NSDateEnumerator : NSEnumerator
{
Date *_start;
Date *_end;
}
- (id)initWithStart:(Date *)start end:(Date *)end;
@end
@implementation NSDateEnumerator
- (id)initWithStart:(Date *)start end:(Date *)end
{
self = [super init];
if (self != nil) {
_start = [start copy];
_end = [end copy];
[_start setIsDate:YES];
[_end setIsDate:YES];
[_start changeDayBy:-1];
}
return self;
}
- (id)nextObject
{
[_start incrementDay];
if ([_end timeIntervalSinceDate:_start] < 0)
return nil;
return _start;
}
- (void)dealloc
{
[_start release];
[_end release];
[super dealloc];
}
@end
@implementation Date(NSCoding)
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:[NSString stringWithCString:icaltime_as_ical_string(_time)] forKey:@"icalTime"];
}
- (id)initWithCoder:(NSCoder *)coder
{
_time = icaltime_from_string([[coder decodeObjectForKey:@"icalTime"] cString]);
return self;
}
@end
/*
* Based on ChronographerSource Appointment class
*/
@implementation Date
static icaltimezone *gl_tz = NULL;
static icaltimezone *gl_utc = NULL;
static NSTimeZone *gl_nstz = nil;
+ (void)initialize
{
const char *tzone;
if (!gl_tz) {
gl_nstz = RETAIN([[NSCalendarDate calendarDate] timeZone]);
tzone = [[gl_nstz description] cString];
gl_utc = icaltimezone_get_utc_timezone();
gl_tz = icaltimezone_get_builtin_timezone(tzone);
if (!gl_tz)
NSLog(@"Couldn't get a timezone corresponding to %s", tzone);
}
}
- (id)copyWithZone:(NSZone *)zone
{
Date *new = [Date allocWithZone:zone];
new->_time = _time;
return new;
}
/*
* This method shouldn't be used as it's not
* clear if it should compare Dates or DateTimes
*/
- (NSComparisonResult)compare:(id)aDate
{
[self notImplemented:_cmd];
return NSOrderedSame;
}
- (BOOL)isEqual:(id)aDate
{
return icaltime_as_timet(_time) == icaltime_as_timet(((Date *)aDate)->_time);
}
- (NSUInteger)hash
{
return icaltime_as_timet(_time);
}
/*
* This is a bit convoluted because icaltime_compare
* creates two dates in utc timezone to do the comparison.
* The conversion to utc means a datetime will be modified
* (up or down, depending on the local time zone) and a
* date won't.
*/
- (NSComparisonResult)compare:(id)aDate withTime:(BOOL)time
{
if (!time)
return icaltime_compare_date_only(_time, ((Date *)aDate)->_time);
NSComparisonResult res;
int a_isdate = _time.is_date;
int b_isdate = ((Date *)aDate)->_time.is_date;
_time.is_date = 0;
((Date *)aDate)->_time.is_date = 0;
res = icaltime_compare(_time, ((Date *)aDate)->_time);
_time.is_date = a_isdate;
((Date *)aDate)->_time.is_date = b_isdate;
return res;
}
- (NSString *)description
{
return [NSString stringWithCString:icaltime_as_ical_string(_time)];
}
- (id)init
{
self = [super init];
if (self) {
_time = icaltime_current_time_with_zone(gl_tz);
_time = icaltime_set_timezone(&_time, gl_tz);
}
return self;
}
+ (id)now
{
return AUTORELEASE([[Date alloc] init]);
}
+ (id)today
{
Date *d = [[Date alloc] init];
[d setIsDate:YES];
return AUTORELEASE(d);
}
+ (id)dateWithTimeInterval:(NSTimeInterval)seconds sinceDate:(Date *)refDate
{
Date *d = [[Date alloc] init];
d->_time = refDate->_time;
/* To be able to add hours and minutes, it has to be a datetime */
d-> _time.is_date = 0;
d->_time = icaltime_add(d->_time, icaldurationtype_from_int(seconds));
if (!d->_time.hour && !d->_time.minute && !d->_time.second)
d->_time.is_date = 1;
else
d->_time.is_date = 0;
return AUTORELEASE(d);
}
+ (id)dateWithCalendarDate:(NSCalendarDate *)cd withTime:(BOOL)time
{
Date *d = [[Date alloc] init];
d->_time.is_date = !time;
d->_time.year = [cd yearOfCommonEra];
d->_time.month = [cd monthOfYear];
d->_time.day = [cd dayOfMonth];
if (time) {
d->_time.hour = [cd hourOfDay];
d->_time.minute = [cd minuteOfHour];
d->_time.second = [cd secondOfMinute];
} else {
d->_time.hour = 0;
d->_time.minute = 0;
d->_time.second = 0;
}
return AUTORELEASE(d);
}
+ (id)dayWithDate:(Date *)date
{
Date *d = [date copy];
[d setIsDate:YES];
return AUTORELEASE(d);
}
- (NSCalendarDate *)calendarDate
{
return [NSCalendarDate dateWithYear:_time.year
month:_time.month
day:_time.day
hour:_time.hour
minute:_time.minute
second:_time.second
timeZone:gl_nstz];
}
- (int)year
{
return _time.year;
}
- (int)monthOfYear
{
return _time.month;
}
- (int)hourOfDay
{
return _time.hour;
}
- (int)secondOfMinute
{
return _time.second;
}
- (int)minuteOfHour
{
return _time.minute;
}
- (int)minuteOfDay
{
return _time.hour * 60 + _time.minute;
}
- (int)dayOfMonth
{
return _time.day;
}
/*
* 1 : Monday
* ...
* 7 : Sunday
*/
- (int)weekday
{
int dow = icaltime_day_of_week(_time) - 1;
return dow ? dow : 7;
}
- (int)weekOfYear
{
char week[3];
time_t tmt = icaltime_as_timet(_time);
struct tm *tm = gmtime(&tmt);
strftime(week, sizeof(week), "%V", tm);
return atoi(week);
}
- (int)numberOfDaysInMonth
{
return icaltime_days_in_month(_time.month, _time.year);
}
- (void)setSecondOfMinute:(int)second
{
NSAssert(!_time.is_date, @"Works only with datetimes");
_time.second = second;
_time = icaltime_normalize(_time);
}
- (void)setMinute:(int)minute
{
NSAssert(!_time.is_date, @"Works only with datetimes");
struct icaldurationtype dt = {0, 0, 0, 0, minute - [self minuteOfDay], 0};
_time = icaltime_add(_time, dt);
}
- (void)setDay:(int)day
{
_time.day = day;
_time = icaltime_normalize(_time);
}
- (void)setMonth:(int)month
{
_time.month = month;
_time = icaltime_normalize(_time);
}
- (void)setYear:(int)year
{
_time.year = year;
}
- (void)incrementDay
{
struct icaldurationtype dt = {0, 1, 0, 0, 0, 0};
_time = icaltime_add(_time, dt);
}
- (void)changeYearBy:(int)diff
{
_time.year += diff;
}
- (void)changeDayBy:(int)diff
{
struct icaldurationtype dt = {diff < 0, ABS(diff), 0, 0, 0, 0};
_time = icaltime_add(_time, dt);
}
- (void)changeMinuteBy:(int)diff
{
struct icaldurationtype dt = {diff < 0, 0, 0, 0, ABS(diff), 0};
_time = icaltime_add(_time, dt);
}
- (NSEnumerator *)enumeratorTo:(Date *)end
{
id e;
e = [NSDateEnumerator allocWithZone:NSDefaultMallocZone()];
e = [e initWithStart:self end:end];
return AUTORELEASE(e);
}
- (BOOL)isDate
{
return _time.is_date;
}
- (void)setIsDate:(BOOL)date
{
_time.is_date = date;
if (date) {
_time.hour = 0;
_time.minute = 0;
_time.second = 0;
}
}
- (NSTimeInterval)timeIntervalSince1970
{
return icaltime_as_timet(_time);
}
- (NSTimeInterval)timeIntervalSinceDate:(Date *)anotherDate
{
return icaldurationtype_as_int(icaltime_subtract(_time, anotherDate->_time));
}
- (NSTimeInterval)timeIntervalSinceNow
{
return [self timeIntervalSinceDate:[Date now]];
}
- (BOOL)belongsToDay:(Date *)day
{
NSAssert([day isDate], @"This method expects a date, not a datetime");
return _time.year == day->_time.year && _time.month == day->_time.month && _time.day == day->_time.day;
}
@end
/*
* Dates are stored in the local timezone in memory
* and in utc in iCalTree. These methods do the
* necessary conversions
*/
@implementation Date(iCalendar)
- (id)initWithICalTime:(struct icaltimetype)time
{
self = [super init];
if (self)
_time = icaltime_convert_to_zone(time, gl_tz);
return self;
}
- (struct icaltimetype)UTCICalTime
{
return icaltime_convert_to_zone(_time, gl_utc);
}
- (struct icaltimetype)localICalTime
{
return _time;
}
@end
|