Jump to content


Photo

Timezone & DST


  • Please log in to reply
7 replies to this topic

#1 SKAN

SKAN
  • Administrators
  • 9062 posts

Posted 13 July 2012 - 09:07 AM

I use the following to ascertain the Timezone difference:

#SingleInstance, Force
SetWorkingDir %A_ScriptDir%  

T1 := A_Now
T2 := A_NowUTC
EnvSub, T1, %T2%, M
TZD := "GMT +" Round( T1/60, 2 )

MsgBox % TZD ; 5.50 for IST - Indian Standard Time

There is no DST for IST.
If DST is in effect for a timezone, will it affect the above returned value?

#2 polyethene

polyethene

    Administrator

  • Administrators
  • 5473 posts

Posted 13 July 2012 - 09:26 AM

Returns 1.00 for me which is correct (BST).

#3 MasterFocus

MasterFocus
  • Moderators
  • 4130 posts

Posted 13 July 2012 - 09:27 AM

The MsgBox says "GMT + -3.00" for me. :lol:
(which is correct)

Related:
1) <!-- m -->http://www.timeanddate.com/time/dst/<!-- m -->
2) <!-- m --><!-- m -->

#4 Lexikos

Lexikos
  • Administrators
  • 8850 posts

Posted 13 July 2012 - 11:42 AM

DST affects the local time, therefore if you run the script in a time zone where DST is currently in effect, it will return a value which includes the DST adjustment. Querying A_Now simply calls GetLocalTime, while A_NowUTC calls GetSystemTime. You could use GetTimeZoneInformation.

#5 SKAN

SKAN
  • Administrators
  • 9062 posts

Posted 13 July 2012 - 02:22 PM

Thanks polyethene & Masterfocus :)

DST affects the local time, therefore if you run the script in a time zone where DST is currently in effect, it will return a value which includes the DST adjustment.


That was the clarification I was looking for. Many thanks. :)

You could use GetTimeZoneInformation.


I am aware of that function.
I have done an astro(nomy/logy) application. It computes planetary positions for given geo-coords and UTC time.
Converting UTC to local time ( in target machine ) is easier - irrespective of DST - as confirmed by you.

Now, I am wondering how should I go about converting UTC to a different Timezone...
For example, I have the UTC computed for Moonrise in Brisbane, Australia
I can easily convert it to Local computer time, but how to convert it to local Brisbane time?
I guess it is not possible without using third-party data like the following?
<!-- m -->http://api.geonames.... ... rname=demo<!-- m -->

The following snippet displays a clock for any timezone but without applying DST

#SingleInstance, Force
Gui, Font, s20
Gui, Add, Text, w175 h40 vTime 0x1201
Gui, Font
GoSub, TimezoneList
Gui, Add, DDL, wp r25 hwndhDDL gApplyTZD vTZE, %TZ%
SendMessage,0x160, 500,0,, ahk_id %hDDL% ; CB_SETDROPPEDWIDTH = 0x0160
Gui, Show
SetTimer UpdateTime

ApplyTZD:
 GuiControlGet, TZE
 TZD := SubStr( TZE,1,6 )
Return

UpdateTime:
 If ( A_NowUTC <> pUTC ) {
  DaT := A_NowUTC
  DaT += %TZD%, H
  FormatTime, Time, %DaT%, hh:mm:ss tt
  StringLower, Time, Time
  GuiControl,, time, %Time%
  pUTC := A_NowUTC
 }
Return

