Best practices with saving datetime & timezone info in database when data is dependant on datetime

Hugo’s answer is mostly correct, but I’ll add a few key points: When you’re storing the customer’s time zone, do NOT store a numerical offset. As others have pointed out, the offset from UTC is only for a single point in time, and can easily change for DST and for other reasons. Instead, you should … Read more

.NET DateTime.Now returns incorrect time when time zone is changed

Yes, the current time zone is cached. For a good reason, it avoids trouble with broken code that uses DateTime.Now to implement elapsed time measurement. Such code tends to suffer a heart-attack when the time suddenly changes by an hour or more. You will have to call System.Globalization.CultureInfo.ClearCachedData() to reset the cached value. The next … Read more

PHP daylight saving time detection

As Jimmy points out you can use timezone transitions, but this is not available on PHP <5.3. as dateTimeZone() is PHP>=5.2.2 but getTransitions() with arguments is not! In that case here is a function that can give you timezone data, including whether in DST or not. function timezonez($timezone=”Europe/London”){ $tz = new DateTimeZone($timezone); $transitions = $tz->getTransitions(); … Read more

Does UTC observe daylight saving time?

No, UTC itself never has DST. It is the constant frame of reference other time zones are expressed relative to. From the Wikipedia UTC page: UTC does not change with a change of seasons, but local time or civil time may change if a time zone jurisdiction observes daylight saving time or summer time. For … Read more

Parsing of Ordered Timestamps in Local Time (to UTC) While Observing Daylight Saving Time

In C#: // Define the input values. string[] input = { “2013-11-03 00:45:00”, “2013-11-03 01:00:00”, “2013-11-03 01:15:00”, “2013-11-03 01:30:00”, “2013-11-03 01:45:00”, “2013-11-03 01:00:00”, “2013-11-03 01:15:00”, “2013-11-03 01:30:00”, “2013-11-03 01:45:00”, “2013-11-03 02:00:00”, }; // Get the time zone the input is meant to be interpreted in. TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById(“Eastern Standard Time”); // Create an array … Read more

Python daylight savings time

You can use time.localtime and look at the tm_isdst flag in the return value. >>> import time >>> time.localtime() (2010, 5, 21, 21, 48, 51, 4, 141, 0) >>> _.tm_isdst 0 Using time.localtime(), you can ask the same question for any arbitrary time to see whether DST would be (or was) in effect for your … Read more

MySQL datetime fields and daylight savings time — how do I reference the “extra” hour?

I’ve got it figured out for my purposes. I’ll summarize what I learned (sorry, these notes are verbose; they’re as much for my future referral as anything else). Contrary to what I said in one of my previous comments, DATETIME and TIMESTAMP fields do behave differently. TIMESTAMP fields (as the docs indicate) take whatever you … Read more