Actions

Perl: Backdating to Sunday: Difference between revisions

From Rabbi Blog

(Migration - Backdating to Sunday)
 
mNo edit summary
Line 1: Line 1:
Spent a little time on this in a related work project: backdating a date to the Sunday of the same week. Sounded simple at first, however I finally resulted to a dry erase board to map it out before coding it. Funny how leaving the terminal sometimes is a good thing.
Spent a little time on this in a related work project: backdating a date to the Sunday of the same week. Sounded simple at first, however I finally resulted to a dry erase board to map it out before coding it. Funny how leaving the terminal sometimes is a good thing.
 
[[Image:Backdatingsunday.JPG|thumb|right|The notes]]
<pre>
<pre>
use Date::DayOfWeek;
use Date::DayOfWeek;

Revision as of 10:45, 3 May 2007

Spent a little time on this in a related work project: backdating a date to the Sunday of the same week. Sounded simple at first, however I finally resulted to a dry erase board to map it out before coding it. Funny how leaving the terminal sometimes is a good thing.

The notes
use Date::DayOfWeek;
use Date::Leapyear;

if ($DateA =~ /(\d{4})(\d{2})(\d{2})/)
	{
	my $tmpyear = $1; #Year
	my $tmpmonth = $2; #Month
	my $tmpday = $3; #Day

	#Determine the day of week
	#Sun0,Mon1,Tue2,Wed3,Tr4,Fri6,Sat7

	my $DOW = dayofweek($tmpday, $tmpmonth, $tmpyear);

	#Determine what happens when we subtract the value of
	#the date from the Day.
	#If it is positive, then we remain in the week.
	#If it is Zero, then the date is Sunday and no roll is needed
	#If it is negative, then we've moved back in the previous week
	#If we are previous week, we need to make sure that
	#we check roll to the previous month. If the current month
	#is Jan, we need to roll the year back also.
	
	my $test = $tmpday - $DOW;

	#Positive Outcome
	if ($test > 0)
		{
		$tmpday=$test;
		}

	#Negative Outcome
	elsif ($test < 0)
		{
		### Check for March Roll back to Feb
		### Honor the Leap Year
		if ($tmpmonth == 03)
			{
			if (isleap($tmpyear))
				{
				$tmpday=(29-abs($test));
				$tmpmonth--;
				}
			else
				{
				$tmpday=(28-abs($test));
				$tmpmonth--;
				}
			}
		#Check for transitions INTO 31 day months
		elsif ($tmpmonth =~ m/02|04|07|09|11/)
			{
			$tmpday=(31-abs($test));
			$tmpmonth--;
			}
		#Check for transitions INTO 30 day months
		elsif ($tmpmonth =~ m/05|07|08|10|12/)
			{
			$tmpday=(30-abs($test));
			$tmpmonth--;
			}
		#Check for transitions into December
		#Roll both the Month & Year back
		elsif ($tmpmonth == 01)
			{
			$tmpday=(31-abs($test));
			$tmpmonth=12;
			$tmpyear--;
			}
		}
	#Put the date back together
	$DateA="$tmpyear$tmpmonth$tmpday";

	# Cludge to test to make sure that no new date is greater than 0 and let us know if so
	# Irrelevant to the output
	my $TESTDOW = dayofweek($tmpday, $tmpmonth, $tmpyear);
	if ($TESTDOWN>0)
		{
		print "!\n";
		}
	}
	#Put the date back together
	$DateA="$tmpyear$tmpmonth$tmpday";

	# Cludge to test to make sure that no new date is greater than 0 and let us know if so
	# Irrelevant to the output
	my $TESTDOW = dayofweek($tmpday, $tmpmonth, $tmpyear);
	if ($TESTDOWN>0)
		{
		print "!\n";
		}
	}