Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Where did all the time go?

by ExReg (Priest)
on Aug 01, 2019 at 20:14 UTC ( #11103727=perlquestion: print w/replies, xml ) Need Help??

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

Just seeking enlightenment here. Already can get it to do what I want, but... I was typing in a few things to get them in the exact format I wanted when I ran into a peculiarity. When I enter

perl -e '($s,$m,$h,$md,$mo,$y,$w,$g,$i)=localtime;print $y+1900 . "." . ($mo+1) . ".$md." . ($h-5) . "$m\n";'

I get 2019. (I hope I transcribed the above correctly)

But if I type

perl -e '($s,$m,$h,$md,$mo,$y,$w,$g,$i)=localtime;print $y+1900 . "." . $mo+1 . ".$md." . ($h-5) . "$m\n";'

I get 2020. The +1 after $mo now gets applied to $y instead. The same is true for the parentheses around $h. I am trying to wrap my feeble mind around this one. What is going on?

Replies are listed 'Best First'.
Re: Where did all the time go?
by choroba (Archbishop) on Aug 01, 2019 at 20:20 UTC
    Precedence issue. Use B::Deparse with -p to see how Perl interprets the code:
    $ perl -MO=Deparse,-p -e '($s,$m,$h,$md,$mo,$y,$w,$g,$i)=localtime;pri +nt $y+1900 . "." . ($mo+1) . ".$md." . ($h-5) . "$m\n";' (($s, $m, $h, $md, $mo, $y, $w, $g, $i) = (localtime)); print((((((($y + 1900) . '.') . ($mo + 1)) . ".$md.") . ($h - 5)) . "$ +m\n")); $ perl -MO=Deparse,-p -e '($s,$m,$h,$md,$mo,$y,$w,$g,$i)=localtime;pri +nt $y+1900 . "." . $mo+1 . ".$md." . ($h-5) . "$m\n";' (($s, $m, $h, $md, $mo, $y, $w, $g, $i) = (localtime)); print(((((((($y + 1900) . '.') . $mo) + 1) . ".$md.") . ($h - 5)) . "$ +m\n"));
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      I knew it had to be precedence, but was unsure what it really looked like. The B::Deparse showed it beautifully. I will have to remember that one.

Re: Where did all the time go?
by Fletch (Chancellor) on Aug 01, 2019 at 20:20 UTC

    Tangential, but you might look at POSIX and strftime or something like DateTime::Format rather than monkeying with reimplementing wheels.

    As to your question you can see how the precedence changes omitting the parens plays out if you stick -MO=Deparse,-p before the -e.

    Update: Yeah, what he said below.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      POSIX and strftime

      I'd suggest Time::Piece instead, it's been a core module since 5.10.


      $ perl -MTime::Piece -le 'print localtime->strftime("%Y.%m.%d.%H%M")' 2019.08.01.2239



      Note that link goes to a defunct module, you probably meant the DateTime::Format::* family of modules instead. But plain DateTime has both strftime and CLDR formatting built right in, that's generally been enough for me (I usually parse with DateTime::Format::Strptime).

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2021-09-24 10:17 GMT
Find Nodes?
    Voting Booth?

    No recent polls found