[prog] Perl dates

Jacinta Richardson jarich at perltraining.com.au
Mon Feb 17 12:10:49 EST 2003


> I have been trying to figgure out how to get a date in the form "mmyyyy" but
> haven't succeeded. I can get a beutifully formatted string using the unix
> `date` command, but that is not what I want this format to name log files.
> 
> The only perl book that i have at the moment is "perl in 24 hrs" ~ or
> something to that effect.  I may have to get a better book, when I can
> afford it.

Remember perldoc.  Perl man pages...

% perldoc -f time

       time    Returns the number of non-leap seconds since what­
               ever time the system considers to be the epoch
               (that's 00:00:00, January 1, 1904 for MacOS, and
               00:00:00 UTC, January 1, 1970 for most other sys­
               tems).  Suitable for feeding to "gmtime" and
               "localtime".

               For measuring time in better granularity than one
               second, you may use either the Time::HiRes module
               from CPAN, or if you have gettimeofday(2), you may
               be able to use the "syscall" interface of Perl,
               see the perlfaq8 manpage for details.

% perdoc -f localtime

       localtime EXPR
               Converts a time as returned by the time function
               to a 9-element list with the time analyzed for the
               local time zone.  Typically used as follows:

                   #  0    1    2     3     4    5     6     7     8
                   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =

localtime(time);

               All list elements are numeric, and come straight
               out of the C `struct tm'.  $sec, $min, and $hour
               are the seconds, minutes, and hours of the speci­
               fied time.  $mday is the day of the month, and
               $mon is the month itself, in the range "0..11"
               with 0 indicating January and 11 indicating Decem­
               ber.  $year is the number of years since 1900.
               That is, $year is "123" in year 2023.  $wday is
               the day of the week, with 0 indicating Sunday and
               3 indicating Wednesday.  $yday is the day of the
               year, in the range "0..364" (or "0..365" in leap
               years.)  $isdst is true if the specified time
               occurs during daylight savings time, false other­
               wise.

		.....


So...

	my $time = time();
	my ($month, $year) = (localtime($time))[4,5];

	my $displaydate = $month . ($year + 1900);

Done!
The clever person will note that $time isn't needed there since localtime
given no arguments assumes the current time, but I did this just in case
you're dealing with a different timestamp.

You'll notice that we take an array slice of the result of local time.
This is because it's kind of silly to create variables for the seconds,
minutes, hours etc if you don't need them.  Doing it this way says "only
give us the values in positions 4 and 5.  All the other positions are
calculated be we don't need to worry about that.

good luck with it.

	Jacinta


--
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson	    |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +613 9354 6001 	    |  
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
(il),-''  (li),'  ((!.-'              |   www.perltraining.com.au   |




More information about the Programming mailing list