std::string time = std::format("{:%H:%M:%OS %z}", std::chrono::zoned_time{"America/New_York", now});
terminate called after throwing an instance of 'std::runtime_error'
what(): tzdb: cannot locate zone: America/New_York
Aborted
It returns a such error with any zone. I have this issue on Debian 12. On Deb 11 it worked fine.
1. All files of TZDB are there and readable - /usr/share/zoneinfo/.
2. I have a last version: "tzdata_2024b-0+deb12u1_all"(bookworm); downgrading to "2024b-0+deb11u1"(bullseye) doesn't help.
3. Neither g++ 13.3.0 nor g++ 14.2.0 can obtain the location.
I have searched around... Suggestions like to change "America/New_York" to "some code"(Ubuntu) or downgrading the tzdata(ArchLinux) didn't work for me.
Maybe someone knows how to locate zone? It looks like Debian, as usually, has its own painful way:))
The problem of "America/New_York" is that there are 2 timezones: "EST" and "EDT". The error probably means that the system is not able to figure out which one to use.
Maybe you can use "EST"/"EDT" instead. That'd mean it is up to you to figure out the right one.
I really don't want to change the local time. I have no reason to change time settings, it works fine and may be reconfigured any time if I need.
I want to let my program to represent the local time(or any time point) in time of a certain location - for ex. NY. As I see there: https://wiki.debian.org/TimeZoneChanges - Time Zone changes doesn't affect time representation by std::chrono::zoned_time. Time point representation is the reason why I'm using "zoned_time".
#include <chrono>
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
int main() {
// Set the TZ environment variable
setenv("TZ", "America/New_York", 1);
tzset(); // Update the timezone information
auto now = std::chrono::system_clock::now();
auto local_time = std::chrono::system_clock::to_time_t(now);
std::tm* tm_ptr = std::localtime(&local_time);
// Format the time manually
std::cout << "Current time in New York: "
<< std::put_time(tm_ptr, "%H:%M:%S %z") << std::endl;
return 0;
}