Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timestamp calculation with daylight saving time

The Central European Daylight Saving Time begins on last Sunday in March. We set our clocks from 02:00 to 03:00. What happens if I do timestamp calcuations in a database request - lets say at 01:59?

UPDATE sessions SET aliveuntil = (CURRENT_TIMESTAMP + INTERVAL '1' MINUTE) WHERE id = ?

Do I get 03:00 as result or 02:00?

And what about the over way around if we set our clocks from 03:00 to 02:00?

SELECT id FROM sessions WHERE aliveuntil < (CURRENT_TIMESTAMP - INTERVAL '1' MINUTE)

After time changed from 03:00 to 02:00... what happens with the (CURRENT_TIMESTAMP - INTERVAL '1' MINUTE) at 02:00? Is it 02:59 or 01:59?

How should this be handled? Best practice and how it's handle (in my particular case) by Oracle Database 11g Release 11.2.0.2.0?

like image 653
alexvetter Avatar asked Jan 04 '13 13:01

alexvetter


People also ask

What is the formula for Daylight Savings Time?

mathematics of associating day of the week with the day of the month. localized DST definitions or future definition changes. equations. USA prior to 2007 began DST on the 1st Sunday in April (Day of month = 1 + (2+6*Y-Y/4) mod 7) and ended it on the last Sunday of October (Day of month = 31- (1 + 5*Y/4) mod 7).

Does getTimezoneOffset account for daylight savings?

All time zones have an offset from GMT (Greenwich Mean Time), from twelve hours behind to twelve hours ahead. The getTimezoneOffset( ) method returns this offset, in minutes, for the locale in which the Date object was created, taking Daylight Savings Time into account.

Do you add or subtract an hour for daylight savings?

Daylight Saving Time Today Today, most Americans spring forward (turn clocks ahead and lose an hour) on the second Sunday in March (at 2:00 A.M.) and fall back (turn clocks back and gain an hour) on the first Sunday in November (at 2:00 A.M.).

How does UTC handle daylight Savings?

The switch to daylight saving time does not affect UTC. It refers to time on the zero or Greenwich meridian, which is not adjusted to reflect changes either to or from Daylight Saving Time.


1 Answers

If I'm understanding their documentation correctly, it depends on how the table/columns are setup in the database. If the columns are setup to use WITH TIME ZONE, then Oracle automatically determines the correct/relevant values. In your above example if the column aliveuntil has this setting, then if you try to add 1 minute at 1:59, the time will update to 3:00.

Here is a helpful article I found on the subject:

http://docs.oracle.com/cd/B19306_01/server.102/b14225/ch4datetime.htm

Scroll down to the bottom of the article and you should see what you're looking for.

Here was a section of the article I found relevant:

For example, in the Eastern region of the United States, the time changes from 01:59:59 a.m. to 3:00:00 a.m. when Daylight Saving Time goes into effect. The interval between 02:00:00 and 02:59:59 a.m. does not exist. Values in that interval are invalid.

like image 144
sgeddes Avatar answered Oct 04 '22 06:10

sgeddes