The following invocation of GNU date, using bash syntax, will return a counter which increments at an average rate of 1 per second, i.e., it will be in sync with TAI (not UTC), on average. We stress "on average" because weirdness will happen during a leap second, but it will return to the correct rate afterwards.
date -d 'TZ="right/UTC" '"$(date -u -Iseconds)" +'%s'
Both the -u flag and the double quotes around the embedded call to date are superfluous, but we include them for clarity.
Some testing of what happens at a leap second:
+ date -d 'TZ="right/UTC" 2015-06-30T23:59:59+0000' +%s
1435708824
+ date -d 'TZ="right/UTC" 2015-07-01T00:00:00+0000' +%s
1435708826
which is correct; 2 seconds have passed in that time interval. We are abusing the "right" timezone database, as explored earlier.
Beware what you do with this counter value. It is different from the POSIX counter value by 20 or more seconds, depending on the number of leap seconds that have occurred. Do not, for example, write this counter value to a file which another tool might read and interpret as a POSIX counter value. As a precaution, it might be better to use the following counter value instead, which will be obviously very different from a POSIX counter value, so less likely to be interpreted the wrong way.
expr 1000000000000 + $(date -d 'TZ="right/UTC" '"$(date -u -Iseconds)" +'%s')
This adds nanoseconds:
date -d 'TZ="right/UTC" '"$(date -u -Ins)" +'%s.%N'
No comments :
Post a Comment