Monday, July 4, 2022

#1 2013-06-30 06:48:58 pm

kel1
Member
Registered: 2013-01-11
Posts: 2188

time to gmt

Hi,

If I use:

Applescript:

time to GMT
-->- 36000

that means -10 hours from Greenwich time right? i.e. GMT is correct? I think it must be true because I used -10 in the sunrise and sunset calendar. Some program said it was -11. I wonder if that's with daylight saving time? We don't have daylight saving time in Hawaii.

Thanks,


Os 10.10.3
Mbp

Offline

 

#2 2013-06-30 09:16:34 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6779

Re: time to gmt

It's the current time from GMT according to the locale you have set in the Time Zone panel of System Preferences, including any difference for daylight savings. The Closest City matters, because not all places in a time zone necessarily have the same rules for daylight savings.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2013-07-02 11:49:27 pm

kel1
Member
Registered: 2013-01-11
Posts: 2188

Re: time to gmt

Hi Shane,

Found the closest city in System Preferences. Was wondering if 'time to gmt' was connected to something I set.

Thanks a lot,
kel


Os 10.10.3
Mbp

Offline

 

#4 2013-07-03 04:10:38 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2809
Website

Re: time to gmt

kel1 wrote:

I wonder if that's with daylight saving time? We don't have daylight saving time in Hawaii.


