1

This is similar to How to get the current time zone?

#include <ctime>
tzset();
time_zone_ptr zone(new posix_time_zone(tzname[localtime(0)->tm_isdst]));

The above code throws 'bad cast' exception. The time zone from tzname or GetTimeZoneInformation is a string like 'China Standard Time' which is not accepted by Boost time zone.

Is there an easy way to resolve such a basic problem?

user1633272
  • 2,007
  • 5
  • 25
  • 48

1 Answers1

1

Using Howard Hinnant's time zone library, the following will print out the current IANA time zone name on Windows:

#include "date/tz.h"
#include <iostream>

int
main()
{
    std::cout << date::current_zone()->name() << '\n';
}

Some installation is required on Windows. For me the above program outputs:

America/New_York

The implementation looks up the current Windows time zone using GetDynamicTimeZoneInformation, and then uses this Unicode mapping to map from the native Windows time zone name to an IANA time zone name.

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577