--Howdy: -- // This is mostly for archives sake just in case I forget how // -- // I did this //
--To recap, the purpose of this exercise was to create a few date variables to use --in an SQL script. I wanted the dates to have a 3 months span every time --I run the script (which would be once a month) so I wouldn't have to --change the date in the SQL script each time. --A few problems: * Create an anchor date to be the first day of the month; this represents the month the script is executed (e.g., if today is 11/12/2002, the anchor date is 11/01/2002) * Calculate spans that crossed new (multiple) year(s). (e.g., if i wanted spans from 3 years ago up to the middle of next year) * Calculate month UP TO the last day of the third month * Get four digit year --So ... this is what I came up with. Seems to work, but I'm going to test --it in the SQL script now. [begin perl script] !/usr/bin/perl -w # created 8 nov 02 - X # looks like we have a winner ... now test on NT platform # 12 nov 02 -X use strict; use Date::Manip; ## get first of this month as anchor ## my $day_now=(localtime())[3]; my $firstday=$day_now - ($day_now-1); my $month_now=(localtime())[4]+1; my $year_now=(localtime())[5]; my $new_year_now=$year_now + 1900; ## now define whole value ## # my $full_date = "$month_now/$day_now/$new_year_now"; my $full_date = "$month_now/$firstday/$new_year_now"; ## try to put it in to be parsed ## # my $date = ParseDate("today"); my $date = ParseDate($full_date); print "today is (in epoch-speak): ", $date, "\n\n"; ## my $startdate = DateCalc($date, "-4 months"); #my $enddate = DateCalc($startdate, "+23 months"); # *evil lol* ... didn't think this would work ... go fig ... my $enddate = DateCalc($startdate, "+3 months - 1 day"); print "start date 4 months ago is (in epoch-speak) ", $startdate, "\n"; print "end date 3 month span is (in epoch-speak)", $enddate, "\n\n"; my $newnow = UnixDate($date, "%m/%d/%Y"); my $newstart = UnixDate($startdate,"%m/%d/%Y"); my $newend = UnixDate($enddate,"%m/%d/%Y"); print "today (in english-speak) is: ", $newnow, "\n\n"; print "new start date (in english-speak) is 4 months ago: ", $newstart, "\n\n"; print "new end date (in english-speak) is: \n"; print "3 months (to last day of month) since new start date ", $newend, "\n\n"; __END__ [end perl script] --Yes, I know that this is probably the ugliest code you've ever seen ... when --I get more experienced with perl, I'll clean it up (or, learn a new technique or three). --Thanks all! -X