Re: NSDate
Re: NSDate
- Subject: Re: NSDate
- From: Shane Stanley <email@hidden>
- Date: Tue, 02 Feb 2010 21:36:05 +1100
- Thread-topic: NSDate
On 20/10/09 6:29 AM, "Christopher Nebel" <email@hidden> wrote:
> On Oct 16, 2009, at 4:00 PM, Shane Stanley wrote:
>
>> Has anyone had any luck coercing an NSDate into an AS date, or an AS
>> anything?
>
> The most straightforward way (which is also completely international-
> safe) is probably to use the "timeIntervalSinceReferenceDate" method,
> which will give you a number of seconds difference from midnight,
> January 1, 2001 GMT, which you can then add to the AppleScript date
> "1/1/2001". Depending on the effect you want, you may need to add in
> "time to GMT" as well, since NSDate objects represent a specific point
> in absolute time, unlike AppleScript dates which imply a time zone.
I know this was a while back and on another list, but...
There's a potential problem with this method because of daylight savings
time.
When you add time to GMT, you're adding the time difference for *today* in
your locale, but when you get an NSDate, it returns a value based on the
offset *at the date in question*. (Surprisingly, to me anyway, this even
extends to the point that a date, say, 50 years ago will be returned with an
offset ignoring the daylight savings difference, when the same day now
includes the difference.)
Because dates default to 12 a.m., one hour's difference can mean if you
ignore the time, you'll be a day out.
Here's an example (bear in mind that it's daylight savings time in January
here, and we go to +1100 from +1000):
set theASDate to date "Tuesday, January 3, 1956 12:00:00 AM"
set theDiff to theASDate - (date "Monday, January 1, 2001 12:00:00 AM") -
(time to GMT)
set theNSDate to current application's NSDate's
dateWithTimeIntervalSinceReferenceDate_(theDiff)
log theNSDate
-- 1956-01-02 23:00:00 +1000
set theDiff to theNSDate's timeIntervalSinceReferenceDate()
set ASDate to (date "Monday, January 1, 2001 12:00:00 AM") + theDiff +
(time to GMT)
log (ASDate as text)
-- Tuesday, January 3, 1956 12:00:00 AM
set theASDate to date "Sunday, January 3, 2010 12:00:00 AM"
set theDiff to theASDate - (date "Monday, January 1, 2001 12:00:00 AM") -
(time to GMT)
set theNSDate to current application's NSDate's
dateWithTimeIntervalSinceReferenceDate_(theDiff)
log theNSDate
-- 2010-01-03 00:00:00 +1100
set theDiff to theNSDate's timeIntervalSinceReferenceDate()
set ASDate to (date "Monday, January 1, 2001 12:00:00 AM") + theDiff +
(time to GMT)
log (ASDate as text)
-- Sunday, January 3, 2010 12:00:00 AM
So it looks like the more long-winded method using NSCalendar and then
setting the various AS date components is the way to go.
Also, date coercions seem to be impossible. This works:
date "Monday, January 1, 2001 12:00:00 AM"
But this fails:
set x to "Monday, January 1, 2001 12:00:00 AM"
date x
I also tried "x as date", also without success. coercions generally seem
problematical.
--
Shane Stanley <email@hidden>
AppleScript Pro, April 2010, Florida <http://www.applescriptpro.com>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
applescriptobjc-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden