help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: EPOCHREALTIME to days, hours, minutes, seconds


From: Alex fxmbsw7 Ratchev
Subject: Re: EPOCHREALTIME to days, hours, minutes, seconds
Date: Fri, 20 Aug 2021 02:24:14 +0200

i didnt really understood but wanted to share i tried a date converter in
awk and failed at leapyears and other unknown factors

On Fri, Aug 20, 2021, 02:22 Seth David Schoen <schoen@loyalty.org> wrote:

> 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.
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]