[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: EPOCHREALTIME to days, hours, minutes, seconds
From: |
Seth David Schoen |
Subject: |
Re: EPOCHREALTIME to days, hours, minutes, seconds |
Date: |
Thu, 19 Aug 2021 17:22:00 -0700 |
Alex fxmbsw7 Ratchev writes:
> ~ $ d=$EPOCHREALTIME dd=${d%.*} t=( $( printf '%(%FT%T)T %(%z)T' $dd $dd )
> ) t[1]=${t[1]::3}:${t[1]:3} r=$( date -d "${t[*]}" +%FT%T.${d#*.}%z ) ;
> declare -p r
> declare -- r="2021-08-19T21:55:07.872144+0200"
>
> On Thu, Aug 19, 2021, 20:38 hancooper via <help-bash@gnu.org> wrote:
>
> > Is there a neat way to convert from decimal seconds (result from
> > EPOCHREALTIME) to days, hours, minutes, seconds?
If you're looking for an interval (e.g. "4 years") rather than a date
(e.g. "1974-01-01T00:00:00"), then you can start with a value in the
shell variable "seconds" and then do something like
years=$((seconds/31536000))
seconds=$((seconds/31536000))
days=$((seconds/86400))
seconds=$((seconds%86400))
hours=$((seconds/3600))
seconds=$((seconds%3600))
minutes=$((seconds/60))
seconds=$((seconds%60))
Then the shell variables "years", "days", "hours", "minutes", and "seconds"
will contain integers, like
echo "$years years, $days days, $hours hours, $minutes minutes, $seconds
seconds"
A more concise but harder to read version would be
for scale in 31536000 86400 3600 60; do
echo -n "$((seconds/scale)) "
seconds=$((seconds%scale))
done; echo $seconds
A problem here this is that it ignores leap days and leap seconds in
this conversion, meaning that the values that you get this way can't be
directly aligned with a calendar. (Because precisely how long a "year"
is in seconds really depends on which particular calendar year; there's
no single answer to that question.) But the values you get from this
calculation would be meaningful to a human reader and can also be
compared with one another, at least!
For a more astronomy-oriented approach you could say that a year is
31556926 seconds instead of 31536000; this is a pretty accurate value on
average in astronomical terms.
- EPOCHREALTIME to days, hours, minutes, seconds, hancooper, 2021/08/19
- Re: EPOCHREALTIME to days, hours, minutes, seconds, Alex fxmbsw7 Ratchev, 2021/08/19
- Re: EPOCHREALTIME to days, hours, minutes, seconds, Tapani Tarvainen, 2021/08/20
- Re: EPOCHREALTIME to days, hours, minutes, seconds, Andreas Kusalananda Kähäri, 2021/08/20
- EPOCHREALTIME to days, hours, minutes, seconds, hancooper, 2021/08/20
- Re: EPOCHREALTIME to days, hours, minutes, seconds, Alex fxmbsw7 Ratchev, 2021/08/20
- Re: EPOCHREALTIME to days, hours, minutes, seconds, Alex fxmbsw7 Ratchev, 2021/08/20
- EPOCHREALTIME to days, hours, minutes, seconds, hancooper, 2021/08/20
- Re: EPOCHREALTIME to days, hours, minutes, seconds, Alex fxmbsw7 Ratchev, 2021/08/20
- Re: EPOCHREALTIME to days, hours, minutes, seconds, Tapani Tarvainen, 2021/08/20
- Re: EPOCHREALTIME to days, hours, minutes, seconds, Andreas Kusalananda Kähäri, 2021/08/20
- Prev by Date:
Re: EPOCHREALTIME to days, hours, minutes, seconds
- Next by Date:
Re: EPOCHREALTIME to days, hours, minutes, seconds
- Previous by thread:
Re: EPOCHREALTIME to days, hours, minutes, seconds
- Next by thread:
Re: EPOCHREALTIME to days, hours, minutes, seconds
- Index(es):