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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With