help-bash
[Top][All Lists]
Advanced

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

Re: Problem printing 24 hour times OR a stupid mistake


From: Chris Elvidge
Subject: Re: Problem printing 24 hour times OR a stupid mistake
Date: Sun, 9 Jan 2022 15:37:07 +0000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 Lightning/5.4

On 09/01/2022 14:13, Dennis Williamson wrote:
On Sun, Jan 9, 2022, 6:57 AM Chris Elvidge <celvidge001@gmail.com> wrote:

On 08/01/2022 15:15, Chris Elvidge wrote:
Given tim12="7:00 am" and tim24=$(date -d"$tim12" +"%H%M")

printf '%04d' "$tim24" prints 0488; '%04o' will output 0700

But if tim12="7:00 pm" and tim24=$(date -d"$tim12" +"%H%M")
'%04d' gives the correct output 1900 but '%04o' gives 3554

I have got round the problem with tim24=$(date -d"$tim12" +"%-H%M") i.e.
stripping leading zeros, but is there any way to force printf to
recognise 0700 as a decimal number?


The stupid mistake is treating 0700 as a number - it is a string after
all, %H%M is a 4 character sequence.
printf '%s\n' "$(date -d"$tim12" +"%H%M")" works perfectly.

--
Chris Elvidge
England




You should be able to omit the printf and just do

date -d"$tim12" +"%H%M"




Agreed, but to get it to print time+separator+programme description, all on one line I need printf '%s|%s\n' "$tim24" "$progname" (or even echo "$tim24|$progname"). Anyway, printing as %s instead of %04d removed the problem.
Cheers.


--
Chris Elvidge
England




reply via email to

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