1

I have a two dates in date +"%Y%m%d%H%M%S" format. How to find the difference and how to check whether the difference is more than 4 hours?

This is how i have tried

echo $(( ( $(date +"%Y%m%d%H%M%S") - $(date +"%Y%m%d%H%M%S" -d "1970-01-01 + $(stat -c '%Z' filename ) secs"))))

date command returns this

Sun Sep  6 10:35:19 CDT 2015
  • If they are truely in +"%Y%m%d%H%M%S" format you will have to parse them first since the year and month will run together in the string and give an invalid date error when you tryu to read. – user1794469 Sep 6 '15 at 15:29
4

Having e.g.:

date1=20150906171119
date2=20150906172244

a sample / start could be:

#!/bin/bash

date1=20150906171119
date2=20150906172244

# Convert date to YYYYMMDD HH:MM:SS and get time in seconds.
d1=$(date -d"${date1:0:8} ${date1:8:2}:${date1:10:2}:${date1:12:2}" +%s)
d2=$(date -d"${date2:0:8} ${date2:8:2}:${date2:10:2}:${date2:12:2}" +%s)

# Limit in seconds
# 60    = 1 min
# 3600  = 1 hour
# 14400 = 4 hours
lim=14400

# arg1=time1 (seconds)
# arg2=time2 (seconds)
# arg3=difference limit
ddiff()
{
    if (($1 > $2)); then
        ddiff $2 $1 $3
    else
        return $(($2 - $1 < $3))
    fi
}

if ddiff $d1 $d2 $lim; then
    echo more then $lim seconds
else
    echo less then $lim seconds
fi

You could throw in an equality check, date1 = date2, if that is important.

  • yea this will help , what if the file is older than 1 day? will this work? – VRVigneshwara Sep 6 '15 at 17:21
  • @VRVigneshwara: You get the time in seconds since Epoch, 1970. As long as you do not have files older then 45 years and 8 months + change, it should not be an issue. But, if the dates differ in time-zones, you'll have unreliable results. – Runium Sep 6 '15 at 18:06
3

You cannot do this as there is no timezone in that format.

"%Y-%m-%d %H:%M:%S %z"

is the format that is standardized for e.g. diff -u and that format includes the timezone.

  • This seems like a comment rather than an answer. – user1794469 Sep 6 '15 at 15:30
  • Well, it is the answer, note that you cannot compute a difference between two values if you cannot know if one might be using daylight saving time. – schily Sep 6 '15 at 16:41
  • Well, you can ignore DST (presumably both times being compared are from the same system and in the same time zone) and you can parse the string to make it compatible with date as Sukminder has shown. – terdon Sep 6 '15 at 17:06
  • If you were right, then the old timestamps from the SCCS history files and what was used by early RCS implementations was not a problem. Unfortunately, even or in special two timevalues from the same location that are only a few minutes apart cause the problem when there is a DST switch in between. So you could believe that the second time is 30 minutes before the first while it really was 90 minutes after the first one. – schily Sep 6 '15 at 17:20
  • If you however add %z to the format, you know the GMT offset in minutes and then everything is OK, BTW: this is used in the SCCSv6 history format. – schily Sep 6 '15 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.