AlfaProject has asked for the wisdom of the Perl Monks concerning the following question:

Hi !
I have a problem with timestamps that I save to database.
For example i want to get dates from 1 timestamp to another.
and it goes through the day when time changes to summer time so i add to timestamp 2 hours.
Is it right way to do it ?
Maybe i need to save it in some 'general' format ?
Thanks

Replies are listed 'Best First'.
Re: timestamp and summer time
by GrandFather (Saint) on Jan 09, 2012 at 22:13 UTC

    Use UT and a ISO 8601 date/time format for your stored time stamp. That allows easy searching, sorting and general manipulation based on time. You probably then need to use Date::Manip or some other date/time module to present dates and times in the users locale, but that's generally much easier than trying to manage localised time in your database!

    True laziness is hard work
Re: timestamp and summer time
by mbethke (Hermit) on Jan 09, 2012 at 21:59 UTC

    Timestamps are fine if you don't want to treat them as such in SQL, I'm also using them for lots of stuff where speed is of essence. But don't do date calculations on timestamps yourself, use Date::Calc and forget about all the gory details.

Re: timestamp and summer time
by JavaFan (Canon) on Jan 09, 2012 at 22:51 UTC
    Either store your timestamps in UTC (which is what your database should do when storing timestamps), or store datetimes which include the timezone, and dst information.
Re: timestamp and summer time
by Marshall (Canon) on Jan 10, 2012 at 00:10 UTC
    Maybe i need to save it in some 'general' format ?

    The "general way" is to use UTC, which is very close to what is called: Greenwich Mean Time (GMT). There is no practical difference between the two.

    Use UTC for date/time storage and convert that when necessary for user presentation. UTC marches onward and never goes "backward".

    In local time, a day may have just 23 hours or may have 25 hours. There can be even wild things like Samona which recently just "deleted one day" (Friday, Dec 30, 2011) when they decided to be on a different side of the "date-line" - a political decision. Samoa Date Change

    When converting from UTC to local time there can be some ambiguities. Think about this 2 AM deal in the US when we either "spring forward" or "fall back". In some locales like mine, there is a special law that doesn't allow the bars to be open one hour later on that special day when the clocks get set back one hour at 2 am (closing time is normally 2am). But the UTC time marches onward, regardless of the local time.

Re: timestamp and summer time
by AlfaProject (Beadle) on Jan 10, 2012 at 12:13 UTC
    Thanks ,
    I have moved from 'localtime' to 'gmtime' perl function.