Click here to get back home

perl date manipulation

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
perl date manipulation siddhartha mulpuru 10-21-2004
Get Chitika Premium
Posted by siddhartha mulpuru on October 21, 2004, 5:02 pm
Please log in for more thread options
Hi,

I need to generate all the dates between a range of dates eg.. I need
all the dates between date A say 1/1/2004 and date B say 3/3/2004. Can
i get some code samples how i can implement this?

I had been looking for any date time modules that are installed but i
am not sure if there are any , below is the list of modules installed

Archive::Tar -- 1.04
CGI -- 3.00
CPAN -- 1.76
Compress::Zlib -- 1.22
Convert::BER -- 1.31
Devel::CoreStack -- 1.3
Devel::Symdump -- 2.03
Digest::MD5 -- 2.27
File::Spec -- 0.85
HTML::Parser -- 3.31
HTML::Tagset -- 3.03
IO-stringy -- ???
MIME-tools -- ???
MIME::Base64 -- 2.20
MIME::Lite -- 3.01
Mail -- ???
Net -- ???
Net::Telnet -- 3.03
Parse::RecDescent -- 1.94
Perl -- 5.8.0
Spreadsheet::WriteExcel -- 0.41
Term::ReadKey -- 2.21
Term::ReadLine -- 1.00
URI -- 1.25
libwww-perl -- ???


If possible i would like something to work using these modules since i
do not have the admin rights and a new module installation would take
a lot of time to get through.

Thanks so much for your time and appreciate your time

Sid


Posted by Bill Karwin on October 21, 2004, 5:42 pm
Please log in for more thread options
siddhartha mulpuru wrote:
> I need to generate all the dates between a range of dates eg.. I need
> all the dates between date A say 1/1/2004 and date B say 3/3/2004. Can
> i get some code samples how i can implement this?

Every Perl distro should have the POSIX module.

use POSIX;

# These are in Day, Month, Year order
my @start = (1, 1, 2004);
my @end = (3, 3, 2004);

# Make adjustments, see `man localtime'
$start[1] -= 1;
$start[2] -= 1900;
$end[1] -= 1;
$end[2] -= 1900;

my $time = POSIX::mktime(0, 0, 0, @start);
while(1)
{
print("Date = ", POSIX::ctime($time));
$time += 24*60*60;
my @tm = POSIX::localtime($time);
last if ($tm[5] > $end[2] ||
         ($tm[5] == $end[2] && $tm[4] > $end[1] ||
         ($tm[4] == $end[1] && $tm[3] > $end[0])));
}


Posted by Gunnar Hjalmarsson on October 22, 2004, 3:42 am
Please log in for more thread options
siddhartha mulpuru wrote:
> I need to generate all the dates between a range of dates eg.. I need
> all the dates between date A say 1/1/2004 and date B say 3/3/2004. Can
> i get some code samples how i can implement this?
>
> I had been looking for any date time modules that are installed but i
> am not sure if there are any , below is the list of modules installed

<very short list of modules snipped>

That list appears to consist of modules that have been installed
*besides* the standard modules. I hope (and suspect) that the standard
module Time::Local is installed (or else you'd better install it).

You should be able to write the code by help of the timegm() function in
Time::Local and the core Perl function gmtime(). (Better than
timelocal() and localtime() because of DST.) Check out the docs for
those functions.

Also, I imagine that a C-style for loop might be useful when creating
the list:

for ( my $t = $start; $t <= $end; $t += 86400 ) {
my ($d, $m, $y) = (gmtime $t)[3..5];
push @dates, join '/', $m+1, $d, $y+1900;
}

Btw, you should consider using an ISO compliant and non-ambigous date
format: 2004-01-01.

Give it a try, and come back here if you encounter difficulties that you
can't solve by help of the docs.

Good luck!

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


Similar ThreadsPosted
cron manipulation and perl-cgi September 20, 2004, 5:02 pm
MS SQL server and SQL DATETIME manipulation in perl April 29, 2005, 2:23 am
Windows Registry manipulation using Unix April 29, 2005, 11:46 am
Excel file manipulation in HPUX system October 14, 2004, 8:13 am
Date::MSAccess V 1.01 July 29, 2004, 7:15 am
problems with Date-Calc July 8, 2004, 3:09 pm
Date::Calc install September 8, 2004, 10:02 am
ANNOUNCE: Date::MSAccess V 1.02 February 14, 2005, 10:44 am
Date::Simple confusion May 8, 2005, 6:10 pm
Date::Calc Problem February 9, 2006, 12:25 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap