jim.shamlin.com

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:

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.