TimezoneList:
TZ=
 ( LTrim Join|
 -12.00 Dateline Standard Time > International Date Line West
 -11.00 Samoa Standard Time > Midway Island, Samoa
 -10.00 Hawaiian Standard Time > Hawaii
 -09.00 Alaskan Standard Time > Alaska
 -08.00 Pacific Standard Time > Pacific Time (US and Canada) Tijuana
 -07.00 Mountain Standard Time > Mountain Time (US and Canada)
 -07.00 Mountain Standard Time (Mexico) > Chihuahua, La Paz, Mazatlan
 -07.00 US Mountain Standard Time > Arizona
 -06.00 Central Standard Time (Mexico) > Guadalajara, Mexico City, Monterrey
 -06.00 Central Standard Time > Central Time (US and Canada)
 -06.00 Canada Central Standard Time > Saskatchewan
 -06.00 Central America Standard Time > Central America
 -05.00 Eastern Standard Time > Eastern Time (US and Canada)
 -05.00 SA Pacific Standard Time > Bogota, Lima, Quito
 -05.00 US Eastern Standard Time > Indiana (East)
 -04.00 Central Brazilian Standard Time > Manaus
 -04.00 Atlantic Standard Time > Atlantic Time (Canada)
 -04.00 SA Western Standard Time > Caracas, La Paz
 -04.00 Pacific SA Standard Time > Santiago
 -03.50 Newfoundland Standard Time > Newfoundland and Labrador
 -03.00 SA Eastern Standard Time > Buenos Aires, Georgetown
 -03.00 E. South America Standard Time > Brasilia
 -03.00 Greenland Standard Time > Greenland
 -02.00 Mid-Atlantic Standard Time > Mid-Atlantic
 -01.00 Azores Standard Time > Azores
 -01.00 Cape Verde Standard Time > Cape Verde Islands
 +00.00 GMT Standard Time > Greenwich Mean Time : Dublin, Edinburgh, Lisbon, London
 +00.00 Greenwich Standard Time > Casablanca, Monrovia
 +01.00 Central European Standard Time > Sarajevo, Skopje, Warsaw, Zagreb
 +01.00 Romance Standard Time > Brussels, Copenhagen, Madrid, Paris
 +01.00 Central Europe Standard Time > Belgrade, Bratislava, Budapest, Ljubljana, Prague
 +01.00 W. Europe Standard Time > Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna
 +01.00 W. Central Africa Standard Time > West Central Africa
 +02.00 E. Europe Standard Time > Minsk
 +02.00 South Africa Standard Time > Harare, Pretoria
 +02.00 Namibia Standard Time > Windhoek
 +02.00 GTB Standard Time > Athens, Bucharest, Istanbul
 +02.00 Egypt Standard Time > Cairo
 +02.00 FLE Standard Time > Helsinki, Kiev, Riga, Sofia, Tallinn, Vilnius
 +02.00 Israel Standard Time > Jerusalem
 +03.00 E. Africa Standard Time > Nairobi
 +03.00 Arab Standard Time > Kuwait, Riyadh
 +03.00 Russian Standard Time > Moscow, St. Petersburg, Volgograd
 +03.00 Arabic Standard Time > Baghdad
 +03.50 Iran Standard Time > Tehran
 +04.00 Azerbaijan Standard Time > Baku
 +04.00 Georgian Standard Time > Tblisi
 +04.00 Caucasus Standard Time > Yerevan
 +04.00 Arabian Standard Time > Abu Dhabi, Muscat
 +04.50 Afghanistan Standard Time > Kabul
 +05.00 Ekaterinburg Standard Time > Ekaterinburg
 +05.00 West Asia Standard Time > Islamabad, Karachi, Tashkent
 +05.50 India Standard Time > Chennai, Kolkata, Mumbai, New Delhi|
 +05.75 Nepal Standard Time > Kathmandu
 +06.00 Central Asia Standard Time > Astana, Dhaka
 +06.00 Sri Lanka Standard Time > Sri Jayawardenepura
 +06.00 N. Central Asia Standard Time > Almaty, Novosibirsk
 +06.50 Myanmar Standard Time > Yangon (Rangoon)
 +07.00 North Asia Standard Time > Krasnoyarsk
 +07.00 SE Asia Standard Time > Bangkok, Hanoi, Jakarta
 +08.00 North Asia East Standard Time > Irkutsk, Ulaanbaatar
 +08.00 Taipei Standard Time > Taipei
 +08.00 W. Australia Standard Time > Perth
 +08.00 Singapore Standard Time > Kuala Lumpur, Singapore
 +08.00 China Standard Time > Beijing, Chongqing, Hong Kong SAR, Urumqi
 +09.00 Korea Standard Time > Seoul
 +09.00 Tokyo Standard Time > Osaka, Sapporo, Tokyo
 +09.00 Yakutsk Standard Time > Yakutsk
 +09.50 AUS Central Standard Time > Darwin
 +09.50 Cen. Australia Standard Time > Adelaide
 +10.00 West Pacific Standard Time > Guam, Port Moresby
 +10.00 Vladivostok Standard Time > Vladivostok
 +10.00 AUS Eastern Standard Time > Canberra, Melbourne, Sydney
 +10.00 Tasmania Standard Time > Hobart
 +10.00 E. Australia Standard Time > Brisbane
 +11.00 Central Pacific Standard Time > Magadan, Solomon Islands, New Caledonia
 +12.00 Fiji Standard Time > Fiji Islands, Kamchatka, Marshall Islands
 +12.00 New Zealand Standard Time > Auckland, Wellington
 +13.00 Tonga Standard Time > Nuku'alofa
 )
Return


#6 Lexikos

Lexikos
  • Administrators
  • 8850 posts

Posted 14 July 2012 - 12:48 AM

Now, I am wondering how should I go about converting UTC to a different Timezone...

