1831185 Members
2942 Online
110021 Solutions
New Discussion

help in perl script

 
SOLVED
Go to solution
unix_admin
Occasional Advisor

help in perl script

My PERL script reports dates of 01/20/100. I used the printf format
$02d on the year value, but I still get year = 100.

Why am I having this problem?
2 REPLIES 2
Radhakrishnan Venkatara
Trusted Contributor
Solution

Re: help in perl script

You are getting dates of 01/20/100 because the localtime() return value
for year is currently 100 for the year 2000. The printf format for
decimal conversion (%02d) will prepend leading zeroes, but does not
truncate to two digits because that would modify the actual value.

'printf' has no truncation capability, even %02s prints the entire
string, so dealing with two digit dates is a problem that requires
coding. Subtracting 100 would yield 00, but for the year 99, that would
result in a year of -01.

To resolve your problem, use 4 digit year, just add 1900 to the year.
This applies to any language using the tm struct returned by functions
such as localtime()

PERL example

#Current date (MM/DD/YYYY)
$TI'DATE = join ('/', $TI'MON + 1, $TI'MDY, $TI'YR + 1900);


radhakrishnan
Negative thinking is a highest form of Intelligence
H.Merijn Brand (procura
Honored Contributor

Re: help in perl script

Although this explanation is correct, the main point is missing: unix dates are returned in seconds since 01-01-1970, and then converted to an array representing the date parts. In that list, the year is represented in years since 1900, hence year 100 stands for 2000.

Furthermore please do NOT use the syntax as described in the example, but use the `normal' syntax instead:

# Current date (DD-MM-YYYY)
my @d = localtime time;
my $date = sprintf "%02d-%02d-%04d", $d[3], $d[2] + 1, $d[5] + 1900;

$TI'DATE is the *OLD* (and depricated) way to write $TI::date. Please do *NOT* use that syntax unless you are trying to write obfuscated scripts.

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn