Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get timezone abbreviation under Windows

We are trying to get the timezone from std::tm with strftime:

char timezone[50];
strftime(timezone, sizeof(timezone), "%Z", &timeCreated);

On iOS, we get "EST" which is what we want. But on Windows, we get "Eastern Summer Time". Anybody know how to consistently get the current timezone in C++ in abbreviation form?

I consider making the abbreviation from the full name of the timezone by simply picking out the first character in each word. But I check the list of abbreviations and notice that we could have timezone like this one "Chuuk Time" and abbreviated as "CHUT". Which makes manually adjusting not possible.


Not the same as Question: Windows Timezone and their abbreviations? I don't need a full list of all timezones and abbreviations. But instead, I need a systematic way to the current timezone using for example strftime. I want them to use the system's current timezone and the the current local.

like image 975
Yuchen Avatar asked Oct 30 '22 05:10

Yuchen


1 Answers

Using this free, open-source library that has been ported to VS-2013 and later:

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

int
main()
{
    using namespace std::chrono;
    using namespace date;
    std::cout << format("%Z\n", make_zoned(current_zone(), system_clock::now()));
}

This just output for me:

EDT

Fully documented.

This library uses the IANA timezone database, and when current_zone() is called, translates the current Windows timezone into the appropriate IANA timezone.

like image 122
Howard Hinnant Avatar answered Nov 04 '22 11:11

Howard Hinnant