[ back to toc ]

Clean system time conversion to YYYY

Date: 2002/05/02 10:21

Q:
Good Morning, Peter.

I am running Perl 5 on a UNIX box and have recently gotten a new customer
with a Perl based conference registration system, see:
http://www.upassoc.org/cgi-sys/cgiwrap/upa/conf2002/reg/new_startreg.pl

I am new to Perl and have tried about 10 different scripts I have found on
the net to take the system time and convert it to MM/DD/YYYY - with YYYY
in a 2002 format -- everything I try comes out as 5/1/102 - the 102
instead of 2002 is the part I cannot fix.

Would you have any ideas?

I also have a serious bug involving pattern matching on this same site.
How is your expertise in Perl pattern matching?

Thanks!

*NAME-DELETED* *NAME-DELETED*
jim@jclauson.com
A:
Here I include some date handling scripts that in 1998.

Regarding your other question: I am regexp God. (Well, a bit exagarated.)
Ask brave.

Regards,
Peter

@mdays = ( '31', '28', '31', '30', '31', '30', '31', '31', '30', '31',
'30', '31' );

sub dysize {
local($y,$yr) = (@_[0],365) ;
return -1 if ($y < 0 ) ;
return 347 if ($y == 1752);
$yr = 366 if((($y % 4) == 0) && ( ($y % 100) || (($y % 400)==0) )) ;
return $yr ;
}

sub t2Date {
my ($time) = shift;
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
localtime($time);

$year += 1900;

$mon++;
if( $mon <10 ){ $mon = "0" . $mon;}
if( $mday <10 ){ $mday = "0" . $mday;}

return "$year/$mon/$mday";
}

sub d2Time {
my ($ds);
my ($year,$month,$day);

$_ = shift;
$ds = 0;

/([0-9]+)\/([0-9]+)\/([0-9]+)/;

$year = $1-1; $month = $2-1; $day = $3-1;

for (1970 .. $year ) { $ds += &dysize($_) ; }
for ( 0 .. $month-1 ){ $ds += $mdays[$_] ; }

if( &dysize($year+1) == 366 && $month >1 ){ $ds++; }

$ds += $day;

return ($ds*86400) ;
}

[ back to toc ]