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

Re: Is there a POSIX compliant way of turning a "HH:MM:SS" formatted string to seconds? ...



echo $(( $(echo 07:08:09 | sed -e 's/^0*\([0-9]\)/3600 * \1 +
/;s/:0*\([0-9]:\)/60 * \1/;s/:0*\([0-9]\)$/ + \1/;') ))

On Fri, Jul 18, 2025 at 1:31 PM Greg Wooledge <greg@wooledge.org> wrote:
>
> On Fri, Jul 18, 2025 at 13:16:17 -0700, Michael Paoli wrote:
> > E.g.:
> > printf 01:02:03 | (IFS=: read h m s; printf '%s\n' $(( $((h * 3600)) +
> > $((m * 60)) + $s )))
>
> You still need to strip leading zeroes.
>
> hobbit:~$ echo 01:08:09 | (IFS=: read h m s; printf '%s\n' $(( $((h * 3600)) +
> > $((m * 60)) + $s )))
> bash: 08: value too great for base (error token is "08")
>


Reply to: