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: Tapani Tarvainen
Subject: Re: EPOCHREALTIME to days, hours, minutes, seconds
Date: Fri, 20 Aug 2021 22:58:20 +0300

On Fri, Aug 20, 2021 at 10:06:53PM +0300, Tapani Tarvainen 
(bash@tapanitarvainen.fi) wrote:

>     now=$EPOCHREALTIME
>     printf -v second '%(%S)T.%s' "${now%[^[:digit:]]*}" "${now#*[^[:digit:]]}"
>     printf -v minute '%(%M)T' "${now%[^[:digit:]]*}"
>     printf -v hour '%(%H)T' "${now%[^[:digit:]]*}"

Instead of [^[:digit:]], [[:punct:]] should also work.

Alternatively, you could explicitly use the decimal point
of current locale:

   now=$EPOCHREALTIME
   DP=$(locale decimal_point)
   printf -v second '%(%S)T.%s' "${now%${DP}*}" "${now#*${DP}}"
   printf -v minute '%(%M)T' "${now%${DP}*}"
   printf -v hour '%(%H)T' "${now%${DP}*}"

Question: If you want to stick to bash builtins only (avoid the
locale command), is there any better way to determine the decimal
point than this:

printf -v DP "%.1f" 0
DP=${DP%?}
DP=${DP#?}

-- 
Tapani Tarvainen



reply via email to

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