in reply to setting TZ causes Date::Manip to report incorrect time

As you have discovered, there are various problems with Time Zones and Date::Manip. If I were you, I'd take a look at the DateTime project. Using DateTime means you can get rid of that clutter of date modules that was previously needed to work with dates in Perl. The DateTime modules know all about Time Zones, including when local governing bodies changed their moves in and out of daylight savings. DateTime also knows about leap seconds and so carries accuracy over long periods very well.

There are a library of DateTime modules now available that can help you with anything related to dates and times, including working with httpd logs, with databases and even with using your own date formats. DateTime offers both strftime and strptime, so you're never limited by existing format modules.

DateTime also does a good job of durations and never assumes there's 24 * 60 * 60 seconds in a day. A fault of most (if not all) previously existing date and time modules.

However, like Date::Manip, DateTime needs to find your local time zone from somewhere if you want to talk about 'local' dates and times. But if your machine doesn't already have the information DateTime needs, there's no need to worry. If you know where you are (and most of us do!) you should be setting it anyways.

If timezones don't matter, then there's always the 'floating' timezone. This zone just 'floats' your datetime until you allocate a time zone to it. Huh? Well, if you make a datetime for 5pm on Sept 9, 2003 but don't specify a timezone then it's 'floating'. One you $dt->set_timezone('SomeZone') then it becomes 5pm on Sept 9, 2003 in SomeZone. It doesn't change the time, just the zone its in. Now if you were to change from SomeZone to UTC (similar to GMT), it becomes some other time, depending on the offset of SomeZone to UTC.

All in all, the DateTime modules solve pretty much any problem to do with dates and times in perl. If you have a problem that it doesn't solve then you're in luck: the development group is very active and will listen to anything you need.

At this point I should point out that I am one of the lowly developers in the project, so I have a certain bias. Despite this, and thinking as objectivly as I can, I still think DateTime is the best solution for most date and time problems in perl.

You can contact the mailing list at datetime@perl.org, and can download the modules from CPAN.

Replies are listed 'Best First'.
Re: Re: setting TZ causes Date::Manip to report incorrect time
by meonkeys (Chaplain) on Sep 09, 2003 at 19:14 UTC

    I appreciate the helpful reply with information about the DateTime project. However, it doesn't seem to answer questions like:

    • What are valid values for the TZ environment variable?
    • Why did Date::Manip behave as I described?

    I feel like you ignored my original post. :(

    the DateTime modules solve pretty much any problem to do with dates and times in perl

    Good to know. What about parsing the date 'today'? How is that affected by time zones? Which of the plethora of DateTime modules should I use to parse 'today' and format it as 'Tue Sep 9 12:12:12 PDT 2003'?

    ---
    "A Jedi uses the Force for knowledge and defense, never for attack."

      Valid values for the TZ variable when using DateTime are anything that matches a valid Olson time zone name, like "America/Chicago". When using Date::Manip they're whatever the Date::Manip docs say are valid. See the "TIMEZONES" section of the docs.

      Why did Date::Manip behave as you described? Presumably there's a bug in Date::Manip, or your expectations are wrong and Date::Manip can't do what you want.

      How often do you need to parse things like "today" and "two days after tomorrow" anyway? With DateTime, for just getting "today" you'd do DateTime->today(). To format it as above you'd do $dt->strftime('%a %b %e %H:%M:%S %Z %Y'). However, you're strongly discouraged from using short time zone names like "PDT" for anything but display, because they are not unique.

      I appreciate the helpful reply with information about the DateTime project. However, it doesn't seem to answer questions like:

      * What are valid values for the TZ environment variable?
      * Why did Date::Manip behave as I described?

      I feel like you ignored my original post. :(

      Sorry you feel that way, however I was trying to answer your question by offering a solution, rather than just straight answering your question.

      Your question was "Does anyone know what's going on with Date::Manip?" The answer to which is 'no', see below. As to the questions in your reply:

      * What are valid values for the TZ environment variable?
      This is not a perl question but one which should be addressed to your operating system. However if you're looking for valid values for Date::Manip, I'd check their POD or even their source. Remember however that if you specify PDT or -0700 (assuming Date::Manip accepts these values) then you will not get any daylight savings information and the number of hours between June 30 and Dec 31 will be a multiple of 24.

      * Why did Date::Manip behave as I described?
      It looks like there's either a bug in D:M or it's a feature you're not using properly. I'm guessing that it's a bug because I can't make sense of it as a feature.

        Sorry you feel that way, however I was trying to answer your question by offering a solution, rather than just straight answering your question.

        Fair enough. I've taken a look at the DateTime project and it looks great. How does it compare performance wise to other date manipulation modules like Date::Manip, Date::Parse/Date::Format, and Time::ParseDate?

        * What are valid values for the TZ environment variable? This is not a perl question but one which should be addressed to your operating system.

        Sure, the OS is in chare of the environment, but this is absolutely a Perl question since Date::Manip relies on the environment settings to know how it should parse and format dates.

        ---
        "A Jedi uses the Force for knowledge and defense, never for attack."