in reply to Care to simplify some working code?

Assuming you don't actually need any of the intermediate stuff, this calculates it directly:

#!/usr/bin/perl -slw use strict; use warnings; use Time::Local; use Test::More "no_plan"; sub lastBusinessDayPrevQ { my( $y, $m, $d ) = unpack 'A4A2A2', shift; my $t = timelocal( 0,0,12, $d, $m-1, $y ); $t -= 43200 until localtime( $t ) !~ m[Mar|Jun|Sep|Dec]; $t -= 43200 until localtime( $t ) =~ m[Mar|Jun|Sep|Dec]; $t -= 43200 until localtime( $t ) =~ m[Mon|Tue|Wed|Thu|Fri]; ( $y, $m, $d ) = unpack 'x20a4 X20a3 xa2', scalar localtime( $t ); $m = { Mar=>'03', Jun=>'06', Sep=>'09', Dec=>'12' }->{ $m }; return join '', $y, $m, $d; } test(); sub test { ok ( lastBusinessDayPrevQ("20110901") eq "20110630"); ok ( lastBusinessDayPrevQ("20120101") eq "20111230"); }

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Care to simplify some working code?
by Voronich (Hermit) on Sep 28, 2011 at 20:42 UTC

    (hmm... I've tried posting a response to this 3 times now.)

    This is spectacular. Thanks very much. I haven't been made to feel quite so foolish in a very long time. Bravo sir.

    BrowserUk++

    Me

      Beware the caveats! See Is there an easy way to get the start date of the current week? for discussion of most of them.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.