Date function

K

K.J. 44

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.
 
K

K.J. 44

I will try that. Thank you!

Sharif said:
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"
 
B

boyd

K.J. 44 said:
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
 
G

Gunnar Hjalmarsson

Sharif said:
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.
 
G

Gunnar Hjalmarsson

boyd said:
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.

What about DST? ;-)
 
B

boyd

Gunnar Hjalmarsson said:
boyd said:
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.

What about DST? ;-)

Good point. So my method would mess up on one of the DST changeovers if
it ran between midnight and 0100 local. Is that right? What is the
"40000" for in your script?

Boyd
 
G

Gunnar Hjalmarsson

boyd said:
boyd said:
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.

What about DST? ;-)

Good point. So my method would mess up on one of the DST changeovers if
it ran between midnight and 0100 local. Is that right?

Yeah, that's what I'm thinking of. Maybe not a disaster in this case,
but always worth considering when dealing with dates.
What is the "40000" for in your script?

That's 'approximately' 12 hours, i.e. well enough time to make the
result DST safe. :)
 
B

boyd

Gunnar Hjalmarsson said:
boyd said:
boyd wrote:
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.

What about DST? ;-)

Good point. So my method would mess up on one of the DST changeovers if
it ran between midnight and 0100 local. Is that right?

Yeah, that's what I'm thinking of. Maybe not a disaster in this case,
but always worth considering when dealing with dates.
What is the "40000" for in your script?

That's 'approximately' 12 hours, i.e. well enough time to make the
result DST safe. :)

Thanks. I have another question. I administer a Sun network and one
Linux box at work, both of which are isolated from the world ( I have to
hand carry in patches, etc. ) So the upcoming DST rule change may be a
problem. I'm sure you are aware that next March, DST starts on, IIRC,
Mar. 11, and ends the first weekend in November. What's the best way to
deal with the changes? (at home I have Ubuntu and Mac OS X )

Boyd
 
G

Gunnar Hjalmarsson

boyd said:
I administer a Sun network and one
Linux box at work, both of which are isolated from the world ( I have to
hand carry in patches, etc. ) So the upcoming DST rule change may be a
problem. I'm sure you are aware that next March, DST starts on, IIRC,
Mar. 11, and ends the first weekend in November. What's the best way to
deal with the changes? (at home I have Ubuntu and Mac OS X )

I'm anything but a DST expert, but don't all OS distros come with
current info about switches to/from DST worldwide? I didn't know about
the change you are referring to, maybe because I live in Sweden and
those dates vary between countries, but if there was a late decision in
your country, isn't there simply a patch for each OS available?

Hopefully somebody is able to give you a more authoritative comment. :)
 
J

J. Gleixner

K.J. 44 said:
I am getting an error saying that it cannot find Date:Format in my
library so I need to add it in. I have never installed a Perl Module
and am not exactly sure where to start. I was at:

http://www.perl.com/CPAN/modules/01modules.index.html

I am not sure how to find what i need. Thanks for any help.

First, since this is a separate question, use a more appropriate
subject, e.g. "How to install modules". Second, please stop top-posting.
Third, to find the module, take a look at http://search.cpan.org/.
Finally, to answer your question on how to install it, read the
documentation for your installation or search the Internet on something
relevant, such as 'perl "how to install module"', and you'd likely have
found helpful documentation already.

ActiveState:
http://aspn.activestate.com/ASPN/docs/ActivePerl/5.8/faq/ActivePerl-faq2.html

*nix: perldoc -q "How do I install a module from CPAN"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top