CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
strftime gives wrong year?

 



Stefan Mensink
New User

Feb 3, 2005, 2:09 PM

Post #1 of 2 (399 views)
strftime gives wrong year? Can't Post

Hello,

While writing a perl script to analyze some logfiles, I ran into a specific problem with strftime for the dates Jan 1st and Jan 2nd 2005. When formatting such a time using strftime, the returned year is 2004! For other dates, everything is OK.

Here's an example script:
------------<cut>-----------
#!/usr/bin/perl

use POSIX qw(strftime);
use Time::Local;

$debug = 0;

$month = 0;
$day = 1;

$mytime = timelocal(10, 10, 10, $day, $month, $year);

for ($year = 1997; $year < 2007; $year++) {
print "-------------------------------------------\n";
print "year=$year month=$month day=$day\n";
$mytime = timelocal(0, 0, 0, $day, $month, $year);
if ($debug) {
($a, $b, $c, $d, $e, $f, $g, $h, $i, $j) = localtime(timelocal(0, 0, 0, $day, $month, $year));
print "$a $b $c $d $e $f $g $h $i $j\n";
}
print strftime("%G-%m-%d", localtime($mytime)),"\n";
}
------------<cut>-----------

and the output would be:
------------<cut>-----------
-------------------------------------------
year=1997 month=0 day=1
1997-01-01
-------------------------------------------
year=1998 month=0 day=1
1998-01-01
-------------------------------------------
year=1999 month=0 day=1
1998-01-01
-------------------------------------------
year=2000 month=0 day=1
1999-01-01
-------------------------------------------
year=2001 month=0 day=1
2001-01-01
-------------------------------------------
year=2002 month=0 day=1
2002-01-01
-------------------------------------------
year=2003 month=0 day=1
2003-01-01
-------------------------------------------
year=2004 month=0 day=1
2004-01-01
-------------------------------------------
year=2005 month=0 day=1
2004-01-01
-------------------------------------------
year=2006 month=0 day=1
2005-01-01
------------<cut>-----------

My timezone would be CET, if that's relevant.

Does anyone have any idea what I'm doing wrong, and/or how to fix the program to print out the correct time (without rewriting strftime, of course).

Thank you,

Stefan Mensink


Stefan Mensink
New User

Feb 3, 2005, 2:32 PM

Post #2 of 2 (397 views)
Re: [Stefan Mensink] strftime gives wrong year? [In reply to] Can't Post

OK. After three evenings of investigating, I finally found what I did wrong.

Instead of using the %G in strftime, which returns the 4-digit year for the given week I should have used %Y which returns the 4-digit year for that exact date.

cheers,
Stefan Mensink

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives