Date Intervals


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date Intervals
# 8  
Old 07-29-2005
Solution

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

Code:
#!/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

Code:
#!/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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Gap length between intervals

hi all, I wish to calculate the length between intervals whose are defined by a starting and an end possition. The data looks like this: 1 10 23 30 45 60 70 100... The desired output should be: 13 # (23-10) 15 # (45-30) 10 # (70-60)... I donīt know how to operate with different... (2 Replies)
Discussion started by: lsantome
2 Replies

2. UNIX for Dummies Questions & Answers

Building intervals

Hi all, I hope you can help me with the following question: I have multiple tables like this: Chr Start End Zygosity Gene chr1 153233510 153233510 het LOR chr1 153233615 153233615 hom LOR chr1 153233701 153233701 hom LOR chr1 ... (5 Replies)
Discussion started by: lsantome
5 Replies

3. Shell Programming and Scripting

Grab exactly one byte from a FIFO, at random intervals

I want to develop a script of the following form: #!/bin/bash # Function 'listen' opens a data stream # which stores all incoming bytes in # a buffer, preparing them to be # grabbed by a following function # which appears at random # intervals during the execution of # the script ... (11 Replies)
Discussion started by: vomv1988
11 Replies

4. Shell Programming and Scripting

Divide numbers into intervals

divide input values into specified number (-100 or -200) according to the key (a1 or a2 ....) For ex: if we give -100 in the command line it would create 100 number intervals (1-100, 100-200, 200-300) untill it covers the value 300 in a1. Note: It should work the same even with huge numbers... (3 Replies)
Discussion started by: ruby_sgp
3 Replies

5. Shell Programming and Scripting

Shell Script - Copy File at intervals

Hi, I want to copy some files from a Folder say, /usr/X at random intervals to another location. Basically, new files will be dumped at random intervals to location /usr/X and I have to copy those new files to some other location (after copying, I cannot delete those files from source... (2 Replies)
Discussion started by: angshuman_ag
2 Replies

6. Shell Programming and Scripting

Bash loop script for specfic intervals

Hello, first of all I am happy to sign up here. Next is, I have shell scripts for all the files I want looped infinitely for specific intervals(This is for a wmii config). My question here is how can I run multiple scripts at a 10 second interval for instance? (4 Replies)
Discussion started by: Mesher
4 Replies

7. Red Hat

How do sa1/sar time intervals work?

Hi, I have set up sar on my RedHat and Fedora Linux systems. I am running sa1 from cron: 0 8-17 * * 1-5 /usr/lib/sa/sa1 1200 3 & The 1200 and 3 parameters tell sa1 to save data every 1200 seconds (== 20 minutes) and to write 3 times. When I run sar to observe my data, I'll see... (1 Reply)
Discussion started by: mschwage
1 Replies

8. Programming

performing a task at regular intervals

hi! i m tryin to write a program that will perform a specific tasks after fixed interval of time.say every 1 min. i jus donno how to go abt it.. which functions to use and so on... i wud like to add that i am dont want to use crontab over here. ny lead is appreciated. thanx. (2 Replies)
Discussion started by: mridula
2 Replies

9. Shell Programming and Scripting

mailing myself at regular intervals...

hi all, i wrote a script to mail myself using pine (modified) to keep remind of b'days. #!/bin/bash grep "`date +%D |awk -F/ '{print $2+1, $1+0}'`" dataFile >/home/username/mailme if test -s /home/username/mailme then pine -I '^X,y' -subject "Birthday Remainder" username... (4 Replies)
Discussion started by: timepassman
4 Replies

10. Shell Programming and Scripting

How to perform Date Intervals?

I have a 300 line script which generates key performance indicators for one of our systems. Since I just started learning sh/ksh half a month ago there's still a lot I haven't had experience with yet. Currently, the script generates the report for a specific day. (It takes the date specified by... (2 Replies)
Discussion started by: yongho
2 Replies
Login or Register to Ask a Question