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.

  • 1
    What Linux (distro) and version are you using? – Alex Jul 28 '18 at 20:49
  • 1
    Do 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 Jul 28 '18 at 20:52
  • 2
    Also, cannot duplicate. I get ISO 8601 format just like the man page says. – Ignacio Vazquez-Abrams Jul 28 '18 at 20:55
  • what locale and timezone are you using? – Jasen Jul 28 '18 at 23:30
  • what does this get you? LC_TIME=C hwclock --show – Jasen Jul 28 '18 at 23:34
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).

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.