From: K.J. 44 on
Hi,

is there a date function in Perl or do I have to try to find a module?

What i need is to be able to come up with the current date, then
subtract one day, and add that to the file name in the form of:

YYYY-MM-DD-RestOfFileName.txt

The script I have written parses these files and I want to run it every
night after midnight on the previous days file which has the naming
convention above.

I am running ActiveState Perl in a Windows environment.

Thanks.

From: Sharif Islam on
K.J. 44 wrote:
> Hi,
>
> is there a date function in Perl or do I have to try to find a module?
>
> What i need is to be able to come up with the current date, then
> subtract one day, and add that to the file name in the form of:
>
> YYYY-MM-DD-RestOfFileName.txt
>
> The script I have written parses these files and I want to run it every
> night after midnight on the previous days file which has the naming
> convention above.
>
> I am running ActiveState Perl in a Windows environment.


You can use the Date::Format module.

use strict
use Date::Format;
my $yesterday = time() - ( 24 * 60 * 60 );
my $prefix= time2str("%Y-%m-%d", $yesterday);
# YYYY-MM-DD-RestOfFileName.txt
print $prefix."-RestOfFileName.txt"
From: K.J. 44 on
I will try that. Thank you!

Sharif Islam wrote:
> K.J. 44 wrote:
> > Hi,
> >
> > is there a date function in Perl or do I have to try to find a module?
> >
> > What i need is to be able to come up with the current date, then
> > subtract one day, and add that to the file name in the form of:
> >
> > YYYY-MM-DD-RestOfFileName.txt
> >
> > The script I have written parses these files and I want to run it every
> > night after midnight on the previous days file which has the naming
> > convention above.
> >
> > I am running ActiveState Perl in a Windows environment.
>
>
> You can use the Date::Format module.
>
> use strict
> use Date::Format;
> my $yesterday = time() - ( 24 * 60 * 60 );
> my $prefix= time2str("%Y-%m-%d", $yesterday);
> # YYYY-MM-DD-RestOfFileName.txt
> print $prefix."-RestOfFileName.txt"

From: boyd on
In article <1164750487.485271.239390(a)80g2000cwy.googlegroups.com>,
"K.J. 44" <Holleran.Kevin(a)gmail.com> wrote:

> Hi,
>
> is there a date function in Perl or do I have to try to find a module?
>
> What i need is to be able to come up with the current date, then
> subtract one day, and add that to the file name in the form of:
>
> YYYY-MM-DD-RestOfFileName.txt
>
> The script I have written parses these files and I want to run it every
> night after midnight on the previous days file which has the naming
> convention above.
>
> I am running ActiveState Perl in a Windows environment.
>
> Thanks.

There are built-in functions and modules, and numerous ones in the CPAN
library. I usually build this kind of thing myself, finding it quicker
than trying to find the right module.

something like:

my( $day, $mon, $yr ) = ( localtime( time - 24*3600) )[3, 4, 5];
$yr += 1900;
$mon += 1;
my $str = sprintf '%4d-%02d-%02d', $yr, $mon, $day;

would give you the prefix string for your filename.

Boyd
From: Gunnar Hjalmarsson on
Sharif Islam wrote:
> You can use the Date::Format module.
>
> use strict
> use Date::Format;
> my $yesterday = time() - ( 24 * 60 * 60 );
> my $prefix= time2str("%Y-%m-%d", $yesterday);
> # YYYY-MM-DD-RestOfFileName.txt
> print $prefix."-RestOfFileName.txt"

Or you can stick to the builtin functions and the standard module
Time::Local:

use Time::Local;
my $midnight = timelocal 0, 0, 0, (localtime)[3..5];
my ($d, $m, $y) = (localtime $midnight-40000)[3..5];
print 'Yesterday: ',
sprintf('%d-%02d-%02d', $y+1900, $m+1, $d), "\n";

Note that Sharif's solution doesn't address the DST problem.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 |  Next  |  Last
Pages: 1 2 3
Prev: Perl/Mail Suggestion....
Next: using DateTime object