HI, I have a script which simulates the rollover/rollback of timestamps for a file. In the attached script, the dates refer to the timestamp of the data being written to a file to be processed at a later time. Therefore, there is a timestamp to denote the time for processing the file. If date to which the data gets written to the file is denoted in terms of its number of days entered into the year, e.g. $in_year=365 denotes Dec 31 and $in_year=89 denotes Mar 31 of a particular non-leap year. Purpose of representing the dates in such a notation is reduce the number of bits represented in the file. What the script is suppose to do: 1) If the date written to the data file is 365 i.e. Dec 31 and that file containing the date Dec 31 (365 in reality) is processed on Jan 1 the following year (which is represented as $in_year=1 i.e. as the number of days entered in a year is only 1), therefore the date that would be appended to the file should reflect the original date and most importantly the original year of to the time the original data was written to the file 2) If the data written to the file is 1 i.e. Jan 1 of the following year, and that file containing that date is processed on $in_year=365 (Dec 31 of the previous year, due to timezone differences), therefore the date to be appended to the original file containing Jan 1 should reflect the date to which the date value i.e Jan 1 was written to the file. However, I'm not able to rollover or rollback the dates with respect to the original date of with respect to the file. Please note that the script attached does not include the part of appending the original date to the file. The script serves to test the date rollover when it's being represented as in the number of days entered in the year. I was wondering if anyone could point out where did I go wrong in the script? Thanks Danny script: #!/usr/bin/perl use strict; my $in_ydays = 365; my ($year, $month, $day); my $current_date = 1; my @actual_date = localtime; my $current_month = 1; $year = 2005; #print("Izmet: Actual month = $actual_date[4], year = $actual_date[7]\n"); ## maps value from $month(numerical) to obtain exact month value in string ## ??? --> 0 month my @MONTH_MAP = (qw (??? Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)); #### Fix overflow of the "month" value #### if $day==365, month++ overflows from month_ary. therefore, additional 31 needed. my @month_ary = ( 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ,31 ); $month_ary[2]++ if (($year%4) == 0); # leap year print"\$month_ary[2]=$month_ary[2] \n"; $month = 1; $day = $in_ydays; LOOP_MONTH: foreach (@month_ary) { print "\$month_ary[$month] = $month_ary[$month] \n"; if ( $day < $month_ary[$month] ) { if ( $day == 0 ) { $month--; $day = $month_ary[$month]; } last LOOP_MONTH; } print("Izmet: day = $day, month = $month\n"); $day -= $month_ary[$month++]; } my $month_diff = $month - $current_month; print("Izmet: month = $month, current_month = $current_month\n"); #### -11 if behind current processing timestamp #### 11 if ahead of timestamp.
print "\$year = $year \n"; if ( $month_diff eq 11 ) { $year -= 1; } elsif ( $month_diff eq -11 ) { $year += 1; } print("Izmet: month = $month\n"); print("Izmet: year = $year, month = $MONTH_MAP[$month], day = $day\n"); ## day, month & year roll{back|over}. ### how to rollover/rollback timestamp if datafile timestamp is ahead/behind current processing time (curren_date, current_mo nth, year) ### datafile timestamp - in_ydays ### expected output: ### Processing time: ---> datafile timestamp (day of the year) ## GMT - ## 1/1/06 ---> 365 (actually for 31/12/2005) (rollback to year 2005 from the current year of 2006) ## GMT + ## 31/12/05 ---> 1 (actually for 1/1/2006) (rollover to year 2006 i.e. 1/1/06 from current year of '05) Script output: bash-2.03$ ./test.pl $month_ary[2]=28 $month_ary[1] = 31 Izmet: day = 365, month = 1 $month_ary[2] = 28 Izmet: day = 334, month = 2 $month_ary[3] = 31 Izmet: day = 306, month = 3 $month_ary[4] = 30 Izmet: day = 275, month = 4 $month_ary[5] = 31 Izmet: day = 245, month = 5 $month_ary[6] = 30 Izmet: day = 214, month = 6 $month_ary[7] = 31 Izmet: day = 184, month = 7 $month_ary[8] = 31 Izmet: day = 153, month = 8 $month_ary[9] = 30 Izmet: day = 122, month = 9 $month_ary[10] = 31 Izmet: day = 92, month = 10 $month_ary[11] = 30 Izmet: day = 61, month = 11 $month_ary[12] = 31 Izmet: day = 31, month = 12 $month_ary[13] = 31 Izmet: month = 12, current_month = 1 $year = 2005 Izmet: month = 12 Izmet: year = 2004, month = Dec, day = 31 --------------------------------- Yahoo! Shopping Find Great Deals on Holiday Gifts at Yahoo! Shopping