Computing.Net > Forums > Unix > increase year, month,day in a loop

increase year, month,day in a loop

Reply to Message Icon

Original Message
Name: anhdu
Date: February 14, 2004 at 09:27:38 Pacific
Subject: increase year, month,day in a loop
OS: UNIX
CPU/Ram: do not know
Comment:

Please give me some help on this shell script PLEASE. I'm very appreciate for your help.
I need a script to run with two parameters: from-date (from-YYYYMMDD) and to-date (to-YYYYMMDD)
the command to run will be:
myScript YYYYMMDD YYYYMMDD
(ex: myscript 20031229 20040103)

I want to get every day in this range and assign to a variable name dayIN and display $dayIN, so will do something else later.
(if is possible, May I have a leap year pleae? If not, that is Ok for now)

I want to get the output as:
20031229
20031230
20031231
20040101
20040102
20040103


THANK YOU VERY MUCH



Report Offensive Message For Removal


Response Number 1
Name: nails
Date: February 14, 2004 at 15:03:39 Pacific
Reply: (edit)

Hi:

This is very much like William Robertson's most excellent shift_date script. You can obtain the link from this post:

http://www.computing.net/unix/wwwboard/forum/5648.html

Regards,


Nails



Report Offensive Follow Up For Removal

Response Number 2
Name: WilliamRobertson
Date: February 14, 2004 at 16:31:04 Pacific
Reply: (edit)

Thanks Nails!

Using shift_date, you could write something like:

#!/bin/ksh
start_date=$1 end_date=$2
test_date=${start_date}

(( test_date <= end_date )) && print ${start_date}

while (( test_date < end_date ))
do
    test_date=$(shift_date ${test_date} +1)
    print ${test_date}
done


Report Offensive Follow Up For Removal

Response Number 3
Name: anhdu
Date: February 15, 2004 at 20:04:39 Pacific
Reply: (edit)

Hi Nails and William,
I'm a beginner in shell script. I downloaded William Robertson's shift_date script. I tried to study and understand it. But it is difficult for my level. If I narrow the date from 20031001 to 20040215. Is there any way easier in the shell script that I can understand? PLEASE give some help PLEASE? I'm really appreciate for all your help. Thank you


Report Offensive Follow Up For Removal

Response Number 4
Name: WilliamRobertson
Date: February 15, 2004 at 23:22:23 Pacific
Reply: (edit)

Well, there is probably a Perl module that provides a library of date functions, but whether that would be easier or not I don't know.

There is some more info about how shift_date works at www.unixreview.com where they featured in in the Shell Corner column.


Report Offensive Follow Up For Removal

Response Number 5
Name: thepubba
Date: February 17, 2004 at 17:49:49 Pacific
Reply: (edit)

First off, you are going to have to split your date ranges into something like:

YYYY MM DD YYYY MM DD

This will avoid awk'ing the 2 input parameters to split them into working numbers.

Figuring out if February is a leap year is easy. Unix has the cal command. When your incremented month equals 2, do something like this:

for Date in $(cal 02 2004)
do
:
done
print $Date

The value of $Date will always be the last day of the month. You can do this for any month, but February is the only one that floats.

If you want to calculate the julian date, then William Robertson's script is the way to go. I also posted a julian converter script in this discussion group some time ago, with logic that is similar to what is in William's script.

The script you want is fairly easy to write, but is not simple. It also will not be short because of the math in computing the dates. If I was to try and do what you want, I'd put all the dates into an array or write them to a file and read them back in.

You logic will be:

Validate input parameters.

Get the start month and day.

Create your variables or output to a file.

Increment month. Test if month becomes greater than 12, change to 1 and increment year (if appropriate).

Continue with previous steps.

You will constantly need to check your dates against the ending range.

Have fun.


Report Offensive Follow Up For Removal


Response Number 6
Name: FishMonger
Date: February 17, 2004 at 22:26:15 Pacific
Reply: (edit)

Since William suggested the possibility of using Perl, I thought I'd give my 2 cents. Here's a Perl script that will do exactly what you asked. The underling code for the Date::Manip module is longer (over 7300 lines) and more complex than Williams shift_date script, but it's also more versitile.

#!/usr/bin/perl -w

use Date::Manip;

Date_Init("TZ=PST");
$date = ParseDate(shift);
$end = ParseDate(shift);

while ($date <= $end) {
print UnixDate($date, "%Y%m%d\n");
$date = DateCalc($date, " + 1 day");
}


Report Offensive Follow Up For Removal

Response Number 7
Name: FishMonger
Date: February 17, 2004 at 22:28:43 Pacific
Reply: (edit)

BTW, that Perl script works with leap years.


Report Offensive Follow Up For Removal

Response Number 8
Name: WilliamRobertson
Date: February 21, 2004 at 07:05:35 Pacific
Reply: (edit)

The Perl module looks interesting. I guess that although it's more complex internally, the fact that it's a downloadable module means you can use it anywhere without having to look inside it. I really should try learning Perl one of these days.

btw, shift_date doesn't return a Julian date, but a date string in the format YYYYMMDD. It uses a Julian-like conversion internally to do the addition/subtraction (picking a convenient start date to save itself some work) then converts back before returning the result.



Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: increase year, month,day in a loop 

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge