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

[OT] Number of seconds from epoch

by sanPerl (Friar)
on Feb 13, 2007 at 13:40 UTC ( #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 (Pope) on Feb 13, 2007 at 13:41 UTC


      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...


      fmerges at
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! :(

    From the perl faq 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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://599683]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2018-05-25 11:44 GMT
Find Nodes?
    Voting Booth?