SystemTimeToTzSpecificLocalTime

#7 garry

garry
  • Members
  • 2596 posts

Posted 14 July 2012 - 10:22 AM

thank you for example and links
first example shows correct time

GMT+1 & DST+1 = CEST GMT +2.00 (switzerland)

some links

http://en.wikipedia.org/wiki/Tz_database

Time and date:
<!-- m -->http://www.timeanddate.com/<!-- m -->
<!-- m -->http://www.timeanddate.com/worldclock/<!-- m -->
* means the time shown has been adjusted for daylight saving time(DST) or summer time (67 places listed).
<!-- m -->http://www.timeanddate.com/time/dst/<!-- m -->
Time in Zurich :
<!-- m -->http://www.timeandda... ... tml?n=268#<!-- m -->

<!-- m -->http://wwp.world-clock.com/<!-- m -->
<!-- m -->http://wwp.greenwich... ... ean-union/<!-- m -->
currency:
<!-- m -->http://wwp.greenwich... ... /currency/<!-- m -->



#8 GodlyCheese

GodlyCheese
  • Members
  • 593 posts

Posted 14 October 2012 - 02:46 AM

I recently found myself in need of a similar function (one that accounts for DST) and decided to write a function for it that you might be interested in: <!-- l --><a class="postlink-local" href="http://www.autohotkey.com/community/viewtopic.php?f=1&t=93698&p=573919#p573919">viewtopic.php?f=1&t=93698&p=573919#p573919</a><!-- l -->

UTCToCurrentTimezone(TimeOutput,TimeInput,IsDSTHere,WasDSTThere)
   {
   T1 := A_Now
   T2 := A_NowUTC
   StringLen, TimeInputCount, TimeInput
   If TimeInput is not digit
      MsgBox,, Timezone Conversion, Error! You must use a YYYYMMDDHH24MISS timestamp.
   Else If TimeInputCount <> 14
      MsgBox,, Timezone Conversion, Error! You must use a YYYYMMDDHH24MISS timestamp.
   If IsDSTHere not in 0,1
      IsDSTHere = 1            ;Assumes you're in the U.S.
   If WasDSTThere not in 0,1
      WasDSTThere = 1          ;Assumes the date is also in the U.S.
   If IsDSTHere = 1
      {
      FormatTime, YearCheck, %T1%, yyyy
      WhenDSTStart = %YearCheck%0301020000
      Loop
         {
         If Count = 2
            {
            Count =
            DSTCheck =
            Break
            }
         FormatTime, DSTCheck, %WhenDSTStart%, ddd
         If DSTCheck = Sun
            {
            If Count =
               WhenDSTStart += 1, Days
            Count++
            }
         Else
            WhenDSTStart += 1, Days
         }
      WhenDSTEnd = %YearCheck%1101020000
      Loop
         {
         FormatTime, DSTCheck, %WhenDSTEnd%, ddd
         If DSTCheck = Sun
            Break
         WhenDSTEnd += 1, Days
         }
      If T1 between %WhenDSTStart% and %WhenDSTEnd%   ;DST Active
         T1 += -1, Hours
      EnvSub, T1, %T2%, H  
      }
   Else
      EnvSub, T1, %T2%, H        ;Real difference is stored as T1
   TimeInput += %T1%, Hours
   If WasDSTThere = 1           ;Old date was in U.S.
      {
      FormatTime, YearCheck, %TimeInput%, yyyy
      WhenDSTStarted = %YearCheck%0301020000
      Loop                                        ;Retrieves second sunday in march, aka DST start +1h
         {
         If Count = 2
            {
            Count =
            DSTCheck =
            Break
            }
         FormatTime, DSTCheck, %WhenDSTStarted%, ddd
         If DSTCheck = Sun
            {
            If Count =
               WhenDSTStarted += 1, Days
            Count++
            }
         Else
            WhenDSTStarted += 1, Days
         }                                     ;DST Start +1h
      WhenDSTEnded = %YearCheck%1101020000
      Loop
         {
         FormatTime, DSTCheck, %WhenDSTEnded%, ddd
         If DSTCheck = Sun
            Break
         WhenDSTEnded += 1, Days
         }
      YearCheck =
      If TimeInput between %WhenDSTStarted% and %WhenDSTEnded%   ;DST was active
         TimeInput += 1, Hours
      }
   %TimeOutput% := TimeInput
   Return
   }

It converts an old UTC date into your current Timezone and reliably accounts for DST. Only catch is that you have to tell the function if DST applies where you are now/when the UTC date was recorded. Note: This function relies on the assumption that DST starts the second Sunday in March and ends the first Sunday in November.