0

I have a timestamp variable in YYYYYMMDDHHMMSS format. using this variable i need to keep checking in a loop whether time elapsed more than say 1 hour.

the variable is actually in a format like this 123-20150925084520 where i need to extract the second part as timestamp and then compare with current timestamp to see the number of hours. something like datediff($date1,$date2,"h")

Here is what i tried. Logged into PUTTY and under username@host:[ahome/username]$ tried below from this SOPOst

$ echo $(( ( $(date -ud '2003-08-02 17:24:33' +'%h') - $(date -ud '2003-04-21 22:55:02' +'%h') )/60/60/24 )) days

which gives date : illegal option --d

i dont see --d option but i removed -ud and tried. gives date: bad conversion

Other options are implementing complex logics coming in 10 s of lines of code. it will take days for a unix newbie like me to understand the logic. Any quick help would be appreciated.

Community
  • 1
  • 1
xGen
  • 494
  • 3
  • 10
  • 27
  • what have you tried? S.O. isn't a free coding service. You're expected to show your work, preferably as a test case that people can cut/paste into their terminals (in the case of ksh). You should also include your expected out from the inputs. There are numerous functions posted here on S.O. in ksh that do date calculations. Did you try looking for them? Sorry, but Good luck. – shellter Sep 25 '15 at 02:57
  • http://unix.stackexchange.com/questions/24626/quickly-calculate-date-differences – Srini V Sep 25 '15 at 05:46
  • tried this link too. but getting some error. Ended up using unix time to get seconds and subtract it (one of the answers in the same post) to get hours. thanks – xGen Sep 25 '15 at 06:06

1 Answers1

1

Here is a simple solution found.

    $STARTTIME=20150925061225
    ENDTIME=$(date '+%Y%m%d%H%M%S')   #20150925090000
    TIMEDIFF=`expr $ENDTIME - $STARTTIME`
    echo $TIMEDIFF # returns 28775(2 hours)
    if [ TIMEDIFF -gt 50000 ];          # Is diff. Greater than 5 hours?
xGen
  • 494
  • 3
  • 10
  • 27