Re: Time offset for future dates
Re: Time offset for future dates
- Subject: Re: Time offset for future dates
- From: "Nigel Garvey" <email@hidden>
- Date: Tue, 19 Dec 2017 13:16:45 +0000
Takaaki Naganoya wrote on Mon, 18 Dec 2017 23:57:52 +0900:
>use AppleScript version "2.5"
>use scripting additions
>use framework "Foundation"
>
>set aDate to getDateInternational(2018, 2, 1) of me ——year, month, date
>set bStr to retISO8601DateTimeString(aDate, "CET") as string
>--> "2018-01-31T16:00:00+0100"
>
>--NSDate -> ISO8601 Date & Time String
>on retISO8601DateTimeString(targDate, timeZoneName)
> set theNSDateFormatter to current application's NSDateFormatter's
alloc()'s init()
> theNSDateFormatter's setDateFormat:"yyyy-MM-dd'T'HH:mm:ssZZZ"
> theNSDateFormatter's setTimeZone:(current application's NSTimeZone's
alloc()'s initWithName:timeZoneName)
> return (theNSDateFormatter's stringFromDate:targDate) as text
>end retISO8601DateTimeString
>
>--Make Date Object from parameters
>on getDateInternational(aYear, aMonth, aDay)
> set theNSCalendar to current application's NSCalendar's
currentCalendar()
> set theDate to theNSCalendar's dateWithEra:1 |year|:aYear
|month|:aMonth |day|:aDay hour:0 minute:0 |second|:0 nanosecond:0
> return theDate as date
>end getDateInternational
Hi.
1. The getDateInternational() handler returns the same AS date for the
same given input regardless of the computer's time zone setting — even
when hours, minutes, and seconds are included. So there's no need for
the overhead of ASObjC in it.
2. "CET" is an "abbreviation", not a "name". But since the machine has
to be set to a CET time zone anyway to get the right end result from the
input, that parameter could be omitted.
3. Alternatively, for complete control, the time zone abbreviation could
be passed to both handlers, in which case aDate (if still obtained as an
AS date) would be the computer-local time corresponding to required time
in the specified time zone:
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
set aDate to getDateInternational(2018, 12, 18, 9, 59, 35, "CET") --—year,
month, date, hour, minute, second, time zone abbreviation.
set bStr to retISO8601DateTimeString(aDate, "CET") as string
--> "2018-12-18T09:59:35+01:00"
--NSDate -> ISO8601 Date & Time String
on retISO8601DateTimeString(targDate, timeZoneAbbreviation)
set theNSDateFormatter to current application's NSDateFormatter's alloc()'s
init()
theNSDateFormatter's setDateFormat:"yyyy-MM-dd'T'HH:mm:ssZZZZZ" -- Five
zeds to get a colon in the time offset (except with GMT).
theNSDateFormatter's setTimeZone:(current application's NSTimeZone's
timeZoneWithAbbreviation:(timeZoneAbbreviation))
return (theNSDateFormatter's stringFromDate:targDate) as text
end retISO8601DateTimeString
--Make a GMT Date Object with parameters from a given time zone.
on getDateInternational(aYear, aMonth, aDay, anHour, aMinute, aSecond,
timeZoneAbbreviation)
set theNSCalendar to current application's NSCalendar's currentCalendar()
theNSCalendar's setTimeZone:(current application's NSTimeZone's
timeZoneWithAbbreviation:(timeZoneAbbreviation))
set theDate to theNSCalendar's dateWithEra:1 |year|:aYear |month|:aMonth
|day|:aDay hour:anHour minute:aMinute |second|:aSecond nanosecond:0
return theDate as date
end getDateInternational
NG
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden