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: "James J. Merkel" <email@hidden>
- Date: Fri, 4 Jun 2004 20:53:05 -0700
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:
-(NSString*)dateTimeStringFromUnixTime:(time_t)time{
struct tm *utc;
NSString *dateString, *dateTimeString;
utc = gmtime(&time);
dateString = [NSString localizedStringWithFormat:@"%d", (1900 +
utc->tm_year)];
dateString = [dateString stringByAppendingString:@":"];
dateString = [dateString stringByAppendingString:[NSString
localizedStringWithFormat:@"d", (1 + utc->tm_mon)]];
dateString = [dateString stringByAppendingString:@":"];
dateString = [dateString stringByAppendingString:[NSString
localizedStringWithFormat:@"d", utc->tm_mday]];
dateString = [dateString stringByAppendingString:@" "];
dateString = [dateString stringByAppendingString:[NSString
localizedStringWithFormat:@"d", utc->tm_hour]];
dateString = [dateString stringByAppendingString:@":"];
dateString = [dateString stringByAppendingString:[NSString
localizedStringWithFormat:@"d", utc->tm_min]];
dateString = [dateString stringByAppendingString:@":"];
dateString = [dateString stringByAppendingString:[NSString
localizedStringWithFormat:@"d", utc->tm_sec]];
dateTimeString = [[NSString alloc]initWithString:dateString];
[dateTimeString autorelease];
return dateTimeString;
}
Hope this helps,
Jim Merkel
_______________________________________________
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.