Re: Representing UTC time in a readable format
Re: Representing UTC time in a readable format
- Subject: Re: Representing UTC time in a readable format
- From: Jean-Denis Muys <email@hidden>
- Date: Sat, 5 Jun 2004 17:14:03 +0200
On Jun 5, 2004, at 7:00 AM, email@hidden wrote:
12. Re: Representing UTC time in a readable format (James J. Merkel)
Message: 12
Cc: Cocoa-Dev Mail <email@hidden>
From: "James J. Merkel" <email@hidden>
Subject: Re: Representing UTC time in a readable format
Date: Fri, 4 Jun 2004 20:53:05 -0700
To: email@hidden
On 2004-06-04, at 11.59, Jeremy Dronfield wrote:
I'd like to be able to insert a time readout represented as UTC time
in
my text. Since there doesn't appear to be a Cocoa way to do it, I'm
doing this to get the UTC date/time:
UTCDateTime *utcDateTime;
OSStatus error = GetUTCDateTime((UTCDateTime *)&utcDateTime,
kUTCDefaultOptions);
UInt16 high = utcDateTime->highSeconds;
UInt32 low = utcDateTime->lowSeconds;
NSLog(@"utcDateTime:%qu", utcDateTime);
NSLog(@"high:%hu", high);
NSLog(@"low:%u", low);
which gives me a log output like this:
utcDateTime:207691735333048
high:997
low:3040632576
How can I translate this into a standard human-readable representation
of UTC time? I'm not clear about the relationship between the high and
low bits, and which I should use.
I think what you are asking is how to convert a time that is expressed
in seconds from Jan 1, 1970 (i.e. Unix C time() style format) to a
human readable representation in an NSString. If so, you can use the
functions in the C time.h library and build an NSString as follows:
Actually, this is probably incorrect, as GetUTCDateTime returns a date
with an epoch of Jan 1, 1904. Here is an unoptimized function that
converts the UTCDateTime struct filled by that call to a
NSCalendarDate:
NSCalendarDate *UTCTimeToNSCalendarDate(UTCDateTime *anUTCtime)
{
unsigned long long timeAsULL = (((unsigned long
long)anUTCtime->highSeconds) << 32) + anUTCtime->lowSeconds;
NSDecimalNumber *intSeconds = [NSDecimalNumber
numberWithUnsignedLongLong:timeAsULL];
NSDecimalNumber *fracSeconds = [NSDecimalNumber
numberWithUnsignedInt:anUTCtime->fraction];
NSDecimalNumber *fracBase = [NSDecimalNumber
numberWithUnsignedLong:65536];
NSDecimalNumber *subSeconds = [fracSeconds
decimalNumberByDividingBy:fracBase];
NSDecimalNumber *nowAsDec = [intSeconds
decimalNumberByAdding:subSeconds];
NSTimeInterval nowAsDouble = [nowAsDec doubleValue];
NSCalendarDate *UTCepoch = [NSCalendarDate dateWithYear:1904 month:1
day:1 hour:0 minute:0 second:0 timeZone:[NSTimeZone
timeZoneForSecondsFromGMT:0]];
NSCalendarDate *nowAsDate = [UTCepoch addTimeInterval:nowAsDouble];
return nowAsDate;
}
Then of course, it's easy to display it. For example:
UTCDateTime nowAsUTC;
OSStatus OSerr = GetUTCDateTime(&nowAsUTC, kUTCDefaultOptions);
NSCalendarDate *now = UTCTimeToNSCalendarDate(&nowAsUTC);
[someText appendString: [now description]];
Of course, this doesn't take into account all the interesting
information about UTC that has been delivered in this thread!
Regards,
Jean-Denis
_______________________________________________
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.