[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? ...



On Fri, Jul 18, 2025 at 12:27:04AM +0200, lbrtchx@tutamail.com wrote:
> _HHMMSS="19:09" means 19*60 + 9 = 1149 seconds
> _HHMMSS="19:08" means 19*60 + 8 = 1148 seconds

So: 19:09 is 19 minutes and 9 seconds ?

It is not HHMMSS for that you should have _HHMMSS="00:19:09"

I found a bug in my solution --- time zone problems, so setting to GMT is needed:

_HHMMSS="19:09:00"
seconds=$( TZ=GMT date --date="1 january 1970 $_HHMMSS" "+%s" )
echo seconds=$seconds
seconds=68940

_HHMMSS="00:19:09"
seconds=$( TZ=GMT date --date="1 january 1970 $_HHMMSS" "+%s" )
echo seconds=$seconds
seconds=1149

> so, it seems date is also somehow parsing that value as octal even though it is being formatted in a date friendly way. is there a way to exactly specify the encoding of the input for date?

-- 
Alain Williams
Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
+44 (0) 787 668 0256  https://www.phcomp.co.uk/
Parliament Hill Computers. Registration Information: https://www.phcomp.co.uk/Contact.html
#include <std_disclaimer.h>


Reply to: