Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Diff'ing dates (solved)

by LighthouseJ (Sexton)
on Oct 02, 2007 at 13:13 UTC ( [id://642097]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to find the difference in seconds between two dates. The two (and only two) dates come as standard out like:
Sun 09 30 06 42 36 2007 Tue 10 02 06 01 55 2007
(Note: the months correspond to September and October respectively.)

I tried to do this with awk because I use awk in plenty of other places but awk's mktime() is a GNU-only addition it seems and I only have access to barebones Solaris installs. The only alternative remaining I think is Perl's timelocal function in Time::Local which I believe is a part of the standard install, right? My goal is to put a Perl one-liner in a sh script and this is the last piece in the pipe. I've already written a Perl script which works but purely as an exercise for the monkish community, where are the improvements I'm missing?

Here's what I have currently but I am actively working on it though:
my @array; while (<>) { chomp; my @fields = split; shift @fields; unshift(@fields, pop(@fields)); $fields[1]--; # decrement the month, timelocal is zero-based push (@array, timelocal (reverse @fields)); } print $array[1] - $array[0];
"The three principal virtues of a programmer are Laziness, Impatience, and Hubris. See the Camel Book for why." -- `man perl`

Replies are listed 'Best First'.
Re: Diff'ing dates
by ikegami (Patriarch) on Oct 02, 2007 at 13:33 UTC
    perl -MTime::Local -lane"END{print$t[1]-$t[0]}$F[1]--;push@t,timelocal +@F[5,4,3,2,1,6]"

    Update:

    perl -MTime::Local -lane"END{print$t}$F[1]--;$t+=($.*2-3)*timelocal@F[ +5,4,3,2,1,6]"
      Using your inspiring one-liner, I morphed my script with yours and came up with this:

      /bin/perl -MTime::Local -ne '{$t[1]-- && push(@epochs,timelocal(@t[5,4,3,2,1,6])) if (@t=split)} END { print $epochs[1] - $epochs[0]; }'

      The END block is most likely what will change because the value of the diff will be used to determine exit status.

      Thanks a lot for the fresh perspective, it was very helpful.

      "The three principal virtues of a programmer are Laziness, Impatience, and Hubris. See the Camel Book for why." -- `man perl`

        The exit code can be controled as follows:

        END { exit 1+($epochs[0] <=> $epochs[1]) }
        0: first time is earlier 1: both times are the same 2: second time is earlier
        perl -MTime::Local -lane'END{exit 1+($d<=>0)}$F[1]--;$d+=($.*2-3)*time +local@F[5,4,3,2,1,6]'
      I tried the command and this is what I got:
      $ echo -e "Sun 09 30 06 42 36 2007\nTue 10 02 06 01 55 2007\n" | perl +-MTime::Local -lane"END{print$t}$F[1]--;$t+=($.*2-3)*timelocal@F[5,4, +3,2,1,6]" Can't modify single ref constructor in postdecrement (--) at -e line 1 +, near "]--" syntax error at -e line 1, near "+=" Execution of -e aborted due to compilation errors.
      "The three principal virtues of a programmer are Laziness, Impatience, and Hubris. See the Camel Book for why." -- `man perl`
        Use single quotes on UNIX-like operating systems:
        echo -e "Sun 09 30 06 42 36 2007\nTue 10 02 06 01 55 2007\n" | \ perl -MTime::Local -lane 'END{print$t}$F[1]--;$t+=($.*2-3)*timelocal@F +[5,4,3,2,1,6]'

        Double quotes cause your shell to interpret e.g. $t as a shell variable.

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
        Use the appropriate quotes for your shell. Switch double quotes for single quotes.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

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

    No recent polls found