How do I find the number of seconds till midnight of current day?
-
Doens't that rather belong to stackoverflow?– BenoitMar 16, 2011 at 15:14
-
@Benoit : I considered that, but decided against because I thought that this would be scripting, not programming.– bguizMar 16, 2011 at 23:02
-
Warning: you are trying to do interval arithmetic with absolute times of day. Think carefully about what happens around DST transitions.– PhilRMay 18, 2011 at 14:00
-
@Phil : Yup, a solution which takes DST (and other corner cases) would be ideal– bguizMay 23, 2011 at 13:02
Add a comment
|
3 Answers
What about:
echo $(($(date -d "$(date +00:00-24:00)" +%s)-$(date +%s)))
-
2
-
-
If you have a version of date that supports it:
echo $(($(date -d 23:59:59 +%s) - $(date +%s) + 1))
or
echo $(($(date -d 'tomorrow 00:00:00' +%s) - $(date +%s)))
If you want a solution which works always try this:
$((`date +%s` % 86400 + $OFFSET ))
The $OFFSET
should be the difference in seconds between the local timezone and UTC.
-
Does this work at the transition of winter/summer time (... wasting time)? Aug 15, 2019 at 7:04