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

Monks, I'm getting an error using this code:
use Date::Manip; my $date2 = "$timestamp"; Date_Init("TZ=EST"); my $http_date = UnixDate(ParseDate("$date2 -0500"), "%a, %d %b %Y %H:% +M:%S %Z");
The error is: WARNING: the TZ Date::Manip config variable is deprecated and will be removed in March 2016. Please use the SetDate or ForceDate config variables instead. So I researched and replaced the third line with this:
$date->config("setdate","now,EST");
But that returns an error too. What am I missing?

Replies are listed 'Best First'.
Re: Help with date:manip
by beech (Parson) on May 10, 2016 at 06:33 UTC

      Date::Manip has both a functions, and an OO interface.

      (Ah, but I see you're referring to his use of $date->config(...) later in the writeup. Yes... it seems a little like just trying things hoping something will work.)

      Anyway, the Date_Init function does work if I use US/Eastern instead of EST. Maybe that will help. :)


      Dave

        You are correct. Just trying things until they work cause I'm just a hack.

        This worked: Date_Init('SetDate=now,US/Eastern');

        Thanks so much!

      Thanks for the tip, but I'm still getting this: ERROR: config_var invalid zone in SetDate: Use of uninitialized value $mod in concatenation (.) or string at /usr/local/lib/perl5/5.22/Date/Manip/TZ.pm line 180. Use of uninitialized value $mod in concatenation (.) or string at /usr/local/lib/perl5/5.22/Date/Manip/TZ.pm line 181. Use of uninitialized value $mod in concatenation (.) or string at /usr/local/lib/perl5/5.22/Date/Manip/TZ.pm line 182.

        invalid zone in SetDate

        US/Eastern

        EST is an abbreviation, and as the error message states, isn't valid.


        Dave

        Thanks for the tip, but I'm still getting this: ERROR...

        :)

        Where is the code that generates that message? Are you trying to run Help with date:manip ? I get some errors with that code also :)

        update: ++ davido again, i had an older version (6.45) which didn't complain, Date::Manip 6.53 definitely complains about "EST" being invalid, no complaints about EST5EDT or America/New_York

Re: Help with date:manip
by SBECK (Chaplain) on May 10, 2016 at 13:00 UTC
    A few notes:

    First off, I see that I definitely need to improve my manual. It's not obvious, if you go right to the https://metacpan.org/pod/Date::Manip::Config document that you can set config variables in two ways. Using the form:

        $date->config("VAR","VAL");
    
    is for the OO interface.  Using the call:
    
        Date_Init("VAR=VAL");
    
    is the functional method (and is described in the https://metacpan.org/pod/Date::Manip::DM6 document. I will clarify the Config document to mention both in order to avoid this confusion. Sorry for that.

    Second, the TZ variable is deprecated, as you note, and will be removed the next release I believe, so you definitely want to use the SetDate config variable.

    Third, you did find a bug because "EST" should have returned a timezone (possibly not the one you want) since it is a valid abbreviation, and abbreviations should be parsed as timezones too. I'm in the process of correcting that bug and it'll be fixed in the next release. To be clear, the call:

       Date_Init("SetDate=now,EST");
    
    should have worked (though it would not have produced the correct timezone as described in my next point).

    Finally, using EST as a timezone (as was suggested by others) can lead to unpredictable results. For example, I'm in the America/New_York timezone, and if I set TZ=EST (or the equivalent SetDate=now,EST), it sets the timezone to be the America/Panama timezone, because it returns a timezone for which the abbreviation is currently EST. Since the abbreviation is currently EDT in the America/New_York timezone, America/New_York will not be used. As others have suggested, you really want to set the timezone unambiguously using a full timezone name. If you were to use SetDate=DATE,EST it would choose a timezone where EST was the abbreviation on DATE.

Re: Help with date:manip
by FreeBeerReekingMonk (Deacon) on May 10, 2016 at 20:57 UTC
    Just posting to note that you can get a list of timezones in Unix like this:
    perl -MDateTime::TimeZone -e 'print join "\n", DateTime::TimeZone->all +_names()'

    (you might need to install the Perl Module with: cpan DateTime::TimeZone)