• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to convert an UTCDateTime to a NSDate
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

  • Prev by Date: Removing HTML Tags
  • Next by Date: Re: Seeing an objects retain count
  • Previous by thread: How to convert an UTCDateTime to a NSDate
  • Next by thread: Where are true print margin values?
  • Index(es):
    • Date
    • Thread