Compare Date And Time


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Compare Date And Time
# 1  
Old 09-28-2006
Compare Date And Time

Hi

can somebody send me the code for compare two date and run a shell script. following is the code which i actully want but I am getting errors.

regard


Jamil

. /opt/home/rep/.profile

#!/bin/sh
VALUE=`sqlplus -silent rep/Ndk38f7@dw <<END
set pagesize 0 feedback off verify off heading off echo off
select trunc(max(chg_dt)) from NOT_SCHEDULED_INSTALL;
exit;
END`
VALUE2=`sqlplus -silent rep/Ndk38f7@dw <<END
set pagesize 0 feedback off verify off heading off echo off
select trunc(sysdate) from dual;
exit;
End`
if [[ $VALUE -eq $VALUE2 ]]; then
echo "No rows returned from database"
exit 0
else
echo $VALUE2
echo $VALUE
fi
# 2  
Old 09-28-2006
Oracle already has date arithmetic, ksh doesn't - use PL/SQL:
Code:
#!/bin/ksh

date_equal()
{

sqlplus -silent rep/Ndk38f7@dw <<END
set pagesize 0 feedback off verify off heading off echo off serverout on size 100000
DECLARE 
   date1 DATE:=NULL;
   date2 DATE:=NULL;
BEGIN
DBMS_OUTPUT.enable(100000);
select trunc(max(chg_dt)) into date1 from NOT_SCHEDULED_INSTALL;
select trunc(sysdate) into date2 from dual;
IF date1 = date2
THEN
	DBMS_OUTPUT.put_line('1');
ELSE
    DBMS_OUTPUT.put_line('0');
END IF;
END;
/
exit;
END

}

if [[ $(date_equal) -eq 1 ]] ; then
# do something 
else
# do something else
fi

# 3  
Old 09-29-2006
date and time problem

Thanks for your reply.
Probelm is it do not work properly, it always print 'not ok'

if [[ $(date_equal) -eq 1 ]] ; then
echo ' ok'
else
echo ' not ok'
fi

thanks
# 4  
Old 09-29-2006
And Now for Something Different

I've used this before and it works pretty well.
Does a few different things with dates depending on the parameters.

#! /usr/bin/ksh

# datecalc -- Perderabo's date calculator
#

USAGE="\
datecalc -a year month day - year month day
datecalc -a year month day [-|+] n
datecalc -d year month day
datecalc -D year month day
datecalc -j year month day
datecalc -j n
datecalc -l year month
use \"datecalc -help\" use for more documentation"

DOCUMENTATION="\
datecalc Version 1.1

datecalc does many manipulations with dates.
datecalc -a is for date arithmetic
datecalc -d or -D converts a date to the day of week
datecalc -j converts to date to or from julian day
datecalc -l outputs the last day of a month

All dates must be between the years 1860 and 3999.

datecalc -a followed by 7 parameters will calculate the
number of days between two dates. Parameters 2-4 and 6-8
must be dates in ymd form, and parameter 5 must be a minus
sign. The output is an integer. Example:

> datecalc -a 1960 12 31 - 1922 2 2
14212


datecalc -a followed by 5 parameters will calculate the
a new date offset from a given date, Parameters 2-4 must
be a date in ymd form, paramter 5 must be + or -, and
paramter 6 must be an integer. Output is a new date.
Example:

> datecalc -a 1960 12 31 + 7
1961 1 7


datecalc -d followed by 3 parameters will convert a date
to a day-of-week. Parameters 2-4 must be a date in ymd
form. Example:

> datecalc -d 1960 12 31
6


datecalc -D is like -d except it displays the name of
the day. Example:

> datecalc -D 1960 12 31
Saturday


datecalc -j followed by 3 parameters will convert a date
to Modified Julian Day number. Example:
> datecalc -j 1960 12 31
37299


datecalc -j followed by a single parameter will convert
a Modified Julian Day number to a date. Example:
> datecalc -j 37299
1960 12 31


datecalc -l followed by year and month will output the last
day of that month. Note that by checking the last day of
February you can test for leap year. Example:
> datecalc -l 2002 2
28"


