KEMBAR78
TimeStamp Comparison in Linux | PDF
0% found this document useful (0 votes)
109 views1 page

TimeStamp Comparison in Linux

Comparing timestamps in Linux can be tricky. The document outlines how to convert timestamps to seconds since January 1, 1970 UTC using the date command and +%s format. It also shows how to convert string timestamps to seconds and compare them by checking if one timestamp in seconds is greater than the other. The example compares two timestamps, ts1 and ts2, and prints the later timestamp, ts2, since its value in seconds is greater than ts1's.

Uploaded by

DataStage4You
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views1 page

TimeStamp Comparison in Linux

Comparing timestamps in Linux can be tricky. The document outlines how to convert timestamps to seconds since January 1, 1970 UTC using the date command and +%s format. It also shows how to convert string timestamps to seconds and compare them by checking if one timestamp in seconds is greater than the other. The example compares two timestamps, ts1 and ts2, and prints the later timestamp, ts2, since its value in seconds is greater than ts1's.

Uploaded by

DataStage4You
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 1

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

You might also like