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: Emanuele Torre
Subject: Re: EPOCHREALTIME to days, hours, minutes, seconds
Date: Fri, 20 Aug 2021 18:39:04 +0200

>        now=$EPOCHREALTIME
>        printf -v seconds "%(%S)T.%s" "${now%.*}" "${now#*.}"
>        printf -v minutes '%(%M)T' "${now%.*}"
>        printf -v hours '%(%H)T' "${now%.*}"

Also, EPOCHREALTIME may not have "." as the decimal delimiter since it
uses the one of the current locale (LC_NUMERIC). So it's more
appropriate to use [!0-9] rather than ".".

        now=$EPOCHREALTIME
        printf -v second '%(%S)T.%s' "${now%[!0-9]*}" "${now#*[!0-9]}"
        printf -v minute '%(%M)T' "${now%[!0-9]*}"
        printf -v hour '%(%H)T' "${now%[!0-9]*}"

--
 emanuele6

On 20/08/2021, Andreas Kusalananda Kähäri <andreas.kahari@abc.se> wrote:
> On Fri, Aug 20, 2021 at 04:34:20PM +0300, Tapani Tarvainen wrote:
>> On Fri, Aug 20, 2021 at 12:22:21PM +0000, hancooper
>> (hancooper@protonmail.com) wrote:
>>
>> > > 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 ?
>>
>> The Gnu date -based solution I posted earlier deals with them,
>> but the above can be modified to do it as well:
>>
>> now=$EPOCHREALTIME
>> printf -v seconds "%(%S)T.${now#*.}" "${now%.*}"
>> printf -v minutes '%(%M)T' "${now%.*}"
>> printf -v hours '%(%H)T' "${now%.*}"
>>
>> --
>> Tapani Tarvainen
>
> Don't inject data into the printf format string.  Instead:
>
>       now=$EPOCHREALTIME
>       printf -v seconds "%(%S)T.%s" "${now%.*}" "${now#*.}"
>       printf -v minutes '%(%M)T' "${now%.*}"
>       printf -v hours '%(%H)T' "${now%.*}"
>
> --
> Andreas (Kusalananda) Kähäri
> SciLifeLab, NBIS, ICM
> Uppsala University, Sweden
>
> .
>
>



reply via email to

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