Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

WWW::Mechanize wrong month

by gsparx (Novice)
on Oct 31, 2012 at 04:47 UTC ( [id://1001595]=perlquestion: print w/replies, xml ) Need Help??

gsparx has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl Monks,

I am having an issue with pulling server time information from a server, and my own computer in fact. I am running perl inside of a Windows Citrus Perl environment, but I see a similar issue in Ubuntu as well.

I am pulling the time/date from a server with

$servertime = ($mech->response()->header("Date"));

and the local time with

$loctime = localtime(time);

but I'm getting the wrong date returned. For example, it is currently October (and it displays this on my computer's clock) but when I call the above code, and then parse it with

@servertime = strptime($servertime); @loctime = strptime($loctime);

I get the following two values:

112 9 31 04 25 06

112 9 30 21 25 05

The above date says that it's September, and since I'm trying to then use Delta_DHMS, it complains that it's an invalid date because I'm trying to tell it that it's September 31...which it's obviously not.

Can anyone think of an easy way to just increment the month so it's 10, or why I might be getting the wrong month from both this server and my own local machine?

Thanks for your help,

Garrett

Replies are listed 'Best First'.
Re: WWW::Mechanize wrong month
by NetWallah (Canon) on Oct 31, 2012 at 06:18 UTC
    Perl Month numbers from 'localtime' etc are ZERO based.

    This syntax:

    ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = POSIX::strpti +me(string, format);
    returns values similar to localtime(), which uses zero-based months.

    $mday is the day of the month and $mon the month in the range 0..11, with 0 indicating January and 11 indicating December. This makes it easy to get a month name from a list:

                 "By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."           -Confucius

Date arithmetic is not recommended
by space_monk (Chaplain) on Oct 31, 2012 at 06:59 UTC
    There are lots and lots of pitfalls in date arithmetic (leap seconds, February, leap years etc), so don't mess with dates when someone else has done it for you. Browse CPAN for an appropriate Date class (Class::Date, Date::Calc etc) and use that for all your date display formatting and calculations.

      So that was the reason I was using Delta_DHMS from Date::Calc, because I wanted it to find the difference for me, but it seems like Date::Calc is not in fact ZERO based, but rather counts months from 1-12.

      I'm currently just incrementing the Month by 1 before I feed it into Delta_DHMS, and it seems to be working so far. So I should probably increment all the values in order to get the right day as well.

      Any other suggestions? Getting a date from WWW::Mechanize will mean it's ZERO based, so I wonder if I can get Date::Calc to take dates that are ZERO based rather than ONE based

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1001595]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-23 17:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found