www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.datetime impenetrable

reply Steve Teale <steve.teale britseyeview.com> writes:
Looking at the documentation makes my head hurt, especially if I have consumed
some beer, when I am not pure and immutable.

Can anyone help me to understand how to determine what timezone the user has
selected to be his/hers.

Alternatively, let me explain my desire. When my program first runs, I want to
hazard a guess as to what size of paper the user is likely to use - US Letter
Size, or A4/inches or metric. GTK does not seem to want to tell me about the
default printer settings - could be the beer of course.

The timezone might allow me to bracket the USA, Canada, and Mexico though of
course then the poor sods in South America and the Philippines and so on would
be SOL.

Anyway, how would you do it?

Bzzzzzzzzzzzz Steve
Sep 15 2011
next sibling parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Thursday, September 15, 2011 10:48 Steve Teale wrote:
 Looking at the documentation makes my head hurt, especially if I have
 consumed some beer, when I am not pure and immutable.

For an overview, check out http://d-programming-language.org/intro-to- datetime.html I'd _love_ to fix the links on the top of the page, but ddoc needs some improvements with regards to how it generates anchor names for that to be possible (anchors are based solely on function or type names with no concept of scaping, so every function with the same name ends up with the same anchor regardless of whether it's a free function or what it's a member function of).
 Can anyone help me to understand how to determine what timezone the user
 has selected to be his/hers.
 
 Alternatively, let me explain my desire. When my program first runs, I want
 to hazard a guess as to what size of paper the user is likely to use - US
 Letter Size, or A4/inches or metric. GTK does not seem to want to tell me
 about the default printer settings - could be the beer of course.
 
 The timezone might allow me to bracket the USA, Canada, and Mexico though
 of course then the poor sods in South America and the Philippines and so
 on would be SOL.
 
 Anyway, how would you do it?

Determining the exact time zone that the computer is in is really hard. It's actually pretty easy to do in Windows, but on Posix... not so much. So, there's no function in std.datetime to do it. If I can ever figure out how to sanely do it on Posix, then I'll add it, but until then, it's not there. If all you want to do is bracket it, then you can just use the UTC offset for the time zone. Because the UTC offset varies depending on the time of year (or even from year to year), you can't just ask a TimeZone for its UTC offset, but I do have a pull request which makes it possible to ask what the UTC offset is at a particular std time or to ask a SysTime what its UTC offset is - but that hasn't been released yet. So, if you wanted to find out what the offset was right now in the current time zone, you would do auto sysTime = Clock.currTime(); auto utcOffset = dur!"hnsecs"(sysTime.timezone.utcToTZ(sysTime.stdTime) - sysTime.stdTime); So, for instance, America/Los_Angeles would give you an offset of -7 hours right now and -8 during the Winter. If you then wanted to narrow the hemisphere, you can do it in _some_ cases by checking whether DST is in effect in July: auto north = SysTime(Date(2011, 7, 1)).dstInEffect; but that will only work if the timezone has DST (and a large chunk of the Southern Hemisphere does not). If you were just trying to bracket USA, Canada, and Mexico though (all of which mostly have DST), it would mostly work to do something like auto jan = SysTime(Date(2011, 1, 11)); auto jul = SysTime(Date(2011, 7, 11)); auto utcOffset = dur!"hnsecs"(jan.timezone.utcToTZ(jan.stdTime) - jan.stdTime); auto inBracket = jul.dstInEffect && utcOffset >= dur!"hours"(-10) && utcOffset <= dur!"hours"(-3) + dur!"minutes"(-30); But that will fail for Hawaii or any other northern time zone with no DST, and it'll probably take in more time zones than you want it to above the equator, but it would _mostly_ work. But honestly, even if std.datetime _could_ tell you what the exact time zone was, that would still be a bit of a pain. You'd have to get the exact list of time zones that you cared about and compare that against the current time zone. It _would_ be mork accurate though. Maybe I'll figure out a reliable way to get the current time zone on Posix at some point, but unfortunately, it's not something that Posix is set up to do very easily (it's time zone handling is _much_ more accurate than Windows though, so neither OS really does it entirely right). Of course, what you really want to do if you can't just ask what the default printer settings are is to do it based on locale, but I don't know how straightforward that is. It might just be easier to use the hack that you're trying to do. And the code that I gave above should do that for you. - Jonathan M Davis
Sep 15 2011
parent Steve Teale <steve.teale britseyeview.com> writes:
Oh, so it is difficult after all!  I thought it was just me.

So I am probably going to have to ask. Then the interesting question will be
which
way around will offend fewest people.

I have installed it as ISO, then have to ask US users if they would prefer
Letter
Size, or the other way round then all those Euro and Indian users say 'typical
American programmer!'.

Thanks for the rundown anyway.

Steve
Sep 15 2011
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Thursday, September 15, 2011 19:30:24 Steve Teale wrote:
 Oh, so it is difficult after all!  I thought it was just me.

No. It's hard. The best that you can get by asking Posix functions is the std and DST abbreviations for the current time zone - and those are non-unique. You'd have to do some crazy stuff like trying to find the time zone file for the current time zone on disk (whose location is usually something like /etc/localtime or /etc/timezone but is not entirely standard) and then compare it with every other time zone file to figure out which one it is. It's ugly. I do hope to come up with a reasonable way to do it at some point (you _should_ be able to do it after all), but I haven't yet. It would require a fair bit of research to figure out what's standard enough to rely on and what isn't. So, yeah. It's one of the many reasons that dealing with time sucks. Though hopefully std.datetime manages to make most of it fairly easy and pleasant to deal with.
 So I am probably going to have to ask. Then the interesting question will be
 which way around will offend fewest people.
 
 I have installed it as ISO, then have to ask US users if they would prefer
 Letter Size, or the other way round then all those Euro and Indian users
 say 'typical American programmer!'.
 
 Thanks for the rundown anyway.

No problem. std.datetime is actually pretty easy to use, but it's large enough that it can be a bit overwhelming. - Jonathan M Davis
Sep 15 2011
prev sibling next sibling parent Mike Wey <mike-wey example.com> writes:
On 09/15/2011 07:48 PM, Steve Teale wrote:
 Alternatively, let me explain my desire. When my program first runs, I want to
 hazard a guess as to what size of paper the user is likely to use - US Letter
 Size, or A4/inches or metric. GTK does not seem to want to tell me about the
 default printer settings - could be the beer of course.

To get the default paper size with gtkD you could use: new PaperSize(cast(string)null); This creates an PaperSize object representing an US Letter if the locale is set to Canada, US or Mexico, and A4 otherwise. -- Mike Wey
Sep 15 2011
prev sibling next sibling parent Kagamin <spam here.lot> writes:
Steve Teale Wrote:

 Alternatively, let me explain my desire. When my program first runs, I want to
 hazard a guess as to what size of paper the user is likely to use - US Letter
 Size, or A4/inches or metric.

Do you wanna say all pdfs from america are done in letter size and won't print right on A4?
Sep 16 2011
prev sibling parent Kagamin <spam here.lot> writes:
Steve Teale Wrote:

 Anyway, how would you do it?

You need a compatibility layer like http://en.wikipedia.org/wiki/Freedesktop.org They already can have solution or you can consult with them.
Sep 16 2011