Some notes on the basic date operations available in Perl.
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.
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 - Seconds, as a number from 0 to 59
- $now - Minutes, as a number from 0 to 59
- $now - Hours, as a number from 0 to 23 (military time)
- $now - Day of the Month, as a number from 1 to 31
- $now - Month, as a number from 0 to 11
- $now - Year, as a number of years since 1900
- $now - Day of Week, as a number from 0 (Sunday) to 6 (Saturday)
- $now - Day of Year, as a number from 0 to 365 (days elapsed since January 1 of the current year)
- $now - 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.
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.