Timezone's doesn't have daylight saving time. The only difference is that in the UK (not Ireland, but I'm not sure about that) they switch between GMT and BST (British Summer Time). In Europe we switch between CET (Central Europe Time) and CEST (Central Europe Summer Time). The time zones itself never changes time, for summer time/daylight saving time we just switch timezones.

So to answer your question: Because you aren't switching timezones for summer time, the difference between your timezone and Greenwich Mean Time should be the same all year.

Last edited by DJ Bazzie Wazzie (2013-07-03 04:20:28 am)

Offline

 

#5 2013-07-03 04:23:20 am

kel1
Member
Registered: 2013-01-11
Posts: 2188

Re: time to gmt

Hi DJ Bazzie Wazzie,

That's good to know. Because I started up this computer before daylight savings time started (sprang forward). The an app I ran was wrong in thinking time to gmt was -11. 'time to gmt' is already preset, according to your settings in System Preferences for Time Zones' closest city.

Thanks,
kel


Os 10.10.3
Mbp

Offline

 

#6 2013-07-03 04:19:16 pm

juergen
Member
From:: Germany
Registered: 2010-11-01
Posts: 46
Website

Re: time to gmt

Hi DJ Bazzie Wazzie,

are you sure? Olson time zones (https://en.wikipedia.org/wiki/Tz_database) know about summer time (daylight saving time) since 1970. That is exactly the reason, why Europe/Paris is not the same as Europe/Amsterdam. Both cities have the same system for standard time / summer time today. But they did not always since 1970.

Today on my machine (in time zone Europe/Berlin), “time to GMT” returns 7200, i.e. two hours. Berlin is one hour east of London, but we have summer time. UTC (a linguistic cross breed of Coordinated universal time and Temps universel coordonné) never has. Hence two hours difference.

And keep in mind that “time to GMT” generally makes sense for “current date” only. It does not help you to convert times in December if you are in June, unless you are in a time zone without summer time.

Jürgen

Offline

 

#7 2013-07-03 05:23:07 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6779

Re: time to gmt

juergen wrote:

Hi DJ Bazzie Wazzie,

are you sure? Olson time zones (https://en.wikipedia.org/wiki/Tz_database) know about summer time (daylight saving time) since 1970. That is exactly the reason, why Europe/Paris is not the same as Europe/Amsterdam. Both cities have the same system for standard time / summer time today. But they did not always since 1970.

DJ is talking from the user's perspective. The underlying timezones, like Europe/Amsterdam, do have all the daylight savings information, and are adjusted accordingly. But what is shown to the user is the time zone in terms of things like Central Europe Time and Central Europe Summer Time. So when daylight savings starts, the timezone the user sees changes.


And keep in mind that “time to GMT” generally makes sense for “current date” only. It does not help you to convert times in December if you are in June, unless you are in a time zone without summer time.


It's even sillier than that. Change your preferences so that the date strings show the timezone, and do the calculation with dates in and out of daylight savings time. The timezone in the results is wrong (or the displayed time is wrong -- take your pick).


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#8 2013-07-03 08:08:43 pm

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: time to gmt

Hello.

It is good to be informed about the bugs.  Nigel Garvey provides a fix/work around in this post that works from Applescript. I haven't really considered the day light savings time, but performing a conversion operation with some care, should get exact results.

Like considering any daylight savings time in the time zone you are currently in, if the conversion function doesn't do it.

There are also ways to bypass this if you resort to C, althoughI can't recollect having seen anything about towns in the locale when specifiying timezones for the the time functions. Allbeit that may be possible. I am sure the ICU library handles this correctly, but then again, I am always optimistic. smile

There are probably even easier ways to resolve this in Cocoa / Objective-C, and maybe ASObjC, than in C, that also considers the town of the timezone.

Last edited by McUsrII (2013-07-03 08:18:03 pm)

Offline

 

#9 2013-07-04 04:59:19 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2809
Website

Re: time to gmt

juergen wrote:

are you sure? Olson time zones (https://en.wikipedia.org/wiki/Tz_database) know about summer time (daylight saving time) since 1970. That is exactly the reason, why Europe/Paris is not the same as Europe/Amsterdam. Both cities have the same system for standard time / summer time today. But they did not always since 1970.


You're right Jürgen. We, the Netherlands, had our own time zone known as Amsterdam Mean Time (AMT). I think it had an 20 minute delay or something. But the Olson database isn't an timezone as CET or UTC, it's an geographic location with rules about their time. But today, looking at the rules in the Olson database it just sets the time zone to CET and CEST and nothing more. In other words, the rules in the Olson database is only to switch between timezones. And looking at other countries (in the europe database), the same applies, they are only switching between WET/WEST, CET/CEST and EET/EEST. So I consider those geographical location more as an alias or pointer to the correct timezones rather than specific time rules.

Offline

 

#10 2013-07-04 07:17:19 am

juergen
Member
From:: Germany
Registered: 2010-11-01
Posts: 46
Website

Re: time to gmt

It is a combination. Rules are set differently for different years. As the Wikipedia defines: “Within the tz database, a time zone is any national region where local clocks have all agreed since 1970”. The names of time zones typically combine a continent name and a city name. Someone had the wise insight that cities tend to be more stable than country borders and names.

For a given time, the tz database matches an Olson time zone to a traditional one like BST. But it may match the same Olson tz to different traditional time zones for different dates. The following AppleScript shows that (I have a day-month-year date format, Time zone is Europe/Berlin):

Applescript:

set dt1 to date ("1.7.2013")
set dt2 to date ("1.7.1975")

tell application "System Events"
   set pl to (make new property list item with properties {value:{dt1, dt2}})
   set r to text of pl
end tell
r

(* r now is
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<array>
   <date>2013-06-30T22:00:00Z</date>
   <date>1975-06-30T23:00:00Z</date>
</array>
</plist>
"
*)

Both dates are midnight and both are in the summer. The 2013 time is two hours apart from UTC, the 1975 date is one hour apart. (We switched 1980 in Germany.) Europe/Berlin is matched to CEST for 2013 and to CET for 1975.

If you check the tz file for Asia and have a look at Israel, you will find that almost every year has its own rule. (Rules are in the Gregorian calendar. If memory serves, Israel uses the Hebrew calendar for the switch in autumn so far.)

So the important points to keep in mind:

If your region has no daylight saving time (like Hawaii in the original question) and never had, “time to gmt” can be used to convert from local time to UTC and vice versa.

In all other cases, conversion is tricky. Have a look at Nigel’s script that McUsrII linked above.

(Just another nasty effect:

Applescript:

set dt1 to date ("26.10.2013 12:00")
set dt2 to date ("27.10.2013 12:00")

(dt2 - dt1) / hours

returns 24.0
Actually the difference  is 25 hours difference in my time zone.)

Jürgen

Offline

 

#11 2013-07-04 09:39:36 am

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: time to gmt

Hello.

I believe I have the same daylight savings time as Juergen, provided that the 27.10.2012 or whatever date was the last sunday of october. The subtraction of the two dates fail to take the extra hour into account as you move the time forward again, (You can sleep one hour longer smile).

The smoothest way I can think of when it comes to calculate time intervals between timezones, is to convert to seconds since Epoch, and convert from seconds since Epoch back into date/time.

That is ok, when you are dealing with whole years, since it will even out over a year. If you convert a date/time for a Timezone (TZ) into seconds since Epoch (1970.01.01:00:00 ) I think that calculation should be calculated correctly by the date functions. But you have to use the correct TZ at that given point in time. (Daylight savings or not).

The same thing goes around, when converting from seconds since Epoch to a timezone, you'll have to know the end result, maybe perform a preflight operation, to see what date you'll end up with, and then perform the calulcation again, if the date/time is within daylight savings time. (Or just subtract the factor).

This is a hard one to fix in AppleScript. But there are handlers for converting from a date to seconds since Epoch, I can't remember those functions consider TZ last time I looked, but that shouldn't be to hard to fix, by setting LC_ALL or LC_TZ in between the calls to the unix date function.

This should at least be as "clean" a solution as it is possible to get. however, some  heuristics for dealing with the individual timezones, is needed, you'll have to know if daylight savings time is active in the timezone for the end-date you are converting to. (And you'll of course have to use the correct timezone forthe timezone you are converting from.

(This post became heavily editied. smile)

Last edited by McUsrII (2013-07-04 10:21:59 am)

Offline

 

#12 2013-07-04 10:19:03 am

kel1
Member
Registered: 2013-01-11
Posts: 2188

Re: time to gmt

Hi everybody,

Great information as always. Just want I wanted to know about the time zones also.

Thanks a lot,
kel


Os 10.10.3
Mbp

Offline

 

#13 2013-07-04 10:37:55 am

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: time to gmt

Hello.

I edited my post heavily! smile My conclusion is that applescript date-objects, aren't practical for anything else, than working  with the time and date in your local timezone, if you work with several timezones, then having variables with time to Epoch in seconds, may turn out to be simpler to deal with. ( I believe a date object in Applescript ends up with being in local time anyway.)

Last edited by McUsrII (2013-07-04 10:38:31 am)

Offline

 

#14 2013-07-04 10:41:34 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5530

Re: time to gmt

juergen wrote:

In all other cases, conversion is tricky. Have a look at Nigel’s script that McUsrII linked above.

(Just another nasty effect:

Applescript:

set dt1 to date ("26.10.2013 12:00")
set dt2 to date ("27.10.2013 12:00")

(dt2 - dt1) / hours

returns 24.0
Actually the difference  is 25 hours difference in my time zone.)


But of course, using my handler:

Applescript:

on TZtoTZ(TZ1date, TZ1, TZ2)
   return (do shell script ("eraTime=$(TZ=" & TZ1 & " date -jf '%Y-%m-%dT%H:%M:%S' '" & (TZ1date as «class isot» as string) & "' '+%s') ; TZ=" & TZ2 & " date -r \"$eraTime\" '+%Y-%m-%dT%H:%M:%S'") as «class isot») as date
end TZtoTZ

set dt1 to date ("26.10.2013 12:00")
set dt2 to date ("27.10.2013 12:00")

(TZtoTZ(dt2, "Europe/Berlin", "GMT") - TZtoTZ(dt1, "Europe/Berlin", "GMT")) / hours --> 25.0


NG

Offline

 

#15 2013-07-04 11:18:15 am

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: time to gmt

Now, if they ever remove class isot, we'll have to turn up in Cupertino… smile And send an invoice later of course. big_smile

Nice handler Nigel!

Last edited by McUsrII (2013-07-04 11:18:48 am)

Offline

 

#16 2013-07-04 11:33:05 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5530

Re: time to gmt

Or a slightly more useful 'time to GMT' function:

Applescript:

on TZtoTZ(TZ1date, TZ1, TZ2)
   return (do shell script ("eraTime=$(TZ=" & TZ1 & " date -jf '%Y-%m-%dT%H:%M:%S' '" & (TZ1date as «class isot» as string) & "' '+%s') ; TZ=" & TZ2 & " date -r \"$eraTime\" '+%Y-%m-%dT%H:%M:%S'") as «class isot») as date
end TZtoTZ

on timeToGMT on dt for TZ
   return dt - TZtoTZ(dt, TZ, "GMT")
end timeToGMT

set dt1 to date ("26.10.2013 12:00")
set localTZ to (do shell script ("readlink '/etc/localtime' | sed 's|/usr/share/zoneinfo/||'"))
timeToGMT on dt1 for localTZ

Last edited by Nigel Garvey (2013-07-04 02:46:08 pm)


NG

Offline

 

#17 2013-07-04 11:51:47 am

kel1
Member
Registered: 2013-01-11
Posts: 2188

Re: time to gmt

Hi Nigel,

When I run your script:

Applescript:

on TZtoTZ(TZ1date, TZ1, TZ2)
   return (do shell script ("eraTime=$(TZ=" & TZ1 & " date -jf '%Y-%m-%dT%H:%M:%S' '" & (TZ1date as «class isot» as string) & "' '+%s') ; TZ=" & TZ2 & " date -r \"$eraTime\" '+%Y-%m-%dT%H:%M:%S'") as «class isot») as date
end TZtoTZ

set dt1 to date ("10/26/2013 12:00")
set dt2 to date ("10/27/2013 12:00")

(TZtoTZ(dt2, "Europe/Berlin", "GMT") - TZtoTZ(dt1, "Europe/Berlin", "GMT")) / hours --> 25.0

I get -24. Did I miss something?


Os 10.10.3
Mbp

Offline

 

#18 2013-07-04 11:58:08 am

kel1
Member
Registered: 2013-01-11
Posts: 2188

Re: time to gmt

That's ok. I see it is relative to your computer's settings (where you are).


Os 10.10.3
Mbp

Offline

 

#19 2013-07-04 12:02:10 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5530

Re: time to gmt

kel1 wrote:

I get -24. Did I miss something?


Possibly your computer's set for a 12-hour clock and "12:00" is being compiled as midnight instead of midday.


NG

Offline

 

#20 2013-07-04 02:05:09 pm

kel1
Member
Registered: 2013-01-11
Posts: 2188

Re: time to gmt

Hi Nigel,

Setting System Preferences to 24 hour clock didn't make it work, but adding PM to the times did work.

Applescript:

on TZtoTZ(TZ1date, TZ1, TZ2)
   return (do shell script ("eraTime=$(TZ=" & TZ1 & " date -jf '%Y-%m-%dT%H:%M:%S' '" & (TZ1date as «class isot» as string) & "' '+%s') ; TZ=" & TZ2 & " date -r \"$eraTime\" '+%Y-%m-%dT%H:%M:%S'") as «class isot») as date
end TZtoTZ

set dt1 to date ("10/26/2013 12:00 PM")
set dt2 to date ("10/27/2013 12:00 PM")

(TZtoTZ(dt2, "Europe/Berlin", "GMT") - TZtoTZ(dt1, "Europe/Berlin", "GMT")) / hours --> 25.0

Where do you get the locations from like "Europe/Berlin"?

Thanks,
kel


Os 10.10.3
Mbp

Offline

 

#21 2013-07-04 02:15:08 pm

kel1
Member
Registered: 2013-01-11
Posts: 2188

Re: time to gmt

That's alright, I need to see 'environ'.

Thanks,
kel


Os 10.10.3
Mbp

Offline

 

#22 2013-07-04 02:19:09 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11767
Website

Re: time to gmt

kel1 wrote:

Setting System Preferences to 24 hour clock didn't make it work


the 12/24 hour setting in Date & Time is just the display mode,
the system locale settings are located in Language & Text > Region


regards

Stefan

Offline

 

#23 2013-07-04 02:47:46 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5530

Re: time to gmt

kel1 wrote:

Where do you get the locations from like "Europe/Berlin"?


In this particular case, I just used "Europe/Berlin" because that's what juergen said was his time zone in post #6. One way to get the time zone on the local computer is, as in post #16, to parse the POSIX path pointed to by the symbolic link "/etc/localtime":

Applescript:

set localTZ to (do shell script ("readlink '/etc/localtime' | sed 's|/usr/share/zoneinfo/||'"))


NG

Offline

 

#24 2013-07-04 07:11:49 pm

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: time to gmt

This was interesting, I started to dig a little into the zoneinfo files that lies below /usr/share.

When I use the command zdump zoneinfo/US/Hawaii I get the current time back, according to the manual, in utc.  So, they don't do much useful, the commands to read tzone files from the shell. I think you'll have to set the LC_TZ to something for their usefullness to kick in.

I was hoping to get the offset or something, instead I ended up with the knowledge that leap seconds aren't included in posix time (seconds since epoch.) hmm (man time2posix)

Last edited by McUsrII (2013-07-04 07:13:08 pm)

Offline

 

#25 2013-07-04 07:37:18 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6779

Re: time to gmt

juergen wrote:

(Just another nasty effect:

Applescript:

set dt1 to date ("26.10.2013 12:00")
set dt2 to date ("27.10.2013 12:00")

(dt2 - dt1) / hours

returns 24.0
Actually the difference  is 25 hours difference in my time zone.)


What you're seeing is correct; you're just not looking at the full picture. Change your script to this:

Applescript:

set dt1 to date ("26.10.2013 12:00")
set dt2 to date ("27.10.2013 12:00")
log dt1
log dt2

(dt2 - dt1) / hours

Now go to System Preferences, Language & Text, and change the Medium format for time display so that it also includes the timezone. Run the script and look at the logged values. You might now agree with AppleScript wink

If you want to do much with AS dates, the key is to ignore the string value, and just deal with the underlying components.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)