lastday() {
integer year month leap
# ja fe ma ap ma jn jl ag se oc no de
set -A mlength xx 31 28 31 30 31 30 31 31 30 31 30 31

year=$1
if ((year<1860 || year> 3999)) ; then
print -u2 year out of range
return 1
fi
month=$2
if ((month<1 || month> 12)) ; then
print -u2 month out of range
return 1
fi

if ((month != 2)) ; then
print ${mlength[month]}
return 0
fi

leap=0
if ((!(year%100))); then
((!(year%400))) && leap=1
else
((!(year%4))) && leap=1
fi

feblength=28
((leap)) && feblength=29
print $feblength
return 0
}


date2jd() {
integer ijd day month year mnjd jd lday

year=$1
month=$2
day=$3
lday=$(lastday $year $month) || exit $?

if ((day<1 || day> lday)) ; then
print -u2 day out of range
return 1
fi

((standard_jd = day - 32075
+ 1461 * (year + 4800 - (14 - month)/12)/4
+ 367 * (month - 2 + (14 - month)/12*12)/12
- 3 * ((year + 4900 - (14 - month)/12)/100)/4))
((jd = standard_jd-2400001))


print $jd
return 0
}


jd2dow()
{
integer jd dow numeric_mode
set +A days Sunday Monday Tuesday Wednesday Thursday Friday Saturday

numeric_mode=0
if [[ $1 = -n ]] ; then
numeric_mode=1
shift
fi


jd=$1
if ((jd<1 || jd>782028)) ; then
print -u2 julian day out of range
return 1
fi

((dow=(jd+3)%7))

if ((numeric_mode)) ; then
print $dow
else
print ${days[dow]}
fi
return
}

jd2date()
{
integer standard_jd temp1 temp2 jd year month day

jd=$1
if ((jd<1 || jd>782028)) ; then
print julian day out of range
return 1
fi
((standard_jd=jd+2400001))
((temp1 = standard_jd + 68569))
((temp2 = 4*temp1/146097))
((temp1 = temp1 - (146097 * temp2 + 3) / 4))
((year = 4000 * (temp1 + 1) / 1461001))
((temp1 = temp1 - 1461 * year/4 + 31))
((month = 80 * temp1 / 2447))
((day = temp1 - 2447 * month / 80))
((temp1 = month / 11))
((month = month + 2 - 12 * temp1))
((year = 100 * (temp2 - 49) + year + temp1))
print $year $month $day
return 0
}


