HowardHinnant / date

A date and time library based on the C++11/14/17 <chrono> header
Other
3.13k stars 673 forks source link

Find timezone difference in the past #554

Open bambuk6 opened 4 years ago

bambuk6 commented 4 years ago

How do I find the difference (say, in minutes) between some timezone and UTC in the past? Will the library respect the timezone rules that were active at that date in the past?

HowardHinnant commented 4 years ago

It depends on the database, but generally yes. Linux typically installs the full IANA database which has rules dating back to the mid 1800's.

HowardHinnant commented 4 years ago
#include "date/tz.h"
#include <iostream>

int
main()
{
    using namespace std;
    using namespace std::chrono;
    using namespace date;

    auto time = sys_days{1834_y/February/27};
    auto la = zoned_time{"America/Los_Angeles", time};
    auto utc = zoned_time{"Etc/UTC", time};
    cout << format("%T\n", la.get_local_time() - utc.get_local_time());
}

Output:

-07:52:58

For pre-std times, the IANA database typically uses "mean solar time" for the city named by the time zone.