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"); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Care to simplify some working code?
by Voronich (Hermit) on Sep 28, 2011 at 20:42 UTC | |
by BrowserUk (Patriarch) on Sep 29, 2011 at 03:28 UTC |