Re: How to convert an UTCDateTime to a NSDate
Re: How to convert an UTCDateTime to a NSDate
- Subject: Re: How to convert an UTCDateTime to a NSDate
- From: Rainer Brockerhoff <email@hidden>
- Date: Mon, 29 Apr 2002 21:38:55 -0300
>
Date: Mon, 29 Apr 2002 18:56:32 +0200
>
From: Lorenzo Puleo <email@hidden>
>
>
How to convert an UTCDateTime to a NSDate?
This is what I use:
NSDate* dateForJan1904() { // utility to return a singleton reference NSDate
static NSDate* Jan1904 = nil;
if (!Jan1904) {
Jan1904 = [[NSDate dateWithString:@"1904-01-01 00:00:00 +0000"] retain];
}
return Jan1904;
}
NSDate* convertUTCtoNSDate(UTCDateTime input) {
NSDate* result = nil;
union {
UTCDateTime local;
UInt64 shifted;
} time;
time.local = input;
if (time.shifted) {
result = [[[NSDate alloc] initWithTimeInterval:time.shifted/65536
sinceDate:dateForJan1904()] autorelease];
}
return result;
}
It returns an autoreleased NSDate, or nil if the input value was zero. It rounds fractional seconds down. I suppose you could gain a couple of cycles by doing some back&forth casting on the input, but I find this more legible...
Conversely, here's the NSDate to UTCDateTime routine:
UTCDateTime convertNSDatetoUTC(NSDate* date) {
union {
UTCDateTime local;
UInt64 shifted;
} result;
result.shifted = date?[date timeIntervalSinceDate: dateForJan1904()]*65536:0;
return result.local;
}
HTH,
--
Rainer Brockerhoff <email@hidden>
Belo Horizonte, Brazil
"I love deadlines. I love the whooshing noise they make as they go by" (Douglas Adams)
http://www.brockerhoff.net/ (updated Apr. 2002)
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.