3

I'm wondering what the easiest method would be to reconcile timezones between Windows and OSx? I've tried listing out all the time zones windows could support and trying to cross reference a list of what OSx could support. I'm going to be given an abbreviation (3 letters) for the timezone and I'll need to set the timezone accordingly. Is there an easy way to do this?

Justin T. Watts
  • 547
  • 1
  • 4
  • 16
  • Don't use the 3 letter codes. – Sotirios Delimanolis May 23 '14 at 04:19
  • what do you suggest? I need some kind of standard both operating systems will understand. – Justin T. Watts May 23 '14 at 04:20
  • 1
    Use tz database names http://en.wikipedia.org/wiki/List_of_tz_database_time_zones – Sotirios Delimanolis May 23 '14 at 04:24
  • Look at the PHP supported timezone list [here](https://php.net/manual/en/timezones.php) - use that as a basis. The three-letter codes are far from unique. I can think of 3 places that use CST to mean different things, two of which are the __same__ place at different times of the year! –  May 23 '14 at 04:25
  • Keep in mind the same java vm version should give you consistent timezone support regardless of OS – gerrytan May 23 '14 at 05:11
  • In the end I'd like to have a way to set the timezone on both operating systems. You guys have given a list for tz, but Windows doesn't understand tz. @gerrytan what support in Java can I use to set the timezones regardless of OS? – Justin T. Watts May 23 '14 at 05:15
  • @JustinT.Watts java.util.Date stores time as UTC milliseconds offset since epoch. You can then print it in any timezone listed on tz database using SimpleDateFormat. See: http://gerrydevstory.com/2013/07/17/date-time-and-timezone-in-java/ – gerrytan May 23 '14 at 05:17
  • I need to actually set the timezone of the OS. – Justin T. Watts May 23 '14 at 05:18
  • Ah.. then that's a tricky one. You need to find a way to interface with corresponding OS API. I never had to write a program like this. Could you possibly have fallen into an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)? – gerrytan May 23 '14 at 05:22
  • that's exactly what I'm thinking @gerrytan if there is a different code I can use to refer for each OS as in using the full timezone name to reference the tz name. But then those are not standard too. I also thought I could use an offset to then find a compatible timezone. – Justin T. Watts May 23 '14 at 05:27

1 Answers1

3

Forget about using 3-letter abbreviations - they're not unique. For example, the humble "CST" could be any of:

  • UTC-06:00 Central Standard Time (North America)
  • UTC-05:00 Cuba Standard Time
  • UTC+08:00 China Standard Time
  • UTC+09:30 Central Standard Time (Australia)
  • UTC+10:30 Central Summer Time (Australia)

Instead, use the standard IANA time zone identifiers, such as America/Los_Angeles or Europe/London.

On Windows, you will need to map these to an appropriate Microsoft time zone id. For example, America/Los_Angeles == "Pacific Standard Time". Don't let the names fool you though. Despite the word "Standard" in the identifier, that covers the entire Pacific time zone, including both PST and PDT.

In general, you can't trust the Microsoft identifiers to mean anything in particular. For instance, should you use "Arab Standard Time", "Arabic Standard Time", or "Arabian Standard Time"? These are three distinct zones with different meanings and similar names. There are plenty of other examples.

Instead of guessing, the definitive reference for translating between Microsoft and IANA zones is the Unicode CLDR. It contains (among other things), a well-researched translation matrix between the different time zones types.

Note that there are many more IANA time zones than there are Microsoft time zones. There are also some IANA zones that have no Microsoft equivalent. Converting from IANA to Microsoft, there is (usually) a single choice of identifier. But converting in the other direction, from Microsoft to IANA, you may have more than one possibility. You may need to use other information, such as the country code, to determine the appropriate mapping.

If you're doing any of this in .Net, you can use Noda Time, and the code in this answer, to perform the translation. The CLDR translation table is included in Noda Time.

If you're doing this in Java, you can simply use the IANA identifiers, as the JRE also includes the CLDR translation table and will map the local Windows time zone to the appropriate IANA time zone.

You can also find the CLDR in other projects, such as ICU4C and ICU4J.

Also, in comments, you wrote:

I need to actually set the timezone of the OS

You're not going to be able to do that easily from Java. In Windows, it would require a call to several Win32 native functions, and you need the appropriate permissions. In other OS's I'm sure the calls are completely different, but there will still be permissions issues.

In general, allowing any single application to change the operating system's time zone is a dangerous idea. Imagine if multiple applications started fighting for control over the time zone. The time zone affects everything on the system. This would be very bad.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Yes I ended up making a mapping of the windows "standard" to the tz standard for unix and am having the timezone sent to me be the full name. I'm actually able to change the timezone via terminal command and command prompt on both OSes. I wanted to see if there was a standard api or command that uses the same standard on both OSes before I put together my own solution. – Justin T. Watts May 25 '14 at 02:37
  • Use the cldr. Don't try to figure out the mapping yourself. It's harder than it looks. – Matt Johnson-Pint May 25 '14 at 05:42