The following function will get the "begin_time
" and "end_time
" from "$(date +%s%N)
", and most of the time (99.99%), it will work as expected. But sometime (0.01%) the "end_time
" will less than the "begin_time
" which is unexpected and should not be happen!
Here is the test function and run in bash on Ubuntu 22.04:
test_date() { local begin_time_in_nanoseconds="$(($(date +%s%N)))" local url="https://www.example.com/" curl --no-keepalive --retry-max-time 0 --retry 0 --silent "$url" local end_time_in_nanoseconds="$(($(date +%s%N)))" local diff_in_nanoseconds="$(($end_time_in_nanoseconds - $begin_time_in_nanoseconds))" printf "%s\n" "<$(date +"%F %T.%N")> ---1--- begin_time_in_nanoseconds: $begin_time_in_nanoseconds , end_time_in_nanoseconds: $end_time_in_nanoseconds , diff_in_nanoseconds: $diff_in_nanoseconds"}
Here is the log output for explaining this situation:
<2024-12-20 11:24:02.266961654> ---1--- begin_time_in_nanoseconds: 1734665343131348800 , end_time_in_nanoseconds: 1734665042230567454 , diff_in_nanoseconds: -300900781346...<2024-12-20 12:10:49.138950498> ---1--- begin_time_in_nanoseconds: 1734667843436826498 , end_time_in_nanoseconds: 1734667849106611998 , diff_in_nanoseconds: 5669785500...<2024-12-20 13:08:09.083870798> ---1--- begin_time_in_nanoseconds: 1734671283709470498 , end_time_in_nanoseconds: 1734671289051147298 , diff_in_nanoseconds: 5341676800
The 1st log is "unexpected", and the 2nd and 3rd log are "expected".
Why is the "end_time_in_nanoseconds
" less than "begin_time_in_nanoseconds
" in the 1st log?