C++ and Timezones

Have you ever had to work with date / times in c / c++ and had to deal with that F###ing Daylight Savings Time? Even more, have you spent hours, days, weeks trying to force everything to UTC / GMT ?

It took me a while, but I figured it out. Include this in your main() and it seems to do the trick:

#ifdef _WIN32
if (_putenv((char *) “TZ=GMT”))
if (putenv((char *) “TZ=GMT”))
wxMessageBox(“Failed setting env for date/time”);  // Somehow notify that the update failed.

I have used this on Win32 (XP) and RedHat derivatives. I can’t guarantee support elsewhere, but I believe this should work on all POSIX systems.

This entry was posted in Linux Tricks. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.