#
# Parse parameters and get to work.
case $1 in
-a) if (($# == 8)) ; then
if [[ $5 != - ]] ; then
print -u2 - "$USAGE"
exit 1
fi
jd1=$(date2jd $2 $3 $4) || exit $?
jd2=$(date2jd $6 $7 $8) || exit $?
((jd3=jd1-jd2))
print $jd3
exit 0
elif (($# == 6)) ; then
jd1=$(date2jd $2 $3 $4) || exit $?
case $5 in
-|+) eval '(('jd2=${jd1}${5}${6}'))'
jd2date $jd2
exit $?
;;
*)
print -u2 - "$USAGE"
exit 1
;;
esac

fi
;;

-d|-D) if (($# != 4)) ; then
print -u2 - "$USAGE"
exit 1
fi
jd1=$(date2jd $2 $3 $4) || exit $?
numeric=-n
[[ $1 = -D ]] && numeric=""
eval jd2dow $numeric $jd1
exit $?
;;

-j) if (($# == 4)) ; then
date2jd $2 $3 $4
exit $?
elif (($# == 2)) ; then
jd2date $2 $3 $4
exit $?
else
print -u2 - "$USAGE"
exit 1
fi
;;

-l) if (($# == 3)) ; then
lastday $2 $3
exit $?
else
print -u2 - "$USAGE"
exit 1
fi
;;

-help) print - "$USAGE"
print ""
print - "$DOCUMENTATION"
exit 0
;;

*) print -u2 - "$USAGE"
exit 0
;;


esac

#not reached
exit 7
# 5  
Old 10-02-2006
Another way would be:
Code:
#!/usr/bin/ksh
RET=`sqlplus -silent rep/Ndk38f7@dw <<END
set pagesize 0 feedback off verify off heading off echo off
select decode(d1,d2,'No rows',to_char(d1)||' '||to_char(d2)) from
(select trunc(max(chg_dt)) d1 from NOT_SCHEDULED_INSTALL),
(select trunc(sysdate) d2 from dual);
exit;
END`
if [[ "$RET" = "No rows" ]] ; then
   # do something
else
  # do something else
fi

Let the Oracles decode-function compares for you.
# 6  
Old 01-10-2007
unix script

I need a little change in this script.

I need when the date is less then sysdate it sleep for 1 houre otherwise it run my abc.sh script.

thanks

Jamil
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare Date to today's date in shell script

Hi Community! Following on from this code in another thread: #!/bin/bash file_string=`/bin/cat date.txt | /usr/bin/awk '{print $5,$4,$7,$6,$8}'` file_date=`/bin/date -d "$file_string"` file_epoch=`/bin/date -d "$file_string" +%s` now_epoch=`/bin/date +%s` if then #let... (2 Replies)
Discussion started by: Greenage
2 Replies

2. UNIX for Beginners Questions & Answers

Compare date in .txt with system date and remove if it's lesser than system date

Can someone help me with the code wherein there is a file f1.txt with different column and 34 column have expiry date and I need to get that and compare with system date and if expiry date is <system date remove those rows and other rows should be moved to new file f2.txt . I don't want to delete... (2 Replies)
Discussion started by: Stuti
2 Replies

3. Shell Programming and Scripting

Compare the system date with date from a text file

I get the date that's inside a text file and assigned it to a variable. When I grep the date from the file, I get this, Not After : Jul 28 14:09:57 2017 GMT So I only crop out the date, with this command echo $dateFile | cut -d ':' -f 2,4The result would be Jul 28 14:57 2017 GMT How do I... (3 Replies)
Discussion started by: Loc
3 Replies

4. Shell Programming and Scripting

Shell script to compare two files of todays date and yesterday's date

hi all, How to compare two files whether they are same are not...? like i had my input files as 20141201_file.txt and 20141130_file2.txt how to compare the above files based on date .. like todays file and yesterdays file...? (4 Replies)
Discussion started by: hemanthsaikumar
4 Replies

5. Shell Programming and Scripting

Adding days to system date then compare to a date

Hi! I am trying to read a file and every line has a specific date as one of its fields. I want to take that date and compare it to the date today plus 6 days. while read line do date=substr($line, $datepos, 8) #date is expected to be YYYYMMDD if ; then ...proceed commands ... (1 Reply)
Discussion started by: kokoro
1 Replies

6. Shell Programming and Scripting

ksh compare dates INSIDE a file (ie date A is > date B)

In KSH, I am pasting 2 almost identical files together and each one has a date and time on each line. I need to determine if the first instance of the date/time is greater than the 2nd instance of the date/time. If the first instance is greater, I just need to echo that line. I thought I would... (4 Replies)
Discussion started by: right_coaster
4 Replies

7. Shell Programming and Scripting

compare date and time inside data of two files

i have two files with identical no of columns. 6th columns is date (MM/DD/YY format) and 7th columns is time (HH:MM:SS) format. I need to compare these two vaules and if the date & time is higher than fileA, save it on fileC; if the value is lower, then save it on fileD CONDITIONS... (7 Replies)
Discussion started by: ajiwww
7 Replies

8. Shell Programming and Scripting

Compare Last Modified Time across Time Zone

Hi, I'm new to shell script programming, I only have Java programming background. I'm writing a shell script to do file synchronization between 2 machines that located at different time zone area. Both machine were set its time zone according to its geographical location (Eg: server is at... (1 Reply)
Discussion started by: python
1 Replies

9. Shell Programming and Scripting

Compare date from db2 table to yesterday's Unix system date

I am currently running the following Korn shell script which works fine: #!/usr/bin/ksh count=`db2 -x "select count(*) from schema.tablename"` echo "count" I would like to add a "where" clause to the 2nd line that would allow me to get a record count of all the records from schema.tablename... (9 Replies)
Discussion started by: sasaliasim
9 Replies

10. UNIX for Dummies Questions & Answers

Need to get 4 Hrs back time and compare with successive time

Hi all, I am working on a script in which i need to get 4 hrs back time from the current time which i got from this perl function : `perl -e 'print localtime(time() - 14400) . "\n"'` now i need to get this in a loop and increment that time by 15 minutes i.e i=900(=15minutes) `perl... (2 Replies)
Discussion started by: maanik85
2 Replies
Login or Register to Ask a Question