Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Epoch to specific output

by Anonymous Monk
on Jan 02, 2014 at 16:32 UTC ( [id://1068981]=perlquestion: print w/replies, xml ) Need Help??

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

I am new to Perl and would like to perform a quick conversion using a one liner Perl execution in bash shell.

x=1388679108 perl -e 'print strftime("%m%d%H%M", $x)' 01021611

I am looking for a way to change any given epoch time to a date format of month/day/hour/minute that will be used for a time stamp. I am aware that perl -e "print scalar localtime $x" will provide me with "Thu Jan 2 16:11:48 2014". But, I am looking for a different type of output format.

Replies are listed 'Best First'.
Re: Epoch to specific output
by Utilitarian (Vicar) on Jan 02, 2014 at 16:45 UTC
    Something like the following (substituting your epoch value for time() )
    $ perl -MPosix -E 'say POSIX::strftime("%m%d%H%M",localtime(time()))' 01021644
    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."

      Thank you for the swift reply.

      The system that I am using does not have the Posix module installed. Is there a way to do this without that option?

        Use upper case: POSIX is a Core module installed with Perl.
        perl -MPOSIX -E 'say POSIX::strftime("%m%d%H%M",localtime(time()))'
        the POSIX module has been core since God was a boy, what version/environment are you on?

        print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: Epoch to specific output
by atcroft (Abbot) on Jan 02, 2014 at 18:07 UTC

    This *seems* to do what you are asking, at least for my first test:

    perl -MPOSIX -e 'my $t = time; print strftime("%m%d%H%M", gmtime $t);'
    You could use localtime in place of gmtime, but then you would have to consider what you want to occur when there are DST (daylight saving time) changes.

    Hope that helps.

      Your suggestion works well with a direct input for time. However, I was unable to substitute time with the $x variable.

      I was able to get it to work though with the following code:

      echo $x|perl -MPOSIX -e 'print strftime("%m%d%H%M", gmtime <stdin>)'

      Thank you everyone for your help!

        Pass your variable as an additional argument, use @ARGV to get it from inside the program:
        perl -MPOSIX -le'print strftime("%m%d%H%M", gmtime $ARGV[0])' "$x"
Re: Epoch to specific output
by oiskuu (Hermit) on Jan 02, 2014 at 20:04 UTC
    Using bash you say? If gnu tools are available, the gnu date utility can perform said task:
    $ date +%Y%m%d%H%M -u -d @1388679108
    
    (Prints the UTC stamp for given epoch time.)
      Not to say that non-GNU versions of date(1) perform any worse:
      % date -ur 1388679108 +%m%d%H%M 01021611
      That syntax ought to work on all of the four major BSDs.
Re: Epoch to specific output
by kcott (Archbishop) on Jan 03, 2014 at 04:21 UTC

    The core Time::Piece module does what you're after:

    $ x=1388679108 \ > perl -MTime::Piece -e 'print gmtime($ENV{x})->strftime("%m%d%H%M")' 01021611

    -- Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-26 00:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found