Date Intervals

I posted a question on date intervals about a month back asking about how I could be able to go about a user entering the starting year/month/day and an ending year/month/day and then the script automatically cycling through each day of each month of each year that the user has specified.

I checked out Perderabo's datecalc script and it was helpful but still not exactly what I needed.

I've thrown together some code today but I know that it's not going to work, I just need some suggestions on how I can complete this.

#!/bin/ksh

#                     Ja Fe Ma Ap My Jn Jl Au Se Oc No De
set -A DaysInMonth xx 31 28 31 30 31 30 31 31 30 31 30 31

isLeapYr=`date.isLeapYr.ksh`
if (( isLeapYr==1 )); then
    DaysInMonth[2]=29
fi

while (( start_date_Y < end_date_Y )); do

    while (( start_date_m < end_date_m )); do

        while (( start_date_d < ${DaysInMonth[${start_date_d}]} )); do
        (( start_date_d=start_date_d+1 ))
        done

        (( start_date_m=start_date_m+1 ))
    done

    (( start_date_Y=start_date_Y+1 ))
done

Some of my concerns are:
Take a look at the start_date_m < end_date_m loop (the 2nd while loop). What if the starting month is 4 and the ending month is 10 (but the user meant the 10 month of the 2nd year) then the loop will break prematurely.

How do I adjust for this?

I'm continuuing to work on this script, so I will keep this post updated.

Using Ruby:

require "date"

Date.new(2004,2,25).upto(Date.new(2004,3,2)) do  |date| 
  puts date.to_s
end

2004-02-25
2004-02-26
2004-02-27
2004-02-28
2004-02-29
2004-03-01
2004-03-02

I'm not familiar with the code you just wrote. :confused:
Can that be done with korn shell?

This would take about 30 seconds to write if you would use datecalc. In fact....

#! /usr/bin/ksh

read "yr1?enter start date (yyyy mm dd) - " mo1 da1
start=$(datecalc -j $yr1 $mo1 $da1) ||
        { echo $yr1 $mo1 $da1 is not valid ; exit 1 ; }
read "yr2?enter start date (yyyy mm dd) - " mo2 da2
stop=$(datecalc -j $yr2 $mo2 $da2) ||
        { echo $yr2 $mo2 $da2 is not valid ; exit 1 ; }

while ((start<stop)) ; do
        datecalc -j $start
        ((start=start+1))
done
exit 0

Well, I'm getting old... :frowning: OK, 110 seconds..

:eek: Amazed.

I'm almost done with my version.

I'll post it up soon so people can compare the easy way (Your way) and the hard way (My way)

It's for the Ruby language, which is free and available for every platform.

Here is my code now. It works (at least for now).

#!/bin/ksh

#=============
# date.ksh
#=============
# Last updated: July 29, 2005

#================================
# This script uses other scripts
#================================
# isLeapYr.ksh

#===========
# Functions
#===========
# single2dbl converts single digits into double (ie: 5 into 05 and 8 into 08)

single2dbl()
{
    number=$1
    length_of_var=${#number}

    if (( length_of_var==1 )); then
        echo "0${number}"
    else
        echo $number
    fi
}

#==============
# Script Modes
#==============
# 0: Auto-feed for current day
# 1: Manual feed for any day (including date intervals)

script_mode=1               # manual mode (for custom dates)

#= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = #
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =#
#= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = #

#========
# Mode 1
#========
# lines marked with triple pounds are always required

if (( script_mode==1 )); then
    
    #============================
    # Input Start date (required)
    #============================

    # date variables for DT_BUS and csv filename
    start_date_Y='' ### year  (ie: 1998, 2005)
    start_date_m=''    ### month (ie: 4, 11)
    start_date_d=''    ### day   (ie: 9, 25)

    #==========
    # End date
    #==========

    # date variables for DT_BUS and csv filename
    end_date_Y=''   ### year  (ie: 1998, 2005)
    end_date_m=''      ### month (ie: 4, 11)
    end_date_d=''      ### day   (ie: 9, 25)

#=============
# else Mode 2
#=============

elif (( script_mode==0 )); then
    # date variables for DT_BUS and csv filename
    start_date_Y=`date +%Y`   # year  (ie: 1998, 2005)
    start_date_m=`date +%m`   # month (ie: 04, 11)
    start_date_d=`date +%d`   # day   (ie: 09, 25)

    # date variable (month) for `ls -al <path> | grep <date>`
    start_date_b=`date +%b`   # month (ie: Jun, Jul)
fi

#= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = #
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =#
#= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = #

echo "From: ${start_date_b} ${start_date_d}, ${start_date_Y}"
echo "To: ${end_date_b} ${end_date_d}, ${end_date_Y}"

#==============
# Declarations
#==============

#                     Ja Fe Ma Ap My Jn Jl Au Se Oc No De
set -A DaysInMonth xx 31 28 31 30 31 30 31 31 30 31 30 31

month_loopReset=0
day_loopReset=0

#===========
# Year Loop
#===========

while (( start_date_Y <= end_date_Y )); do

    isLeapYr=`isLeapYr.ksh $start_date_Y`

    if (( isLeapYr==1 )); then
        DaysInMonth[2]=29
    else
        DaysInMonth[2]=28
    fi

    #============
    # Month Loop
    #============

    if (( month_loopReset==1 )); then
        month_loopStart=1
    else
        month_loopStart=$start_date_m
    fi
    
    if (( start_date_Y < end_date_Y )); then
        month_loopEnd=12
    else
        month_loopEnd=$end_date_m
    fi

    while (( month_loopStart <= month_loopEnd )); do

        #echo ${month_loopStart}

        #==========
        # Day Loop
        #==========

        if (( day_loopReset==1 )); then
            day_loopStart=1
        else
            day_loopStart=$start_date_d
        fi
        
        if (( start_date_m < end_date_m )); then
            day_loopEnd=${DaysInMonth[${start_date_m}]}
        else
            day_loopEnd=$end_date_d
        fi

        while (( day_loopStart <= day_loopEnd )); do
            
            #======
            # ta-da!
            #======  
            
            echo "${start_date_Y} `single2dbl $month_loopStart` `single2dbl $day_loopStart`"

            (( day_loopStart=day_loopStart+1 ))
        done # end Day loop

        if (( month_loopStart == month_loopEnd )); then
            month_loopReset=1
        fi

        (( month_loopStart=month_loopStart+1 ))
    done # end Month loop
 
    (( start_date_Y=start_date_Y+1 ))
done # end Year loop
#!/bin/ksh

#==============
# isLeapYr.ksh
#==============
# Description: Checks if year is a leap year.
# Input: Year in the format yyyy (ie: 1998, 2003)
# Output: 1 for true, 0 for false
# Last updated: July 29, 2005

#===================================
# Rules for calculating a leap year
#===================================
# 1. A year that is divisible by 4 is a leap year
# 2. Exception to rule 1: a year that is divisible by 100 is not a leap year
# 3. Exception to rule 2: a year that is divisible by 400 is a leap year

year=$1    # init input variable
isLeapYear=0 # init return variable

# rule 1
if (( year%4==0 )); then

    isLeapYear=1
    
    # rule 2 (exception to rule 1)
    if (( year%100==0 )); then

        isLeapYear=0

        # rule 3 (exception to rule 2)
        if (( year%400==0 )); then

            isLeapYear=1
        fi
    fi
fi

# echo 1 for true or 0 for false
echo $isLeapYear