Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

[OT] Number of seconds from epoch

by sanPerl (Friar)
on Feb 13, 2007 at 13:40 UTC ( [id://599683]=perlquestion: print w/replies, xml ) Need Help??

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

I am writing perl scripts for nagios. I am getting a value $time_new from a subroutine. I am using
$time = `date -d '$time_new' '+%s'`;

to get the number of seconds from epoch (for e.g. date -d 'Tue Feb 13 19:04:04 IST 2007' +%s, gives you 1171373644). It is running fine on Linux, when I run my script on SunOS Unix, the script fails.
I checked with man pages of SunOS and found that there is NO +%s option in Unix date command.
Any suggestions please

Replies are listed 'Best First'.
Re: [OT] Number of seconds from epoch
by Corion (Patriarch) on Feb 13, 2007 at 13:41 UTC

      Hi,

      In add to this, take care of what you really need, mean localtime or gmtime. In scientific stuff and in other fields you usually use UT as a convention instead of the local time, so take care with this...

      Regards,

      fmerges at irc.freenode.net
Re: [OT] Number of seconds from epoch
by almut (Canon) on Feb 13, 2007 at 14:17 UTC

    Just in case you're too lazy to split up your date string into something that Time::Local can handle, and don't mind slurping in 240k of Perl code, you could also use the module Date::Manip

    #!/usr/bin/perl use Date::Manip qw(UnixDate); my $time_new = 'Tue Feb 13 19:04:04 IST 2007'; my $time = UnixDate($time_new, "%s"); print "secs since the Epoch: $time\n"; # prints 1171373644
      Thanks a lot to everyone, for wonderful suggestions.
Re: [OT] Number of seconds from epoch
by klekker (Pilgrim) on Feb 13, 2007 at 13:57 UTC
    time should do.

    update: ah sorry, no help at all... just figured out that you wanted to convert a date! :(

    update2:
    From the perl faq http://perldoc.perl.org/perlfaq4.html: How can I take a string and turn it into epoch seconds?

    If it's a regular enough string that it always has the same format, you can split it up and pass the parts to timelocal in the standard Time::Local module. Otherwise, you should look into the Date::Calc and Date::Manip modules from CPAN.


    k

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (8)
As of 2024-04-19 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found