in reply to The date last monday

Years are counted from 1900 and months are zero-based, ie January is 0 and December is 11.

Cheers,

JohnGG

Update: Just noticed that you need leading zeros so use %02d to get those.

Replies are listed 'Best First'.
Re^2: The date last monday
by Scarborough (Hermit) on Oct 03, 2006 at 10:59 UTC
    I know and in production i have a sub routine to format as 20061002 but my challange is to do the formating and calulation in one line. Heres my sub
    sub get_CCYYMMDD{ my $timecode = $_[0]; my ($day, $month, $year) = (localtime($timecode))[3..5]; $year += 1900; $month++; $month = $month<10 ? "0".$month : $month; $day = $day<10 ? "0".$day : $day; my $today = $year.$month.$day; #print $today."\n"; return $today; }

    Am I asking to much to do this all in one go?

      You seem to have written a rather less flexible version of POSIX::strftime :-)

      use POSIX 'strftime'; sub get_CCYYMMDD { return strftime '%Y%m%d', $_[0]; }
      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

        I have never investigated POSIX maybe its time I did. Thanks for your advice
      "Am I asking to much to do this all in one go?"

      Well. no, as previous posters have demonstrated. On the other hand, if I were to encounter a one-liner 'solution' in a code-review I'd flag it as a 'Maintenance problem'. The one-liner is cute and quirky and flashy. But the purpose of code is to be readable at 0300 by a half-asleep bloke who just got to the pager before it woke the Baby. I vote for simple and verbose every time.

      ----
      I Go Back to Sleep, Now.

      OGB

        I'm with you, the orginal verbose version will stay in the code. However I have enjoyed the Monks contributions on this subject and from setting myself the challange have learnt about POSIX and map and how to pad sprinf which was not in the nutshell book. So for me a very worth while question. Thanks everyone
      Sorry I missed the drift of your OP. No, you are not asking too much at all. You need to transform the array slice using a map, like this

      perl -e 'printf qq{%4d%02d%02d\n},map{$_->[0]+1900,$_->[1]+1,$_->[2]}[ +((localtime(time-((1-(localtime(time))[6])* -86400)))[5,4,3])];'

      Note that the slice is passed into the map as an anonymus list.

      Cheers,

      JohnGG

        Thanks for the help, I finally understand map the missing link for me.

        Glad I felt like a challange this morning.
        Note that the slice is passed into the map as an anonymus list.

        <pedant>You mean an anonymous array.<pedant>

        --
        <http://dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg