将另一个时区的 tm 转换为 GMT 时区的 tm
Convert tm of another timezone into tm of the GMT timezone
我正在使用 chrono 和 c++20。
我有一个 EST 时区的 tm
结构,但不知道如何获得 GMT 时区的相应时间。
这是我一直在想的:
tm timeInAmerica = {0};
timeInAmerica.tm_year = 75;//1975
timeInAmerica.tm_month = 0;//January
timeInAmerica.tm_mday = 31;
timeInAmerica.tm_hour = 23;
timeInAmerica.tm_minute = 23;
timeInAmerica.tm_second = 23;
auto timeZone = std::chrono::locate_zone("America/New_York");
auto sysTime = timeZone->to_sys( /*needs local_time */ );
...我不知道如何将 tm
转换为 local_time
以便我可以将其输入 to_sys()
.
我也不知道如何将返回的 sysTime
值转换回 tm
(这将允许我检查 GMT 年、月、日、小时、分钟)。
using namespace std::chrono;
tm timeInAmerica = {0};
timeInAmerica.tm_year = 75;//1975
timeInAmerica.tm_mon = 0;//January
timeInAmerica.tm_mday = 31;
timeInAmerica.tm_hour = 23;
timeInAmerica.tm_min = 23;
timeInAmerica.tm_sec = 23;
auto lt = local_days{year{timeInAmerica.tm_year+1900}
/month(timeInAmerica.tm_mon+1)
/timeInAmerica.tm_mday}
+ hours{timeInAmerica.tm_hour}
+ minutes{timeInAmerica.tm_min}
+ seconds{timeInAmerica.tm_sec};
auto timeZone = locate_zone("America/New_York");
auto sysTime = timeZone->to_sys(lt);
auto sysDay = floor<days>(sysTime);
year_month_day ymd = sysDay;
hh_mm_ss hms{sysTime - sysDay};
int y = int{ymd.year()};
int m = unsigned{ymd.month()};
int d = unsigned{ymd.day()};
int h = hms.hours().count();
int M = hms.minutes().count();
int s = hms.seconds().count();
我发布了 using namespace std::chrono
只是为了将冗长的内容降低到低吼。如果您希望将 std::chrono::
放在所有正确的位置,那也很好。
lt
是表示当地时间所需的 local_time<seconds>
(或只是 local_seconds
)。从 tm
.
转换时请注意偏差(1900 和 1)
要将 sysTime
转换回 {year, month, day, hour, minute, second}
结构,首先将 sysTime
截断为天精度 time_point
。那么days-precision time_point
可以转换为{year, month, day}
数据结构。
一天中的时间就是 date_time 减去日期。这可以转换成一个{hours, minutes, seconds}
数据结构:hh_mm_ss
.
year_month_day
和 hh_mm_ss
都有获取强类型字段的 getter。然后每个强类型字段都转换为整数,如上所示。转换回 tm
时,不要忘记偏差(1900 和 1)。
此外,一切都有流媒体运营商。这样调试起来非常方便:
cout << "lt = " << lt << '\n'; // 1975-01-31 23:23:23
cout << "sysTime = " << sysTime << '\n'; // 1975-02-01 04:23:23
cout << "sysDay = " << sysDay << '\n'; // 1975-02-01
cout << "ymd = " << ymd << '\n'; // 1975-02-01
cout << "hms = " << hms << '\n'; // 04:23:23
我正在使用 chrono 和 c++20。
我有一个 EST 时区的 tm
结构,但不知道如何获得 GMT 时区的相应时间。
这是我一直在想的:
tm timeInAmerica = {0};
timeInAmerica.tm_year = 75;//1975
timeInAmerica.tm_month = 0;//January
timeInAmerica.tm_mday = 31;
timeInAmerica.tm_hour = 23;
timeInAmerica.tm_minute = 23;
timeInAmerica.tm_second = 23;
auto timeZone = std::chrono::locate_zone("America/New_York");
auto sysTime = timeZone->to_sys( /*needs local_time */ );
...我不知道如何将 tm
转换为 local_time
以便我可以将其输入 to_sys()
.
我也不知道如何将返回的 sysTime
值转换回 tm
(这将允许我检查 GMT 年、月、日、小时、分钟)。
using namespace std::chrono;
tm timeInAmerica = {0};
timeInAmerica.tm_year = 75;//1975
timeInAmerica.tm_mon = 0;//January
timeInAmerica.tm_mday = 31;
timeInAmerica.tm_hour = 23;
timeInAmerica.tm_min = 23;
timeInAmerica.tm_sec = 23;
auto lt = local_days{year{timeInAmerica.tm_year+1900}
/month(timeInAmerica.tm_mon+1)
/timeInAmerica.tm_mday}
+ hours{timeInAmerica.tm_hour}
+ minutes{timeInAmerica.tm_min}
+ seconds{timeInAmerica.tm_sec};
auto timeZone = locate_zone("America/New_York");
auto sysTime = timeZone->to_sys(lt);
auto sysDay = floor<days>(sysTime);
year_month_day ymd = sysDay;
hh_mm_ss hms{sysTime - sysDay};
int y = int{ymd.year()};
int m = unsigned{ymd.month()};
int d = unsigned{ymd.day()};
int h = hms.hours().count();
int M = hms.minutes().count();
int s = hms.seconds().count();
我发布了 using namespace std::chrono
只是为了将冗长的内容降低到低吼。如果您希望将 std::chrono::
放在所有正确的位置,那也很好。
lt
是表示当地时间所需的 local_time<seconds>
(或只是 local_seconds
)。从 tm
.
要将 sysTime
转换回 {year, month, day, hour, minute, second}
结构,首先将 sysTime
截断为天精度 time_point
。那么days-precision time_point
可以转换为{year, month, day}
数据结构。
一天中的时间就是 date_time 减去日期。这可以转换成一个{hours, minutes, seconds}
数据结构:hh_mm_ss
.
year_month_day
和 hh_mm_ss
都有获取强类型字段的 getter。然后每个强类型字段都转换为整数,如上所示。转换回 tm
时,不要忘记偏差(1900 和 1)。
此外,一切都有流媒体运营商。这样调试起来非常方便:
cout << "lt = " << lt << '\n'; // 1975-01-31 23:23:23
cout << "sysTime = " << sysTime << '\n'; // 1975-02-01 04:23:23
cout << "sysDay = " << sysDay << '\n'; // 1975-02-01
cout << "ymd = " << ymd << '\n'; // 1975-02-01
cout << "hms = " << hms << '\n'; // 04:23:23