How do you change the time of the Linux hardware (hwclock) clock to 24-hour format?

0

Currently, my hardware clock returns the date/time (hwclock --show) like this:

Sat 28 Jul 2018 08:47:47 PM BST .659124 seconds

And date returns:

Sat Jul 28 21:23:13 BST 2018

I'd like to change the time of the hardware clock to 24-hour format and the format of date to match the hardware clock's date/time format.

I have searched high and low but haven't found thorough documentation on managing date and time on Linux systems (i.e., short, medium and long date, regional formats such as DD-MM-YYYY or DD/MM/YY), especially in a command line environment.

I have man'ed hwclock and date. They don't help. I also know that there are a set of similar questions that have been asked before. I have looked at them and they don't help either.

Can someone help with regards to my specific question noted above or point me to some thorough documentation with a discussion on how to deal with multiple date/time scenarios for different regions in a command line environment? Thanks in advance.

user928397

Posted 2018-07-28T20:31:33.210

Reputation:

1What Linux (distro) and version are you using? – Alex – 2018-07-28T20:49:29.170

1Do you really want to reset bit 2 of Status Register B, or do you just want to display the time in 24h format? – Ignacio Vazquez-Abrams – 2018-07-28T20:52:19.713

2Also, cannot duplicate. I get ISO 8601 format just like the man page says. – Ignacio Vazquez-Abrams – 2018-07-28T20:55:45.063

what locale and timezone are you using? – Jasen – 2018-07-28T23:30:20.370

what does this get you? LC_TIME=C hwclock --show – Jasen – 2018-07-28T23:34:36.360

Answers

2

You can make date do the work for you: the -d option allows you to specify the date in a "mostly free format human readable date string" (from man date).

Unfortunately, the format isn't quite free enough to accept the hwclock output, but it works fine if you remove the final field specifying fractions of a second:

date -d "$(hwclock|sed 's/  \..*$//g')"

I have tested, but not managed to hit upon the format when there are zero fractions of a second, so I don't know if it would show the same format, eg Sat 28 Jul 2018 08:47:47 PM BST .000000 seconds. If there is no decimal point, another sed replacement will be needed.

Provided that there is still a double space after the time zone, the following will work:

date -d "$(hwclock|sed 's/  .*$//g')"

You can make this a shell script or a function defined in ~/.bashrc called something like hwclockdf (ie in date format).

AFH

Posted 2018-07-28T20:31:33.210

Reputation: 15 470