How to compare two time stamps?
Solution 1
Try this,
The time 2017-03-25 05:52:09.373941000
can be converted to the first format 24MAR17:00:14:09
and then do a comparison.
date_in="24MAR17:00:14:09"
date_out=`date +%d%b%y:%H:%M:%S -d "2017-03-25 05:52:09.373941000" | tr '[:lower:]' '[:upper:]'`
[ $date_in == $date_out ] && echo "The dates match"
Solution 2
The main problem is to parse the strings provided. The command date accepts just some formats, not all. The solution is to take advantage of busybox's date ability of accepting (almost) any format.
D1
Create a format string that prints a date in the same format as supplied:
$ date -u +'%d%b%y:%T' 07Jul17:15:18:48
Use that string to tell busybox date how to parse the date string. Be aware that the local TZ may change the result, use -u (UTC) to avoid mistakes, or make sure to set the correct TZ for the string of time to be parsed.
$ d1='24MAR17:00:14:09' $ busybox date -u -D '%d%b%y:%T' -d "$d1" Fri Mar 24 00:14:09 UTC 2017
Make busybox date to print the value in seconds (since epoch):
$ busybox date -u -D '%d%b%y:%T' -d "$d1" +'%s' 1490314449
D2
You may repeat the procedure for the second timestamp as well, but, in this case, the string is understood by date directly:
$ d2='2017-03-25 05:52:09.373941000'
$ date -u -d "$d2" +'%s'
1490421129
And busybox date removing the nanoseconds (${d2%.*}
) (more about this later):
$ busybox date -u -d "${d2%.*}" +'%s'
1490421129
Diff
You should have captured results above in two variables:
$ resd1="$(busybox date -u -D '%d%b%y:%T' -d "$d1" +'%s')"
$ resd2="$(date -u -d "$d2" +'%s')"
Then, calculate the difference and compare to zero:
$ (( resd1-resd2 )) && echo "Dates are different" || echo "Dates are equal"
Dates are different
Or directly compare the strings (not the values):
$ [[ "$resd1" == "$resd2" ]] && echo "equal" || echo "different"
different
Nanoseconds
There are three issues if you need to compare also nanoseconds.
The first value does not have nanoseconds. The first is that the first date does not have the nanoseconds value.
You may append 9 zeros:$ resd1="$(busybox date -u -D '%d%b%y:%T' -d "$d1" +'%s')" $ resd1="$resd1""$(printf '%0*d' 9 0)" $ echo "$resd1"echo "$resd2 - $resd1" 1490314449000000000
Or (better) re-process the value with date:
$ resd1="$(date -u -d "@$resd1" +'%s%N')" $ echo "$resd1" 1490314449000000000
The second date has nanoseconds. In this case the second date has a format that date could directly process, but if it had a format that required pre-processing by busybox date, we will need to append the nanoseconds later. As an example:
$ d2='2017-03-25 05:52:09.373941000' $ resd2="$(busybox date -u -D '%Y-%m-%d %T' -d "$d2" +'%s')${d2##*.}" $ echo "$resd2" 1490421129373941000
And, the bash you are running needs to be able to process 64 bit integers.
If so, calculate the difference:$ echo "(( $resd2 - $resd1 ))" (( 1490421129373941000 - 1490314449000000000 )) $ echo "$(( $resd1 - $resd2 ))" 106680373941000
Which is a little over 106 thousand seconds (~29 hours):
$ echo "$(( ($resd2 - $resd1)/10**9 ))" 106680
Solution 3
bash solution:
#!/bin/bash
d1='24MAR17:00:14:09'
d2='2017-03-25 05:52:09.373941000'
d1="$(sed -E 's/^([0-9]{2})([A-Z]{3})([0-9]{2}):/\1-\2-\3 /' <<< $d1)"
if [[ $(date -d "$d1") == $(date -d "$d2") ]]
then
echo "dates are equal"
else
echo "dates are unequal"
fi
Related videos on Youtube
![Rahul](https://i.stack.imgur.com/LYsF3.jpg?s=256&g=1)
Rahul
Updated on September 18, 2022Comments
-
Rahul almost 2 years
I have two time stamps one is like this
24MAR17:00:14:09
and another is like this2017-03-25 05:52:09.373941000
and i want to compare the two time stamps same or not i tried a lot date function and awk function but nothing giving me results please help me.-
RomanPerekhrest almost 7 yearswhat is your comparison?
-
Rahul almost 7 yearsI want to compare those two time stamps whether they are same or not
-
roaima almost 7 yearsThose timestamps cannot be equal because they have different precision. Or are you only interested in a comparison to the second? Nearest second? Truncated to the current second?
-