Visit Our UNIX and Linux User Community


Perl script to toggle through dates by week


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script to toggle through dates by week
# 1  
Old 10-27-2009
MySQL Perl script to toggle through dates by week

Hi,
I need help to toggle through dates on a weekly basis to be fed into a script as inputs. The format should be: yyyy/mm/dd (start) yyyy/mm/dd (end), where end date is 7 days increments.

The date (start) would be input as an ARGV and would continue until current date.

I can check current date as follows:
Code:
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime time;
$year += 1900;
$mon += 1;

is there any perl function that would toggle through by a week.

And if the Start Date is say Not Sunday, then the start date should be adjusted to the next nearest Sunday.

thank you
Subha

---------- Post updated at 06:57 PM ---------- Previous update was at 12:23 PM ----------

#!/usr/bin/perl -w
use strict;
use warnings;
use Time::Local;

if ($#ARGV < 0)
{
die "Usage dates <input arg> : format is yyyy/mm/dd\n ";
exit (1);
}

my $start_date = $ARGV[0];
my $time = 0;
my $end_time = time;
print "Start Date: $start_date \n";
print "End Date: $end_time \n";

my @dates = split ('/', $start_date);
my $syyyy = $dates[0];
my $smm = $dates[1];
my $sdd = $dates[2];
$smm = $smm - 1;
$time = timelocal(0,0,0,$sdd,$smm,$syyyy);

print "Start time entered:", scalar(localtime($time)), "\n";

while ($time < $end_time)
{
$time += 7 * 24 * 60 * 60;
my ($isec, $imin, $ihour, $idd, $imm, $iyyyy, $iwday, $iyday, $iisdst) = localtime($time);
$iyyyy += 1900;
$imm += 1;
print "Time toggle: $iyyyy $imm $idd \n";
}

Last edited by pludi; 10-27-2009 at 03:36 PM.. Reason: code tags, please...
# 2  
Old 10-27-2009
If you have GNU date you could perhaps also do it like this:
Code:
for i in {0..6}; do 
  date -d "091028 $i days " '+%Y/%m/%d'
done|xargs

2009/10/28 2009/10/29 2009/10/30 2009/10/31 2009/11/01 2009/11/02 2009/11/03

# 3  
Old 10-27-2009
use Date::Calc , that is efficient.

Lemme know if you have any trouble using it or need an example.

HTH,
PL

Previous Thread | Next Thread
Test Your Knowledge in Computers #879
Difficulty: Easy
Supervisory Control and Data Authorization (SCADA) is a control system architecture .
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Splitting week start date and end date based on custom period start dates

Below are my custom period start and end dates based on a calender, these dates are placed in a file, for each period i need to split into three weeks for each period row, example is given below. Could you please help out to achieve solution through shell script.. File content: ... (2 Replies)
Discussion started by: nani2019
2 Replies

2. Shell Programming and Scripting

Perl : Module to get the last week date and time

Hello folks, I am looking for a Perl module or a program logic that gives the startdate and enddate of the last week.. Suppose say, assuming this week starts from Sunday to Saturday If I execute the script in this week. I need to get the last week sunday's date. Could anyone please... (1 Reply)
Discussion started by: scriptscript
1 Replies

3. Shell Programming and Scripting

Need Help:Shell Script for Solaris to change the dates in a file by one week

I have to increase the date by one week in an input when script is executed in solaris. I was able to acheive this using ksh script that is working in Linux enivironment, when i execute the same script in Solaris i am getting below error: /var/tmp\n\r-> ./script.ksh date: illegal option -- d... (3 Replies)
Discussion started by: sriramanaramoju
3 Replies

4. Linux

Get all the files from a FTP location with previous week's dates in the file names using Linux

I have a weird requirement where I have to get the files from a FTP(Lets say FTP1) location and place it on my current FTP(Lets say FTP2) location. The issue is, these are daily files (in a pattern Sales_YYYYMMDD_report.csv) and are placed every day on FTP1 and my process usually runs on Monday(eg.... (2 Replies)
Discussion started by: dhruuv369
2 Replies

5. Shell Programming and Scripting

Sorting dates in Perl

I have a directory of backup files. named like this: ldap.data.04-06-2012.tar ldap.data.03-06-2012.tar ldap.data.02-06-2012.tar ldap.data.01-06-2012.tar ldap.data.31-05-2012.tar ldap.data.30-05-2012.tar ldap.data.29-05-2012.tar ldap.data.28-05-2012.tar ldap.data.27-05-2012.tar... (6 Replies)
Discussion started by: robsonde
6 Replies

6. Shell Programming and Scripting

Subtracting two dates in PERL

Hi guys, First of all, I would like to say this is my first post in the unix.com forums. I am a beginner in PERL and have only started writing my first scripts. With that out of the way, I have a question regarding the calculation of time dates in PERL. I have two scalar variables with the... (1 Reply)
Discussion started by: DiRNiS
1 Replies

7. Shell Programming and Scripting

Perl difference between dates

Hi, Is there any way I can get the difference between two dates in terms of days? I have used this method so far, but I cant format it in terms of days. @a=&DateCalc($date1,$date2,0); The o/p that I am getting is sort of like this: +0:0:0:4:0:0:0 I just want to get 4 days as an o/p.... (1 Reply)
Discussion started by: King Nothing
1 Replies

8. Shell Programming and Scripting

Need script to generate all the dates in DDMMYY format between 2 dates

Hello friends, I am looking for a script or method that can display all the dates between any 2 given dates. Input: Date 1 290109 Date 2 010209 Output: 300109 310109 Please help me. Thanks. :):confused: (2 Replies)
Discussion started by: frozensmilz
2 Replies

9. UNIX for Dummies Questions & Answers

How to find Day of the Week from the given date (Perl)?

How to find the Day of the Week of the given Date using perl? If I have a date in YYY--MM-DD format, how to find the DOW? Based on that, I need to find the following sunday. Pls help. (5 Replies)
Discussion started by: deepakwins
5 Replies

10. Shell Programming and Scripting

Help w/ Perl dates

I need to create 12 variables, the first of which is the date of the first day of the current month (01/01/2006), and the remaining 11 are to equal each month after the current. var1 = 01/01/2006 var2 = 02/01/2006 var3 = 03/01/2006 var4 = 04/01/2006 etc. How can I easily do this is in... (7 Replies)
Discussion started by: ssmiths001
7 Replies

Featured Tech Videos