Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Convert Time to Epoch Using External Variable

by Anonymous Monk
on Feb 04, 2015 at 14:39 UTC ( [id://1115524]=perlquestion: print w/replies, xml ) Need Help??

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

Hope someone will be able to help me. I tried looking at previous answers and questions but did not find a solution.

Restrictions: Perl v. 5.8.4, Solaris 10, default modules

I would like to convert a specified time (e.g., Wed Feb 4 12:34:56 2015) to an epoch time using one line of Perl.

I was thinking about using:

echo "56,34,12,4,1,115"|perl -MPOSIX -e 'print mktime(<stdin>)'

However, <stdin> is treated as just one variable instead of all the variables.

Replies are listed 'Best First'.
Re: Convert Time to Epoch Using External Variable
by choroba (Cardinal) on Feb 04, 2015 at 14:45 UTC
    You can use the numbers as arguments instead of reading them from the standard input:
    perl -MPOSIX -e 'print mktime @ARGV' -- 56 34 12 4 1 115

    If you need to use the standard input and the comma separated format, use split:

    echo 56,34,12,4,1,115 | perl -MPOSIX -e 'print mktime split /,/, <>'
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Note the use of -- to tell perl (the executable) to pass the remaining arguments to the Perl script (in this case, given by -e). See Command Switches.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

        I think the -- flag to stop option parsing is only necessary if something you want treated as an argument actually looks like it might be an option, e.g. -999 or --foo. In the code given it is not required but it does no harm and using it is not a bad habit to get into.

        $ perl -MPOSIX -le 'print mktime @ARGV' -- 56 34 12 4 1 115 1423053296 $ perl -MPOSIX -le 'print mktime @ARGV' 56 34 12 4 1 115 1423053296 $

        I hope this is of interest.

        Cheers,

        JohnGG

      Thank you for your help!

      The @ARGV seems like the best option for me.

Log In?
Username:
Password:

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

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

    No recent polls found