Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

time() returning the wrong time?

Status
Not open for further replies.

nitrostar

Programmer
Sep 2, 2008
2
US
Hi, I'm trying to display the current time and date...

If I do this:

Code:
#!/usr/bin/perl
use POSIX;

print "Time is: ";
$test = time();
print "$test\n";
($sec,$min,$hour,$mday,$month,$year) = gmtime($test);
print "$year-$month-$mday-$hour:$min:$sec\n";
print strftime "%Y-%m-%d-%H.%M.%S\n", gmtime;

I get this:
Time is: 1220390156
108-8-2-21:15:56
2008-09-02-21.15.56

Why am I getting month 8 in the second line? Isn't today september?
 
See this:


The month is represented from 0 to 11, not 1 to 12. strftime() takes care of this stuff for you (also note the year is relative to 1900, so 1900 + 108 is 2008, in case you were wondering about that too).

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Awesome

I had found documentation on the +1900 but nothing on +1 for month....

I had fixed it using
Code:
print strftime "%Y-%m-%d-%H.%M.%S\n", gmtime($test);


Thank you so much!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top