You probably want to use Perl's built in 'localtime'
function rather than system("date").
Here's an example:
my($day, $month, $year) = (localtime())[3,4,5];
my $a = sprintf("%04u-%02u-%02u", $year + 1900, $month + 1, $day);
Here's a walkthrough ...
You can get the current time using time(). This function returns a
single integer being the number of seconds since January 1st 1970.
The number is also in GMT.
The localtime() function takes a time value and splits it into its
component parts (seconds, minutes, hours, day, month, year and more)
for your local time zone and returns these values as a list. If you
don't give localtime() a time value, it calls time().
In the sample code above, I only wanted the day, month and year so
I asked for elements 3, 4 and 5 of the returned list (skipping
elements 0, 1 and 2).
There are two gotchas with the returned values (these are due to the
way the underlying operating system library functions work rather
than Perl weirdisms).
The month numbers start at zero for January. This is useful if you
want to use the number to index into an array of month names but if
you want a numeric value, you'll need to add 1.
The year number starts at 0 for 1900 so this year it's returned as
101. To get a standard 4 digit year, add 1900.
I prefer my dates in the YYYY-MM-DD form when adding them in to
filenames - that way an alphabetical sort is also a chronological
sort. I used the sprintf() function to add in the required leading
zeros.
Regards
Grant
> -----Original Message-----
> From: Mark Martin [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, 1 May 2001 22:19
> To: [EMAIL PROTECTED]
> Subject: Date stamping files
>
>
> Does anybody know how to clean up the system time :
>
> Tue May 1 10:18:32 BST 2001
>
> or select only part of it to give a more managable date
> format that can be
> used to stamp an output file from a program :
>
> $a = system ("date"); # OR A TRUNCATED VERSION????
>
> # OR CLEAN IT UP HERE ???
>
> $b = "filename.$a.txt"; # NOT SO SURE ABOUT THIS EITHER???
>
> open (OUT2,">$b");
>
> Thanks a lot!
> _____________________________________________
>
> Mark Martin
> Computer Centre
> National University of Ireland Maynooth
>
> Tel: (01)708 4716/3830
> Fax: (01)628 6249
>