Computing.Net > Forums > Unix > Getting old date into variable

Getting old date into variable

Reply to Message Icon

Original Message
Name: Carl Hoog
Date: June 27, 2003 at 08:54:31 Pacific
Subject: Getting old date into variable
OS: HP-UX
CPU/Ram: Not relevent
Comment:

I have a backup directory with a few hundred thousand files, several thousand from each day. I want to tar up the files into 1 or more files for each day. I can use the find command with -mtime to get files that are 'x' number of days old, but how do I get today's date minus 'x' into a variable of the form 'YYYYMMDD' so that I can use it as part of the tar file name?



Report Offensive Message For Removal

Response Number 1
Name: David Perry
Date: June 27, 2003 at 10:54:38 Pacific
Subject: Getting old date into variable
Reply: (edit)

var=`date +%Y%m%d`


Report Offensive Follow Up For Removal

Response Number 2
Name: Carl Hoog
Date: June 27, 2003 at 11:08:26 Pacific
Subject: Getting old date into variable
Reply: (edit)

That gives me today's date. When I do a find command with the -mtime option, I want today's date - 'x' where x is the number of days I'm going back to do the archiving.


Report Offensive Follow Up For Removal

Response Number 3
Name: nails
Date: June 27, 2003 at 15:54:50 Pacific
Subject: Getting old date into variable
Reply: (edit)

Carl:

In an earler thread in this forum named:

count days since jan 1 1970

discusses this particular problem. In this thread, I say what a pain date arithmetic is with non-GNU tools. While Perl is probably your best choice, you can do it in the shell using Scaliger's Julian Date. I'm including it here for your perusal:

Regards,

Nails

#!/bin/sh

# Scaliger's Julian date (JD) is a continuous count of days from 1 January
# 4713 BC. The following algorithm is good from years 1801 to 2099
# See URL:http://aa.usno.navy.mil/faq/docs/JD_Formula.html for
# more information. arguments are
# day, month, year
get_JD () {
# calculation below is all on one line.
# Also the bc calcuator is being called with a unix "here" document, so you need 2 less than arrows between bc and MSG. This forum wipes them out:
bc MSG
scale=0
$1-32075+1461*($3+4800+($2-14)/12)/4+367*($2-2-($2-14)/12*12)/12-3*(($3+4900+($2-14)/12)/100)/4
MSG
}

jd1970=`get_JD 1 1 1970`
jd1971=`get_JD 1 1 1971`
diff=`expr $jd1971 - $jd1970`
echo $diff # still 365 days



Report Offensive Follow Up For Removal

Response Number 4
Name: WilliamRobertson
Date: June 27, 2003 at 16:43:20 Pacific
Subject: Getting old date into variable
Reply: (edit)

Try

shift_date $(date +%Y%m%d) -1


Report Offensive Follow Up For Removal

Response Number 5
Name: Carl Hoog
Date: July 1, 2003 at 14:10:04 Pacific
Subject: Getting old date into variable
Reply: (edit)

I tried the shift suggestion but couldn't get the 'date - 1' part to ever work.

Nails: The formula you provided works great. Do you have a compact formula for getting back the year, month, and day components from the Julian date?


Report Offensive Follow Up For Removal


Response Number 6
Name: WilliamRobertson
Date: July 1, 2003 at 16:32:21 Pacific
Subject: Getting old date into variable
Reply: (edit)

Not 'date - 1', 'shift_date -1'. My shift_date script takes two parameters: the first is a date in YYYYMMDD format, the second is an integer which can be negative. It works to my knowledge on HPUX, Solaris and FreeBSD.


Report Offensive Follow Up For Removal

Response Number 7
Name: nails
Date: July 2, 2003 at 09:05:18 Pacific
Subject: Getting old date into variable
Reply: (edit)

Carl:

Yup, there is. In the interest of full-disclosure, this is from an article from Sys Admin, "Date Arithmetic with the Shell" by Kyle Douglass and Ed Schaefer.

They got the algorithm's from USNO:


