Re: Issues subtracting Unix epoch from date
Re: Issues subtracting Unix epoch from date
- Subject: Re: Issues subtracting Unix epoch from date
- From: "Nigel Garvey" <email@hidden>
- Date: Sat, 20 Feb 2010 20:34:11 +0000
"Mark J. Reed" wrote on Sat, 20 Feb 2010 09:42:59 -0500:
> You need something that will
>tell you the offset to UTC as of an arbitrary date, not just as of
>now. Which is a hard thing to do, given the way the DST rules have
>shifted over time and space.
(* Convert an ISO-format date string to an AppleScript date. *)
on isotToDate(isot)
set theDate to date "Monday 25 November 1000 00:00:00"
set n to (text 1 thru 8 of isot) as integer
set theDate's year to n div 10000
set theDate's month to (n mod 10000 div 100)
set theDate's day to n mod 100
if ((count isot) > 8) then
set n to (text 10 thru 15 of isot) as integer
set theDate's time to n div 10000 * hours + n mod 10000 div 100 *
minutes + n mod 100
end if
return theDate
end isotToDate
(* Transpose a date/time from GMT to a given time zone. *)
on GMTtoTZ(GMTDate, timeZoneID)
set eraTime to (GMTDate - (date "Thursday 1 January 1970 00:00:00"))
as text
-- Crude scientific notation -> decimal conversion. OK for whole numbers.
set e to (offset of "E" in eraTime)
if (e > 0) then
set dot to (offset of "." in eraTime)
set eraTime to text 1 thru (dot - 1) of eraTime & text 1 thru
(text (e + 1) thru -1 of eraTime) of (text (dot + 1) thru (e - 1) of
eraTime & "0000000000")
end if
return isotToDate(do shell script ("TZ='" & timeZoneID & "' /bin/
date -r " & eraTime & " +%Y%m%dT%H%M%S"))
end GMTtoTZ
(* Transpose a date/time from a given time zone to GMT. *)
on TZtoGMT(TZDate, timeZoneID)
-- The difference between TZDate when it's local and the GMT date we
want is usually
-- the same as the difference between the local date when TZDate is
GMT and TZDate itself …
set GMTDate to TZDate - (GMTtoTZ(TZDate, timeZoneID) - TZDate)
-- … but not around the time the clocks go forward. If the GMT
obtained doesn't reciprocate to TZDate,
-- shift to a nearby local date where the above DOES work, get a new
GMT, unshift it by the same amount.
set checkDate to GMTtoTZ(GMTDate, timeZoneID)
if (checkDate is not TZDate) then
if (GMTDate > checkDate) then -- "Clocks forward" is towards GMT.
set shift to GMTDate - checkDate
else -- "Clocks forward" is away from GMT.
set shift to -days
end if
set nearbyDate to TZDate + shift
set GMTDate to nearbyDate - (GMTtoTZ(nearbyDate, timeZoneID) -
nearbyDate) - shift
end if
return GMTDate
end TZtoGMT
on epochTime(GMTDate)
return GMTDate - (date "Saturday 1 January 1972 00:00:00")
end epochTime
-- Perl code by Mark J. Reed.
set myTZ to (do shell script "perl -le 'print( readlink(\"/etc/
localtime\") =~m{zoneinfo/(.*)} )' ")
set arbitraryLocalDate to date "Saturday 30 June 2007 05:05:00"
set epochSeconds to epochTime(TZtoGMT(arbitraryLocalDate, myTZ))
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