-
Notifications
You must be signed in to change notification settings - Fork 665
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to convert timezone for format date string ? #764
Comments
#include "date/tz.h"
#include <chrono>
#include <iostream>
#include <sstream>
int
main()
{
using namespace date;
using namespace std;
using namespace std::chrono;
string input{"2022-12-13 10:32:06"};
cout << input << '\n';
istringstream in{std::move(input)};
in.exceptions(ios::failbit);
local_seconds ltp;
in >> parse("%F %T", ltp);
auto utp = locate_zone("Asia/Shanghai")->to_sys(ltp);
string output = format("%F %T", utp);
cout << output << '\n';
} In a nutshell, the input is This program outputs:
An alternative to: auto utp = locate_zone("Asia/Shanghai")->to_sys(ltp); is: auto utp = zoned_time{"Asia/Shanghai", ltp}.get_sys_time(); They are both equivalent. A key point to observe in this example is that the input is parsed into a In the event that the "other time zone" is not UTC, for example "America/New_York", then the use of local_seconds ltp;
in >> parse("%F %T", ltp);
auto Stp = zoned_time{"Asia/Shanghai", ltp};
auto Ntp = zoned_time{"America/New_York", Stp};
string output = format("%F %T", Ntp); Output:
One can construct one One could do this at the local_seconds ltp;
in >> parse("%F %T", ltp);
auto utp = locate_zone("Asia/Shanghai")->to_sys(ltp);
auto Ntp = locate_zone("America/New_York")->to_local(utp);
string output = format("%F %T", Ntp); |
The text was updated successfully, but these errors were encountered: