locked
Get current time zone from WinRT

    Question

  • The Windows::Globalization::Calendar class /seems/ to be missing any functionality for finding out what time zone the time is for. It is not UTC.

    I notice that .NET 4 and JavaScript offer methods to do this.

    Does anyone know if there is a WinRT way of doing this, or do I have to resort to something else... ? (e.g. Win32 API calls)

    Thanks in advance.

    Tuesday, May 15, 2012 4:35 PM

Answers

All replies

  • There is the Win32 GetTimeZoneInformation function, which works, but seems against the spirit of Metro/WinRT to rely on Win32 API...
    Tuesday, May 15, 2012 4:49 PM
  •  

    Hello,

     

    In metro, we can use a part of Win32 API. We can use these Time APIs in metro.

     

    EnumDynamicTimeZoneInformation

    FileTimeToSystemTime

    GetDynamicTimeZoneInformation

    GetDynamicTimeZoneInformationEffectiveYears

    GetLocalTime

    GetSystemTime

    GetSystemTimeAsFileTime

    GetTimeZoneInformation

    GetTimeZoneInformationForYear

    SystemTimeToFileTime

    SystemTimeToTzSpecificLocalTime

    TzSpecificLocalTimeToSystemTime

     

    Please check this document

    http://msdn.microsoft.com/en-us/library/windows/desktop/br205762.aspx

    http://msdn.microsoft.com/en-us/library/windows/desktop/br205757.aspx

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Semi Essessi Wednesday, May 16, 2012 6:00 PM
    Wednesday, May 16, 2012 7:20 AM
  • Thanks.
    Wednesday, May 16, 2012 6:00 PM
  • Is this compatible with winRT?
    Friday, October 12, 2012 5:36 AM
  • Hi,

    I've created a Windows Runtime library that wraps the allowed Win32 functionality and put it on Nuget and GitHub:

    https://nuget.org/packages/WinRTTimeZones

    The source is here: https://github.com/onovotny/WinRTTimeZones

    Regards,
    Oren

    • Proposed as answer by Oren NovotnyMVP Wednesday, October 17, 2012 2:26 AM
    Wednesday, October 17, 2012 2:26 AM
  • In the restricted case where you are just interested in the current system's time zone, there's a simple method, here part of a function that returns the likely timezone abbreviation, including daylight-savings versus standard time.

    There are no international standards for timezone abbreviations.  The usual recommended hack (at least for US timezones) is to take the first letter of each word as done below.   This will not work 100% for time zones worldwide.  Results are typically 3 letter (e.g., always if US) or 4 letter.  For a somewhat full list of actual abbreviations, see www.timeanddate.com/library/abbreviations/timezones/

    Since Win8 does not support Win7's System.TimeZones, we use TimeZoneInfo instead.  MSDN says TimeZone.CurrentTimeZone in Win 7 = TimeZoneInfo.Local .

    public string GetTimeZoneAbbreviation() {
      string tzname;
      if(TimeZoneInfo.Local.IsDaylightSavingTime(DateTime.Now))
        tzname = TimeZoneInfo.Local.DaylightName;
      else
        tzname = TimeZoneInfo.Local.StandardName;
    
      string[] words = tzname.Split(" ".ToCharArray());
      string tzabbr = "";
      foreach (string word in words)
        tzabbr += word[0];
      return tzabbr;
    }

    (Not used here but also of interest, as other correspondents have noted:  The WinRTTimeZones code, available from NuGet, has a TimeZones objects that wraps Win32 time features supported in Win 8.  See also "Programming Windows, Version 6", Petzold, Chapter 15.)


    • Edited by glennp_ Friday, May 31, 2013 10:29 PM typo in code
    Friday, May 31, 2013 8:26 PM