Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Time to seconds

by BlueLines (Hermit)
on Nov 16, 2001 at 00:40 UTC ( [id://125664]=note: print w/replies, xml ) Need Help??


in reply to Time to seconds

Umm, the regexen are all nice, but why not use Time::Local to handle the time part?
use Time::Local; my ($hours,$minutes,$seconds) = (1,2,3); #insert your regex here print timelocal($seconds,$minutes,$hours,1,0,70);


BlueLines

Disclaimer: This post may contain inaccurate information, be habit forming, cause atomic warfare between peaceful countries, speed up male pattern baldness, interfere with your cable reception, exile you from certain third world countries, ruin your marriage, and generally spoil your day. No batteries included, no strings attached, your mileage may vary.

Replies are listed 'Best First'.
Re: Re: Time to seconds
by chipmunk (Parson) on Nov 16, 2001 at 08:02 UTC
    The result of timelocal will depend on the local time zone. For example, when I ran your code it printed 18123 because I'm at GMT-0500. Of course, that can be avoided by using gmtime insead. The following code prints the expected 3723:
    use Time::Local; my ($hours, $minutes, $seconds) = (1, 2, 3); #insert your regex here print timegm($seconds, $minutes, $hours, 1, 0, 70);
    Unfortunately, even that's not a complete solution, because the epoch is not Jan 1, 1970 on all platforms. On the Macintosh, for example, it's Jan 1, 1904, and the output is 2082830523.

    There's still a way to do it with timelocal, though:

    use Time::Local; my ($hours, $minutes, $seconds) = (1, 2, 3); #insert your regex here print timelocal($seconds, $minutes, $hours, 1, 0, 100) - timelocal(0, 0, 0, 1, 0, 100);
    The choice of year is relatively arbitrary, but note that you mustn't pick a time of year that will be affected by daylight savings.

    So, you can solve this problem using Time::Local, you just have to be careful about how you do it.

Re: Re: Time to seconds
by argus (Acolyte) on Nov 16, 2001 at 17:34 UTC
    I thought of Time::Loacla at first, but the code had to be as portable as possible. Some of the machines that it might run on might not have that module installed. I hated useing Getopts::Long, but it seems generally standard around here and I am in a time crunch. I might recode it later. See the Mass Domian Name Lookup for how I used it.

      Do you mean Time::Local? If so then you don't have a problem as the module is part of the standard Perl distribution and will be installed everywhere where Perl is. If it isn't installed then there is something wrong with the Perl installation and you may have far more serious problems :)

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you don't talk about Perl club."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-03-29 09:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found