d-evil has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am getting the dates from a page and i am using the UnixDate subroutine to get the year, month and date in a specified format. But after the UnixDate processing, for one date i am getting a day before that. The following code shows the printing before and after the UnixDate subroutine

print Dumper $tot_start_date; print Dumper $tot_end_date; print Dumper $beta_start_date; print Dumper $beta_end_date; $tot_start_date = UnixDate($tot_start_date,"%Y-%m-%d"); $tot_end_date = UnixDate($tot_end_date,"%Y-%m-%d"); $beta_start_date = UnixDate($beta_start_date,"%Y-%m-%d"); $beta_end_date = UnixDate($beta_end_date,"%Y-%m-%d"); print Dumper $tot_start_date; print Dumper $tot_end_date; print Dumper $beta_start_date; print Dumper $beta_end_date; And here is the output: $VAR1 = '2008-06-27 17:37:11 +0000'; $VAR1 = '2008-09-29 04:02:18 +0000'; $VAR1 = '2008-09-29 04:02:18 +0000'; $VAR1 = '2009-01-07 00:00:00 -0800'; $VAR1 = '2008-06-27'; $VAR1 = '2008-09-28'; $VAR1 = '2008-09-28'; $VAR1 = '2009-01-07';
If you see the second and third date in the output, it is one day before to the actual date before the UnixDate processing. Any clue on this? is the time on the date making a difference? How to get the exact date. Atleast i dont want to use regular expression to extract the date. Thanks in advance,

Replies are listed 'Best First'.
Re: UnixDate - getting a different date
by roboticus (Chancellor) on Dec 23, 2008 at 19:45 UTC
    d-evel:

    I'd guess your locale. Part of your program is likely using UTC while other parts use local time. That can cause quite a difference in time...leading to different dates.

    Update: Here's a quickie example:

    $ cat x.pl #!/usr/bin/perl -w use strict; use warnings; printf "%d/%d %d:%02d:%02u\n", (gmtime(time))[4,3,2,1,0]; printf "%d/%d %d:%02d:%02u\n", (localtime(time))[4,3,2,1,0]; $ ./x.pl 11/23 19:51:40 11/23 14:51:40
    ...roboticus
Re: UnixDate - getting a different date
by zwon (Abbot) on Dec 23, 2008 at 22:24 UTC
    Yep, it's locale. I'm getting different results, depending on TZ value. But changing TZ after I've called UnixDate once doesn't have any effect.
    #!/usr/bin/perl use strict; use warnings; use Date::Manip; use Data::Dumper; my $tot_start_date = '2008-06-27 17:37:11 +0000'; my $tot_end_date = '2008-09-29 04:02:18 +0000'; my $beta_start_date = '2008-09-29 04:02:18 +0000'; my $beta_end_date = '2009-01-07 00:00:00 -0800'; #$ENV{TZ}='America/Montreal'; $ENV{TZ}='UTC'; $tot_start_date = UnixDate($tot_start_date,"%Y-%m-%d"); $tot_end_date = UnixDate($tot_end_date,"%Y-%m-%d"); $beta_start_date = UnixDate($beta_start_date,"%Y-%m-%d"); $beta_end_date = UnixDate($beta_end_date,"%Y-%m-%d"); print Dumper $tot_start_date; print Dumper $tot_end_date; print Dumper $beta_start_date; print Dumper $beta_end_date; __END__ output for Montreal: $VAR1 = '2008-06-27'; $VAR1 = '2008-09-28'; $VAR1 = '2008-09-28'; $VAR1 = '2009-01-07'; output for UTC: $VAR1 = '2008-06-27'; $VAR1 = '2008-09-29'; $VAR1 = '2008-09-29'; $VAR1 = '2009-01-07';
      ^^ Even if i set timezone, i am getting a different dates in some cases. Is there any way or knob to discard the time in the date with the UnixDate
        d-evil:

        If you're not going to use a module, the best knob is to use functions that are compatible with each other (i.e., use functions that assume UTC only or the local time zone only, and don't mix them).

        But the better knob would be to not worry about it at all. Instead, use one of the Date::* modules from CPAN to do all the grunt work.

        Update: By the way, I think you wanted to use <code> tags for (your question) your question, rather than <center>. You might go back to your node and fix it...

        ...roboticus

        Update: The [node] in parenthesis is present only so that node "id vs. node oddity" makes sense.