CppCon 2015: Greg Miller “Time Programming Fundamentals"
Why Stack Overflow recommends it
Stack Overflow users link it when answering time zone and daylight saving questions, since it explains the concepts behind cctz and why code should avoid raw UTC offsets.
Recommended by 2 people across 3 different months, 2011 to 2015.
What it is
A 2015 CppCon talk by Google engineer Greg Miller on why date and time programming goes wrong and how a simplified mental model fixes it. He traces how time handling evolved, presents the model, and demonstrates it with the open source cctz C++ library, ending with practical time hygiene tips.
What people said
Recommends Nobody moving away from SE · 2011-08 · Stack Overflow · on “Rounding PHP timestamp”
Update After seeing Greg Miller's "Time Programming Fundamentals" talk [this video] about Google's cctz C++ library [LINK] , I am dissatisfied with my previous answer. Handling time is a very complex topic (even when discounting relativistic effects). We have to honor things like timezones with uneven offsets [LINK] , daylight saving time [LINK] , time jumps due to legislation changes [LINK] or leap seconds [LINK] and probably many more quirks. While many of these have only a local effect in time and space, users of our programs can still be unlucky enough to get affected. TL;DR It is probably best to rely on underlying functions like in holodoc's answer [LINK] to handle the quirks of timezone ma
Recommends Greg Miller · 2015-10 · Stack Overflow · on “How to get diffreence between GMT time and any time zone in C++”
You can use the cctz [LINK] library to calculate the difference in UTC offset between two different time zones at some specific time. #include <chrono> #include "cctz.h" using namespace std::chrono; cctz::TimeZone nyc; cctz::LoadTimeZone("America/New_York", &nyc); cctz::TimeZone anc; cctz::LoadTimeZone("America/Anchorage", &anc); const auto now = system_clock::now(); const auto nyc_off = cctz::BreakTime(now, nyc).offset; const auto anc_off = cctz::BreakTime(now, anc).offset; const auto off_diff = nyc_off - anc_off; Now, the truth is that you really don't want to do that. Really. Healthy, modern code should never (did I say never, because I meant never ) care about UTC offsets. Computing with
Recommends Greg Miller · 2015-11 · Stack Overflow · on “C/C++ time zone correct time conversion (to seconds since epoch)”
Here's an answer that does what you want using Google's [LINK] #include <chrono> #include <iostream> #include <string> #include "src/cctz.h" using namespace std; int main(int argc, char* argv[]) { const char kFmt[] = "%Y-%m-%dT%H:%M:%S%Ez"; // I expect both of these to transform to 1440671500. const char utc_example[] = "2015-08-27T11:31:40+0100"; const char tz_example[] = "2015-08-27T10:31:40Z"; cout << "1440671500 expected" << endl; // Required by cctz::Parse(). Only used if the formatted // time does not include offset info. const auto utc = cctz::UTCTimeZone(); std::chrono::system_clock::time_point tp; if (!Parse(kFmt, utc_example, utc, &tp)) return -1; cout << "utc example: " << std::ch
Cites Matt Johnson-Pint · 2013-03 · Stack Overflow · on “How to initialize a JavaScript Date to a particular time zone”
Background JavaScript's Date object tracks time in UTC internally, but typically accepts input and produces output in the local time of the computer it's running on. It has very few facilities for working with time in other time zones. The internal representation of a Date object is a single number - namely timestamp - representing the number of milliseconds that have elapsed since 1970-01-01 00:00:00 UTC , without regard to leap seconds. There is no time zone or string format stored in the Date object itself. When various functions of the Date object are used, the computer's local time zone is applied to the internal representation. If the function produces a string, then the computer's loc