Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Date function

Reply
Thread Tools

Date function

 
 
K.J. 44
Guest
Posts: n/a
 
      11-28-2006
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.

 
Reply With Quote
 
 
 
 
K.J. 44
Guest
Posts: n/a
 
      11-28-2006
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"


 
Reply With Quote
 
 
 
 
boyd
Guest
Posts: n/a
 
      11-28-2006
In article <(E-Mail Removed). com>,
"K.J. 44" <(E-Mail Removed)> 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
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      11-28-2006
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
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      11-28-2006

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.


Or just the POSIX module's strftime().

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      11-28-2006
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?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
boyd
Guest
Posts: n/a
 
      11-28-2006
In article <(E-Mail Removed)>,
Gunnar Hjalmarsson <(E-Mail Removed)> wrote:

> 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? What is the
"40000" for in your script?

Boyd
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      11-28-2006
boyd wrote:
> In article <(E-Mail Removed)>,
> Gunnar Hjalmarsson <(E-Mail Removed)> wrote:
>>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.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
boyd
Guest
Posts: n/a
 
      11-28-2006
In article <(E-Mail Removed)>,
Gunnar Hjalmarsson <(E-Mail Removed)> wrote:

> boyd wrote:
> > In article <(E-Mail Removed)>,
> > Gunnar Hjalmarsson <(E-Mail Removed)> wrote:
> >>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
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      11-28-2006
boyd wrote:
> 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.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Given a date, how to find the beginning date and ending date of that week Matt ASP General 11 11-08-2003 11:24 PM
Given a date, how to find the beginning date and ending date of that week Matt ASP .Net 1 11-08-2003 09:14 PM
Given a date, how to find the beginning date and ending date of that week Matt C Programming 3 11-08-2003 09:07 PM
Given a date, how to find the beginning date and ending date of that week Matt C++ 2 11-08-2003 08:30 PM



Advertisments