• 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: UTCDateTime????
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: UTCDateTime????


  • Subject: Re: UTCDateTime????
  • From: Brian Hill <email@hidden>
  • Date: Wed, 1 Aug 2001 09:39:51 -0500

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
___________________________________________________________


  • Follow-Ups:
    • Re: UTCDateTime????
      • From: Brendan Younger <email@hidden>
References: 
 >UTCDateTime???? (From: Rainer Brockerhoff <email@hidden>)

  • Prev by Date: NSURL Proxy
  • Next by Date: Re: Need Help with Help (Help Viewer now dies..)
  • Previous by thread: UTCDateTime????
  • Next by thread: Re: UTCDateTime????
  • Index(es):
    • Date
    • Thread