problem using timelocal, localtime to discern d-o-w.

Discussion in 'Perl Misc' started by John, Mar 22, 2007.

  1. John

    John Guest

    Given a date passed in from a spreadsheet, I'm trying to come up with
    the day of the week.

    Trying to use timelocal (http://search.cpan.org/~drolsky/Time-
    Local-1.17/lib/Time/Local.pm) to turn the date into a scalar, then
    localtime to return the full slate of results, including the day of
    the week.

    I seem to be getting back at least what I put in -- that is the month,
    day and year are inverted, reverted and returned as I first called
    timelocal. But the day of week is not matching my paper calendar.


    #!/usr/bin/perl -w
    use strict;
    use Time::Local;

    my $gdate;
    my @wkdayname = ('Sunday', 'Monday', 'Tuesday', 'Wednesday',
    'Thursday',
    'Friday', 'Saturday', 'Sunday');

    {
    $gdate = "4/18/2007";
    my ($gmonth, $gmday, $gyear);
    ($gmonth, $gmday, $gyear) = split(/\//,$gdate);

    {
    print "\ngmonth = $gmonth, ";
    print "gmday = $gmday, ";
    print "gyear = $gyear\n ";
    }

    my $gday;
    my @timeScr;
    my $scrTime=timelocal(0, 0, 11, $gmday, $gmonth, $gyear-1900);
    @timeScr = localtime($scrTime);
    print "timescr= @timeScr \n";

    $gday = $timeScr[6];
    print "using timescr6 gday = $gday\n";
    $gday = $wkdayname[$gday];
    print "gday = $gday\n";

    }


    Results:

    gmonth = 4, gmday = 18, gyear = 2007
    timescr= 0 0 11 18 4 107 5 137 1
    using timescr6 gday = 5
    gday = Friday

    But it says here that April 18, 2007 is a Wednesday.


    John Campbell
    Haddonfield, NJ

    Perldoc v3.09, under perl v5.008001 for darwin
     
    John, Mar 22, 2007
    #1
    1. Advertising

  2. John

    Paul Lalli Guest

    On Mar 22, 8:49 am, "John" <> wrote:
    > Given a date passed in from a spreadsheet, I'm trying to come up with
    > the day of the week.
    >
    > Trying to use timelocal (http://search.cpan.org/~drolsky/Time-
    > Local-1.17/lib/Time/Local.pm) to turn the date into a scalar, then
    > localtime to return the full slate of results, including the day of
    > the week.
    >
    > I seem to be getting back at least what I put in -- that is the month,
    > day and year are inverted, reverted and returned as I first called
    > timelocal. But the day of week is not matching my paper calendar.
    >
    > #!/usr/bin/perl -w
    > use strict;
    > use Time::Local;
    >
    > my $gdate;
    > my @wkdayname = ('Sunday', 'Monday', 'Tuesday', 'Wednesday',
    > 'Thursday',
    > 'Friday', 'Saturday', 'Sunday');
    >
    > {
    > $gdate = "4/18/2007";
    > my ($gmonth, $gmday, $gyear);
    > ($gmonth, $gmday, $gyear) = split(/\//,$gdate);
    >
    > {
    > print "\ngmonth = $gmonth, ";
    > print "gmday = $gmday, ";
    > print "gyear = $gyear\n ";
    > }
    >
    > my $gday;
    > my @timeScr;
    > my $scrTime=timelocal(0, 0, 11, $gmday, $gmonth, $gyear-1900);


    You should, perhaps, read the documentation you linked to above. :)

    It is worth drawing particular attention to the expected
    ranges for the values provided. The value for the day of
    the month is the actual day (ie 1..31), while the month is
    the number of months since January (0..11). This is
    consistent with the values returned from localtime() and
    gmtime().

    So in other words, you passed it *May* 18, 2007, which is indeed a
    Friday.

    > @timeScr = localtime($scrTime);
    > print "timescr= @timeScr \n";
    >
    > $gday = $timeScr[6];
    > print "using timescr6 gday = $gday\n";
    > $gday = $wkdayname[$gday];
    > print "gday = $gday\n";
    >
    > }
    >
    > Results:
    >
    > gmonth = 4, gmday = 18, gyear = 2007
    > timescr= 0 0 11 18 4 107 5 137 1
    > using timescr6 gday = 5
    > gday = Friday
    >
    > But it says here that April 18, 2007 is a Wednesday.



    By the way, there's no need for localtime/timelocal here. Just pass
    your values to POSIX::strftime and ask it what the day of week is:

    #!/usr/bin/perl
    use strict;
    use warnings;
    use POSIX qw/strftime/;
    my $gdate = "4/18/2007";
    my ($gmonth, $gmday, $gyear) = split(/\//,$gdate);
    print "\ngmonth = $gmonth, ";
    print "gmday = $gmday, ";
    print "gyear = $gyear\n";
    print strftime("%A\n", 0, 0, 0, $gmday, $gmonth - 1, $gyear - 1900);
    __END__

    Hope this helps,
    Paul Lalli
     
    Paul Lalli, Mar 22, 2007
    #2
    1. Advertising

  3. John

    John Guest

    On Mar 22, 9:27 am, "Paul Lalli" <> wrote:
    >
    > ... the month is
    > the number of months since January (0..11). This is
    > consistent with the values returned from localtime() and
    > gmtime().
    >
    > By the way, there's no need for localtime/timelocal here. Just pass
    > your values to POSIX::strftime


    > Hope this helps,



    It does help. strftime works like a charm. Thanks also to Glenn.
     
    John, Mar 22, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J Moreno
    Replies:
    14
    Views:
    263
    J Moreno
    Mar 9, 2006
  2. Datamon
    Replies:
    1
    Views:
    106
  3. Tina dan
    Replies:
    0
    Views:
    580
    Tina dan
    Jun 18, 2012
  4. Tina dan
    Replies:
    0
    Views:
    529
    Tina dan
    Jun 18, 2012
  5. Tina dan
    Replies:
    0
    Views:
    596
    Tina dan
    Jun 19, 2012
Loading...

Share This Page