Riaan Lehmkuhl's Blog

Subversion, Progamming, Tips & Tricks and whatever else springs to mind.
11Jun

The UNIX date command and Date manipulation

11 June 2008 22:44 by Riaan Lehmkuhl
You need to do something on a daily with a file in a directory named yyyy-mm-dd.txt (year-month-day.txt).
That's easy enough:
#!/bin/ksh
# just get the current date
today="`date +%Y-%m-%d`"
filename="${today}.txt"
echo $filename
Oops, you need the file from yesterday. Not so simple any more as the unix date command just sets or prints the date.
Let's do some of the usual date math (month ends, leap years, etc.).
#!/bin/ksh
# default is one to travel back in time to yesterday
daystogoback="1" 
# to be a little dynamic, we can use an argument to go back n days.
if [ $# -eq "1" ]; then
   daystogoback="$1"
fi
# get the current year month and day
nowy=`date +%Y`
nowm=`date +%m`
nowd=`date +%d`
# get rid of the month's possible leading zero
nowm=`echo $nowm*1|bc`
# if today's day is greater than the number of days to go back
# we really can just take the shortcut and subtract the days to go back
# because we will stay in the current month and nothing fancy is needed
if test $nowd -gt $daystogoback
then
        nowd=`echo $nowd-$daystogoback|bc`
else
# if we are in January, we have to go to the previous year 
# and set the month to December
# else we can just subtract the month
        if test $nowm -eq "1"
        then
                nowy=`echo $nowy-1|bc`
                nowm="12"
        else
                nowm=`echo $nowm-1|bc`
        fi
# now we can calculate the day, but first we have to see
# how many days in the month...
# we default to 31 and do nothing more for the months with 31 days
# we have to check for February and then for a leap year (28/29)
# the rest of the months will have 30 days
        newd="31"
        case "$nowm" in
                "1") ;;
                "3") ;;
                "5") ;;
                "7") ;;
                "8") ;;
                "10") ;;
                "12") ;;
                "2")    if test "0" -eq "`echo $nowy%4|bc`"
                                then
                                        newd="29"
                                else
                                        newd="28"
                                fi ;;
                *) newd="30" ;;
        esac
# subtract the current day from the days to go back 
# and then from the total days in the month
        nowd="`echo $daystogoback-$nowd|bc`"
        nowd="`echo $newd-$nowd|bc`"
fi
# now we just do some formatting for the month and day
if test $nowm -lt "10"
then
        nowm="0$nowm";
fi
if test $nowd -lt "10"
then
        nowd="0$nowd";
fi
# our new date
filedate="$nowy-$nowm-$nowd"
# and the file we need to access...
filename="${filedate}.txt"
echo $filename
Always fun playing with dates.

References:
KSH script BASICS
Wikipedia: Korn shell

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Comments are closed

Riaan Lehmkuhl


Me, a disorder of the brain that results in a disruption in a person's thinking, mood, and ability to relate to others.

Recent comments

Comment RSS

Thingies

Calendar And Month List

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Disclaimer & Privacy

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Privacy:
We use third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.

Most comments

Cool Quote

I know that you believe that you understood what you think I said, but I am not sure you realize that what you heard is not what I meant. - Robert McCloskey