www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to get the current Timezone

reply "wobbles" <grogan.colin gmail.com> writes:
 From the docs in std.datetime, I figured I could write:

Clock.currTime.timezone().name()

to get the timezone this system is in. However, it just returns 
an empty string.
Anyone know how to get the timezone of the machine easily?

Thanks!
Aug 28 2015
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Friday, 28 August 2015 at 14:07:37 UTC, wobbles wrote:
 However, it just returns an empty string.
from the doc: Note that this always returns the empty string. This is because time zones cannot be uniquely identified by the attributes given by the OS (such as the stdName and dstName), and neither Posix systems nor Windows systems provide an easy way to get the TZ Database name of the local time zone.
 Anyone know how to get the timezone of the machine easily?
Best you can do is try the stdName or dstName properties of Timezone, perhaps checking if dstInEffect to choose which one to print. Or ou could use the utcOffsetAt function to print a number (like UTC-4:00, after formatting it) The actual location the user sets in the timezone settings of the computer I think the docs are right about - it isn't available on either OS, even using platform specific APIs... though I'm sure it is stored somewhere and maybe you could e.g. dig into the Windows registry to find it.
Aug 28 2015
next sibling parent "Kagamin" <spam here.lot> writes:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation ?
Aug 28 2015
prev sibling parent reply "WhatMeWorry" <kheaser gmail.com> writes:
On Friday, 28 August 2015 at 14:18:24 UTC, Adam D. Ruppe wrote:
 On Friday, 28 August 2015 at 14:07:37 UTC, wobbles wrote:
 However, it just returns an empty string.
from the doc: Note that this always returns the empty string. This is because time zones cannot be uniquely identified by the attributes given by the OS (such as the stdName and dstName), and neither Posix systems nor Windows systems provide an easy way to get the TZ Database name of the local time zone.
Stupid question. If it always returns an empty string, why is it even there?
Aug 28 2015
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Friday, 28 August 2015 at 17:59:06 UTC, WhatMeWorry wrote:
 Stupid question. If it always returns an empty string, why is 
 it even there?
It can return meaningful information in other subclasses; it is a method from the interface and is just blank in the LocalTime class. If you construct one of the other subclasses, you may provide a name which can be retrieved through it, or perhaps some future class might implement it too.
Aug 28 2015
parent reply Jonathan M Davis via Digitalmars-d-learn writes:
On Friday, August 28, 2015 18:04:16 Adam D. Ruppe via Digitalmars-d-learn wrote:
 On Friday, 28 August 2015 at 17:59:06 UTC, WhatMeWorry wrote:
 Stupid question. If it always returns an empty string, why is
 it even there?
It can return meaningful information in other subclasses; it is a method from the interface and is just blank in the LocalTime class. If you construct one of the other subclasses, you may provide a name which can be retrieved through it, or perhaps some future class might implement it too.
A prime example would be PosixTimeZone. It knows exactly what its name is. Ultimately though, the name field probably isn't very useful - particularly since I really need to deprecate the functions for converting between the TZ database names and Microsft's names, because it's system dependent, and they keep changing it, so there's no guarantee that the mapping is correct or even that the result exists on a given box (since that box could be out-of-date, or the D program may not have been updated). That being the case, the name property really just becomes applicable to PosixTimeZone. I _really_ wish that Microsoft would just use the TZ database like everyone else... - Jonathan M Davis
Aug 28 2015
parent reply "rumbu" <rumbu rumbu.ro> writes:
On Friday, 28 August 2015 at 23:03:16 UTC, Jonathan M Davis wrote:

 I _really_ wish that Microsoft would just use the TZ database 
 like everyone else...

 - Jonathan M Davis
Starting with Windows 8.1, it does, but only in Windows Runtime (so called modern/store apps).
Aug 28 2015
parent Jonathan M Davis via Digitalmars-d-learn writes:
On Saturday, August 29, 2015 05:25:33 rumbu via Digitalmars-d-learn wrote:
 On Friday, 28 August 2015 at 23:03:16 UTC, Jonathan M Davis wrote:

 I _really_ wish that Microsoft would just use the TZ database
 like everyone else...

 - Jonathan M Davis
Starting with Windows 8.1, it does, but only in Windows Runtime (so called modern/store apps).
Well, that's good news but ultimately pretty useless. If it's really going ot be of any use to applications in general, then the OS itself needs to be using the TZ database files, and that needs to be accessible via their normal C API, not just the WinRT stuff. But maybe this is a good sign for the future. - Jonathan M Davis
Aug 30 2015