Archive

Posts Tagged ‘date array’

Perl – Get current time without perl packages


Although perl has a built in package for getting the DateTime, you cannot guarantee that the system you are using has this package installed. Therefore in this situation it would be necessary to put the localtime into an array and work with it from there. The below script shows how to do this:

### initialize datetime my %DTTIME = ();
my ($SEC,$MIN,$HOUR,$MDAY,$MON,$YEAR,$WDAY,$YDAY,$ISDST) = localtime(time);
### format numbers
$DTTIME{year }  = sprintf "%04d",($YEAR + 1900); ## 4 digits to specify the year
$DTTIME{mon  }  = sprintf "%02d",($MON + 1); ## zeropad months
$DTTIME{mday }  = sprintf "%02d",$MDAY; ## zeropad day of the month
$DTTIME{wday }  = sprintf "%02d",$WDAY + 1;  ## zeropad day of week;sunday = 1;
$DTTIME{yday }  = sprintf "%02d",$YDAY; ## zeropad nth day of the year
$DTTIME{hour }  = sprintf "%02d",$HOUR; ## zeropad hour
$DTTIME{min  }  = sprintf "%02d",$MIN;  ## zeropad minutes
$DTTIME{sec  }  = sprintf "%02d",$SEC;  ## zeropad seconds
$DTTIME{isdst}  = $ISDST;

If you wished to then use this you could do so as follows:

print "Date = $DTTIME{mday} - $DTTIME{mon} - $DTTIME{year}";

Advertisements
Categories: Perl Tags: , , ,