i have one hard coded time which will be 23:45 and one will be sysdate (same date) and time less than 23:45.
i want to start my job at 23:45 and the input file will be arriving before that. so i want to make sure that the task starts only at 23:45 and from the input file time till 23:45 the script should go into sleep mode.
for that i need difference between both the times in seconds so that i can use sleep for the difference time.
Please help me on how to get the difference in seconds.
Any other solution to achieve the same requirement is also welcomed.
#!/bin/ksh
# add 27 hours to a "timestamp"
sub_times() # seconds diff: $1 - now
{
perl -e '
use Time::Local;
# get current time
$tm="$ARGV[0]";
( $sec,
$min,
$hr,
$mday,
$mon,
$yr,
$wday,
$yday,
$isdst)=localtime(time);
# alter to 23:45 or ARGV[0]
$sec=0;
$min=substr($tm,3,2);
$hr=substr($tm,0,2);
# epochtm is number of seconds at ARGV[0] in future
$epochtm=timelocal($sec,$min,$hr,$mday,$mon,$yr);
if($epochtm > time)
{
$epochtm=$epochtm - time;
print "$epochtm", "\n";
}
else # we have an error ie., time is > 23:45
{
print "0\n";
} ' "$1"
}
# example use of sub_times:
sleep_time=$( sub_times "23:45")
echo "$sleep_time"
sleep_time=$( sub_times "00:00")
echo "$sleep_time"
The GNU date command in full of goodies but not when it comes to calculate a date or time difference. Here is what I came up with after looking to more than one solution.
#!/bin/bash
date2stamp () {
date -u --date "$1" +%s
}
dateDiff (){
dte1=$(date2stamp $1)
dte2=$(date2stamp $2)
diffSec=$((dte2-dte1))
if ((diffSec < 0)); then abs=-1; else abs=1; fi
echo $((diffSec*abs))
}
dateDiff "15:30:15" "15:35:18"