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

J

John

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
 
P

Paul Lalli

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
 
J

John

... 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top