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:06:53 +0300

On Fri, Aug 20, 2021 at 06:39:04PM +0200, Emanuele Torre 
(torreemanuele6@gmail.com) wrote:

> 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]*}"

Good point. Though of course that may break in locales that
have additional digit characters (say, Arabic or Lao).
So instead of [!0-9] it'd be better to use [^[:digit]] in there:

    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:]]*}"

Incidentally, [![:digit:]] does not work there, you need to use the
POSIX-specified caret (^) instead of an exclamation mark when using
character classes. I'm not sure if this is intentional or a bug in
bash; man page doesn't seem to mention it.

-- 
Tapani Tarvainen



reply via email to

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