TimeStamp Comparison in Linux
TimeStamp Comparison in Linux
Comparing Date, Time or TimeStamp is tricky thing in Linux. This is how you can achieve this By converting date to Seconds
$ date +%s
the above command will current time in seconds since 1970-01-01 00:00:00 UTC
if you have time in string form you can use below command to convert them like $ date -d "$ts" +%s
#where ts is containing the date or timestamp
i.e -
ts1="2008-02-13 10:48:58.502075"
date -d "$ts1" +%s
1202928538
# so for comparison of timestamps -ts1="2008-02-13 10:48:58.502075"
ts2="2008-12-15 16:00:00.000000"
if [ $( date -d "$ts1" +%s ) -gt $( date -d "$ts2" +%s ) ]; then echo $ts1;
else echo $ts2; fi
2008-12-15 16:00:00.000000