Re: UTCDateTime????
Re: UTCDateTime????
- Subject: Re: UTCDateTime????
- From: Brendan Younger <email@hidden>
- Date: Wed, 1 Aug 2001 13:11:54 -0500
On Wednesday, August 1, 2001, at 09:39 AM, Brian Hill wrote:
On Wednesday, August 1, 2001, at 08:49 AM, Rainer Brockerhoff wrote:
Date: Wed, 1 Aug 2001 03:03:22 -0700
From: Steve Gehrman <email@hidden>
I'm calling carbon to get the creation date of a file. Unfortunately
the creationDate value is a UTCDateTime.
How do I convert this to an NSDate?
Somewhat inelegant, but works:
UTCDateTime timeStamp; // this is the input
NSCalendarDate* date; // this is the output
union {
LocalDateTime local;
UInt64 shifted;
} u;
ConvertUTCToLocalDateTime(timeStamp,&u.local);
date = [NSDate
dateWithTimeIntervalSinceReferenceDate:
u.shifted/65536.0-3061141200.0];
You may want to check the OSErr returned by ConvertUTCToLocalDateTime
and assign some convenient "invalid" value to the date, or assign some
default value to u.shifted. AFAIK ConvertUTCToLocalDateTime returns an
error if the UTCDateTime is zero.
Here's a handy category I wrote some time ago:
@implementation NSCalendarDate(CarbonExtras)
+ (NSCalendarDate*)dateWithUTCDateTime:(UTCDateTime)inTime
{
NSCalendarDate* date;
UInt32 localTime;
DateTimeRec dateTime;
ConvertUTCToLocalTime(inTime.lowSeconds,&localTime);
SecondsToDate(localTime,&dateTime);
date = [NSCalendarDate dateWithYear:dateTime.year
month:dateTime.month
day:dateTime.day
hour:dateTime.hour
minute:dateTime.minute
second:dateTime.second
timeZone:[NSTimeZone localTimeZone]];
return date;
}
@end
It should work for the next 37 years or so (when you'd start having to
use the 'Long' versions of the Carbon date time functions).
Brian
email@hidden http://personalpages.tds.net/~brian_hill
___________________________________________________________
"Why? I came into this game for adventure - go anywhere, travel
light, get in, get out, wherever there's trouble, a man alone.
Now they've got the whole country sectioned off and you can't
move without a form. I'm the last of a breed."
-- Archibald "Harry" Tuttle, Rogue HVAC Repairman
___________________________________________________________
_______________________________________________
Sorry to beat this to death, but I just had to deal with this yesterday
and my particular solution is:
UTCDateTime UTCdate;
NSDate* date, *scratch;
scratch = [NSDate dateWithString:@"1904-01-01 00:00:00 +0000"];
date = [[NSDate alloc] initWithTimeInterval:UTCDate.lowSeconds
sinceDate:scratch];
This and Brian's will be obsolete in the next few decades. (Oh boy, Y2K
again!)
Brendan Younger