Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

[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

Comment on [OT] Number of seconds from epoch
Download Code
Re: [OT] Number of seconds from epoch
by Corion (Pope) 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 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

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.

Log In?
Username:
Password:

What's my password?
Create A New User
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? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2014-09-17 05:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (58 votes), past polls