Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generating the Dates between two Dates 1

Status
Not open for further replies.

rkumar28

MIS
Jan 30, 2005
15
US
Hi,
I am a newbie in perl. I will really appreciate any help and advice.

I have a Oracle table that contains quite a few Filenames. I am trying to write a perl script thats generates all the possible dates between two dates that is passed as a parameter to Perl script and concatenate these dates with FileNames.

For E.G.:
I grab one filename from Oracle table called "SMITH.sfs". I pass in two dates to a perl script 2006-06-01 and 2006-06-30. I have to generate the 30 dates one for each day between the above two dates that was passed in.
I am trying to get the output something like below and write it to a flat file:

SMITH.sfs.20060601
SMITH.sfs.20060602
SMITH.sfs.20060603
........and so on till
SMITH.sfs.20060630

Below is my script that works fine with the Oracle SYSDATE.....(This script is not passing the date right now). I have to modify this script to generate dates as mentioned above.

#!/usr/bin/perl
use DBI;
$dbh = DBI->connect("dbi:Oracle:sid...",user,passwd) ;
print "Connected Sucessfully to Oracle database \n"; \
$sql = q{
Select
ID,
File_Name||to_char(SYSDATE,'YYYYMMDD') As FILE_NAME
From TABLE A
Where D_Reg Like '%d{8}%'
UNION
Select
ID,
File_Name||to_char(SYSDATE,'YYYYMMDD') As FILE_NAME
From TABLE A
Where D_Reg Like '%d{6}%'
};
$stmt = $dbh->prepare($sql) or die "Cannot prepare the SQL";
$stmt->execute() or die "Cannot Excecute";

$rec = $stmt->fetchall_hashref('DATA_FL_ID');
for $DATA_FL_ID ( keys %$rec )
{
my $record = $rec->{ $DATA_FL_ID };
print $record->{'FILE_NAME'}, "\n"; -->THIS PRINTS THE FILENAMES....
}

I will really appreciate any help in this regards.

Thanks
RK
 
if you can pass in the dates as a start date, like 2006-06-01 and how many days to generate filenames for, like 30 days, including the start date, something like this might work:

Code:
use POSIX qw(strftime);
use Time::Local 'timelocal_nocheck';

[COLOR=red]# assumes we have the filename already[/color]
my $file = 'SMITH.sfs';

[COLOR=red]# here are your two passed in arguments[/color]
my $start_date = '2006-06-01';
my $days_to_add = '30';

[COLOR=red]# rearrange the date[/color]
$start_date =~ /(\d+)-(\d+)-(\d+)/;
my ($mday,$mon,$year) = ($3,$2,$1);

[COLOR=red]# convert to epoch time[/color]
my $date = timelocal_nocheck(0,0,0,
                             $mday-1,
                             $mon-1,
                             $year
                            );
[COLOR=red]# make the new filenames[/color]
for (1..$days_to_add) {
   my $new_date = strftime('%Y%m%d',0,0,0,
                           (localtime($date+=86400))[3,4,5]
                          );
   print "$file.$new_date\n";
}

There is probably a date module that can handle this type of work too. The above would require no more than perls core modules but since it's using POSIX your results might vary. You could do it without using POSIX but would need to adjust the code a bit.
 
You might ask why I did it like that instead of a simple incrementing of the day. The way I coded it you can have the start day as any arbitrary day of the month and add as many days as you want and still get the correct output. LIke if you started at 2006-06-17 and wanted to add 28 days the out put will still be OK:

SMITH.sfs.20060617
SMITH.sfs.20060618
SMITH.sfs.20060619
SMITH.sfs.20060620
SMITH.sfs.20060621
SMITH.sfs.20060622
SMITH.sfs.20060623
SMITH.sfs.20060624
SMITH.sfs.20060625
SMITH.sfs.20060626
SMITH.sfs.20060627
SMITH.sfs.20060628
SMITH.sfs.20060629
SMITH.sfs.20060630
SMITH.sfs.20060701
SMITH.sfs.20060702
SMITH.sfs.20060703
SMITH.sfs.20060704
SMITH.sfs.20060705
SMITH.sfs.20060706
SMITH.sfs.20060707
SMITH.sfs.20060708
SMITH.sfs.20060709
SMITH.sfs.20060710
SMITH.sfs.20060711
SMITH.sfs.20060712
SMITH.sfs.20060713
SMITH.sfs.20060714

If you *have* to use a range between two dates then one of the date manipulation modules would probably be easiest to use.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top