help-bash
[Top][All Lists]
Advanced

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

EPOCHREALTIME to days, hours, minutes, seconds


From: hancooper
Subject: EPOCHREALTIME to days, hours, minutes, seconds
Date: Fri, 20 Aug 2021 12:22:21 +0000

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Friday, August 20, 2021 7:52 AM, Andreas Kusalananda Kähäri 
<andreas.kahari@abc.se> wrote:

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


How can I deal with seconds that includes fractional part ?

This produces error

Elapsed Time bash: printf: 13.506899: invalid number
bash: printf: 13.506899: invalid number
bash: printf: 13.506899: invalid number
bash: printf: 13.506899: invalid number


now=$EPOCHSECONDS
printf -v seconds '%(%S)T' "$t"




reply via email to

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