# This function computes the gregorian date from the julian date - $1. Returns
a
# date string of the form: MONTH DAY YEAR
# See URL: http://aa.usno.navy.mil/faq/docs/JD_Formula.html for more information
get_greg_from_JD ()
{
typeset -i L
typeset -i N
typeset -i I
typeset -i J
typeset -i DAY
typeset -i MON
typeset -i YR

L=$(($1+68569)) # $1 is the julian date
N=$((4*L/146097))
L=$((L-(146097*N+3)/4))
I=$((4000*(L+1)/1461001))
L=$((L-1461*I/4+31))
J=$((80*L/2447))
DAY=$((L-2447*J/80))
L=$((J/11))
MON=$((J+2-12*L))
YR=$((100*(N-49)+I+L))

echo $MON $DAY $YR
}

TY=$(date '+%Y') # Year for today - MUST be YYYY
TM=$(date '+%m') # Month for today
TD=$(date '+%d') # Day for today
# printf "%s %s %s\n" $TM $TD $TY
JD=$(get_JD TD TM TY) # today's JD
# yesterday's date
yesterdays_date_str=$(get_greg_from_JD $((JD-1)) )
# parse yesterdays date string
set - $(echo $yesterdays_date_str)
echo $1 # month
echo $2 # day
echo $3 # year

Regards,

Nails


Report Offensive Follow Up For Removal

Response Number 8
Name: Jonny
Date: July 28, 2003 at 04:58:42 Pacific
Subject: Getting old date into variable
Reply: (edit)

oh oh oh

its very hard to work with dates under unix, because there is no spezify tool for it!

my solution is, that "we" have a oracle server installed on the unix machine, so i can calculate the date with sql:

backup_datum=`sqlplus -s EOF
user/password
set head off
set feed off
select to_char(sysdate - $1 ,'YYYYMMDD') from dual;
EOF`

this is another way to do it!

regards

Jonny


Report Offensive Follow Up For Removal

Response Number 9
Name: WilliamRobertson
Date: July 28, 2003 at 14:27:49 Pacific
Subject: Getting old date into variable
Reply: (edit)

> this is another way to do it!

Well clearly two possible solutions are described above.

Perhaps you could explain a bit more why neither of them seems to be what you need.


Report Offensive Follow Up For Removal

Response Number 10
Name: Jonny
Date: July 31, 2003 at 05:25:08 Pacific
Subject: Getting old date into variable
Reply: (edit)

@ William

that is my old script:

#!/bin/ksh
#########################################################"
# Script name : calculate_date.sh"
# Path : /home/oracle/dba/bin"
# Author : Jonny Schulz"
# Rheinland Versicherungen"
# Create : 27.06.2003"
# Change :"
# Of :"

# Description"
# This Script calculate Date."
#########################################################"


#####################
### VARIABLES ###
#####################

date=`date +'%Y.%m.%d'`
yyyy=`date +'%Y'`
yy=`date +'%y'`
month=`date +'%m'`
day=`date +'%d'`
sign=$1
count=$2
format=$3


#####################
### FUNCTIONS ###
#####################

do_help()
{
echo ""
echo "#########################################################"
echo "# Script name : calculate_date.sh"
echo "# Path : /home/oracle/dba/bin"
echo "# Author : Jonny Schulz"
echo "# Rheinland Versicherungen"
echo "# Create : 27.06.2003"
echo "# Change :"
echo "# Of :"
ec---"
echo "# Description"
echo "# This Script calculate Date."
echo "#########################################################"
echo ""
echo " Please enter the following parameters:"
echo " calculate_date.sh [+/-] [0123456789] [1/2/3/4/5/6/7/8]"
echo ""
echo " [+/-] = Art to calculate"
echo " [0123456789] = Count days"
echo " [1/2/3/4/5/6/7/8] = Which format"
echo ""
echo " Possible formats:"
echo " ------------------"
echo " YYYY.MM.DD (1)"
echo " YYYYMMDD (2)"
echo " YY.MM.DD (3)"
echo " YYMMDD (4)"
echo " DD.MM.YYYY (5)"
echo " DDMMYYYY (6)"
echo " DD.MM.YY (7)"
echo " DDMMYYYY (8)"
echo ""
echo ""
}

minus()
{
while [[ $count != 0 ]]
do
count=$(($count-1))
case $month in
01 ) day=$(($day-1))
if [[ $day = 0 ]]
then
day=31
month=12
year=$(($year-1))
fi
;;

02 ) day=$(($day-1))
if [[ $day = 0 ]]
then
day=31
month=01
fi
;;

03 )
leap_year=`bc EOF
scale=8
$year/4
EOF`

leap_year=`echo $leap_year | cut -d'.' -f2`

