Re: EPOCHREALTIME to days, hours, minutes, seconds
From:
Andreas Kusalananda Kähäri
Subject:
Re: EPOCHREALTIME to days, hours, minutes, seconds
Date:
Fri, 20 Aug 2021 09:52:33 +0200
On Fri, Aug 20, 2021 at 10:04:27AM +0300, Tapani Tarvainen wrote:
> On Thu, Aug 19, 2021 at 06:38:12PM +0000, 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 are treating it as a date (rather than a time difference),
> you can do simply
>
> A=$EPOCHREALTIME
> date -d @$A +'%d %H:%M:%S'
>
> Adjust format to taste.
>
> If it's a time difference but less than a year, and you want days,
> hours, minutes and seconds in variables, you can do it like this:
>
> eval $(date -d @$A +'days=%j hours=%H minutes=%M seconds=%S.%N')
Quoting would be nice.
> days=$((days-1))
>
> If you want to discard extra zeroes:
>
> seconds=${seconds%000}
> seconds=${seconds#0}
> hours=${hours#0}
> minutes=${minutes#0}
If you're using bash but don't have other GNU tools installed, and only
want second granularity, then use printf instead:
now=$EPOCHSECONDS
printf -v seconds '%(%S)T' "$now"
printf -v minutes '%(%M)T' "$now"
printf -v hours '%(%H)T' "$now"
--
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden
.