Date Operations
Some notes on the basic date operations available in Perl.
Date Command
The "date" command in Perl fetches the current date and time from the system. It can be executed like this ...
$var = `date`;
Which loads the variable with a string containing the date:
Wed Mar 3 05:55:42 MST 2002
There's an invisible "newline" character at the end of the date, which may need to be chomped off.
It's useful for a quick-and-dirty date stamp, and while you could parse the string to fetch the day, month, year, hour, etc., the local time function (below) provides an easier way of doing that.
Localtime Function
Perl has a built in function that will return a date as an array of values:
@now = localtime;
Having done that, you can now access the individual components of the date as an array:
- $now[0] - Seconds, as a number from 0 to 59
- $now[1] - Minutes, as a number from 0 to 59
- $now[2] - Hours, as a number from 0 to 23 (military time)
- $now[3] - Day of the Month, as a number from 1 to 31
- $now[4] - Month, as a number from 0 to 11
- $now[5] - Year, as a number of years since 1900
- $now[6] - Day of Week, as a number from 0 (Sunday) to 6 (Saturday)
- $now[7] - Day of Year, as a number from 0 to 365 (days elapsed since January 1 of the current year)
- $now[8] - A flag, 0 or 1, indicating whenter daylight savings time is active
The month and day of week are offset by one (month 1 is February, not January), which makes it easier to coordinate with an array of literal values, but a bit of a bugger if you're just using the number.
The locatime array reflects the time set on the server. You can use gmtime instead to get Zulu time, but that is derived from the server's internal clock as well.
And finally
This is online for my own use and reference, but feel to snag it if you think it would be useful. It's a trifle and I don't expect to be credited or compensated in any way ... but nor does it come with any sort of guarantee.