day=$(($day-1))
if [[ $day = 0 ]]
then
if [[ $leap_year = "00000000" ]]
then
day=29
month=02
else
day=28
month=02
fi
fi
;;

04 ) day=$(($day-1))
if [[ $day = 0 ]]
then
day=31
month=03
fi
;;

05 ) day=$(($day-1))
if [[ $day = 0 ]]
then
day=30
month=04
fi
;;

06 ) day=$(($day-1))
if [[ $day = 0 ]]
then
day=31
month=05
fi
;;

07 ) day=$(($day-1))
if [[ $day = 0 ]]
then
day=30
month=06
fi
;;

08 ) day=$(($day-1))
if [[ $day = 0 ]]
then
day=31
month=07
fi
;;

09 ) day=$(($day-1))
if [[ $day = 0 ]]
then
day=31
month=08
fi
;;

10 ) day=$(($day-1))
if [[ $day = 0 ]]
then
day=30
month=09
fi
;;

11 ) day=$(($day-1))
if [[ $day = 0 ]]
then
day=31
month=10
fi
;;

12 ) day=$(($day-1))
if [[ $day = 0 ]]
then
day=30
month=11
fi
;;
esac
done
}

plus()
{
while [[ $count != 0 ]]
do
count=$(($count-1))
case $month in
01 ) day=$(($day+1))
if [[ $day = 32 ]]
then
day=01
month=02
fi
;;

02 )
leap_year=`bc EOF
scale=8
$year/4
EOF`

leap_year=`echo $leap_year | cut -d'.' -f2`

day=$(($day+1))
if [[ $leap_year = "00000000" ]]
then
if [[ $day = 30 ]]
then
day=01
month=03
fi
else
if [[ $day = 29 ]]
then
day=01
month=03
fi
fi
;;

03 ) day=$(($day+1))
if [[ $day = 32 ]]
then
day=01
month=04
fi
;;

04 ) day=$(($day+1))
if [[ $day = 31 ]]
then
day=01
month=05
fi
;;

05 ) day=$(($day+1))
if [[ $day = 32 ]]
then
day=01
month=06
fi
;;

06 ) day=$(($day+1))
if [[ $day = 31 ]]
then
day=01
month=07
fi
;;

07 ) day=$(($day+1))
if [[ $day = 32 ]]
then
day=01
month=08
fi
;;

08 ) day=$(($day+1))
if [[ $day = 32 ]]
then
day=01
month=09
fi
;;

09 ) day=$(($day+1))
if [[ $day = 31 ]]
then
day=01
month=10
fi
;;

10 ) day=$(($day+1))
if [[ $day = 32 ]]
then
day=01
month=11
fi
;;

11 ) day=$(($day+1))
if [[ $day = 31 ]]
then
day=01
month=12
fi
;;

12 ) day=$(($day+1))
if [[ $day = 32 ]]
then
day=01
month=01
year=$(($year+1))
fi
;;
esac
done
}


################
### MAIN ###
################

case $format in
1 | 2 | 5 | 6 ) year=$yyyy ;;
3 | 4 | 7 | 8 ) year=$yy ;;
* ) do_help ;;
esac

if [[ $sign = "+" ]]
then
plus
else
if [[ $sign = "-" ]]
then
minus
else
do_help
fi
fi

case $day in
1 ) day="01" ;;
2 ) day="02" ;;
3 ) day="03" ;;
4 ) day="04" ;;
5 ) day="05" ;;
6 ) day="06" ;;
7 ) day="07" ;;
8 ) day="08" ;;
9 ) day="09" ;;
esac

case $year in
1 ) year="01" ;;
2 ) year="02" ;;
3 ) year="03" ;;
4 ) year="04" ;;
5 ) year="05" ;;
6 ) year="06" ;;
7 ) year="07" ;;
8 ) year="08" ;;
9 ) year="09" ;;
esac

case $format in
1 | 3 ) date_calculate=${year}.${month}.${day}
;;
2 | 4 ) date_calculate=echo ${year}${month}${day}
;;
5 | 7 ) date_calculate=echo ${day}.${month}.${year}
;;
6 | 8 ) date_calculate=echo ${day}${month}${year}
;;
esac

echo $date_calculate

exit 0


unfortunately the runtime is bad!
for this reason i create a new script and use the sql statement. i only had surch for a better script, but no script use a better runtime...


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: Getting old date into variable

Comments:

 


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