Changing the Default Date Format on a Raspberry Pi

Fun fact, Canada adopted the ISO-8601 date format yyyy-mm-dd on 1989-12-31, however, after experiencing the messy failure of metrication in the 70s, many Canadians stuck to the American mm/dd/yyyy or European dd/mm/yyyy date formats. Canada is one of the only countries to use all three date formats, yet somehow software localization formats for Canada are most often mm/dd/yyyy.

“Date and time notation in Canada combines conventions from the United Kingdom, the United States, and France, often creating confusion.”

wikipedia.org/wiki/Date_and_time_notation_in_Canada


The default date time format for Canada on the Pi is a hot mess.

pi@raspberrypi:~ $ date
Sat 09 Jan 2021 10:46:20 PM MST

My feelings are summed up pretty well in this XKCD comic.

So, the default date format is one of the first things I change on a new device. It was surprisingly challenging to change the default date and time format on my shiny new Pi. None of the blog posts I found, nor the example in the man locale pages worked, so I thought I’d document what worked for me here.

My Pi

  • Model: Raspberry Pi 4 Model B
  • OS: Raspberry Pi OS with desktop (GNU/Linux)
  • Release date: 2020-12-02
  • Kernel release: 5.4.83-v7l+
  • Patches up to date as of 2021-01-09

Files

I have shared the files that are edited in this process in a GitHub repo for reference: github.com/marcleblanc2/pi/tree/main/date-format

Steps

1. Create a new .locale directory in your home directory

mkdir ~/.locale

2. Change to your new .locale directory

cd ~/.locale

3. Copy the original locale file to your new .locale directory

cp /usr/share/i18n/locales/en_CA ~/.locale/en_CA_Custom

4. Edit your new locale file

nano ~/.locale/en_CA_Custom

5. Find these three lines in your new locale file, and replace the default format with your chosen format

d_t_fmt "%F %T %z"
d_fmt "%F"
t_fmt "%T"

6. I also added this line in the new locale file, as I found it referenced in the man file for date, but I haven’t tested it independently to see if it makes any difference

date_fmt "%F %T %z"

7. Save your new locale file and exit your editor

8. Generate a new locale from your new locale file

localedef -f UTF-8 -i ~/.locale/en_CA_Custom ~/.locale/en_CA_Custom.UTF-8

9. List the locales available on your Pi to verify the generation worked, and to get the exact name of the generated locale

locale -a

10. Test the newly generated locale; this should output the current date in your new format

LOCPATH=~/.locale LC_ALL=en_CA_Custom.UTF-8 date

11. Repeat steps 4 – 10 until you’re happy with the format

12. Edit your environment file

sudo nano /etc/environment

13. Add this line to your environment file, to define the LOCPATH environment variable, and assign it the value of the path to your new .locale directory created in step 1

LOCPATH=/home/pi/.locale

14. Save your environment file and exit your editor

15. Edit your default locale file

sudo nano /etc/default/locale

16. Change the locale names in in the variable values to the name of the generated locale.
Note the difference in .utf8 vs .UTF-8, and be sure to match the formatting already in the file.

LANG=en_CA_Custom.UTF-8
LC_ALL=en_CA_Custom.UTF-8
LANGUAGE=en_CA_Custom.UTF-8

17. Save your default locale file and exit your editor

18. Reboot your Pi

sudo reboot

19. Test it again

date

20. Celebrate

References

  1. This is the blog post that my Google search results and forums linked to the most, but it’s a few years old, and the UTF-8 formatting conversion is no longer necessary: ccollins.wordpress.com/2009/01/06/how-to-change-date-formats-on-ubuntu
  2. thomas-krenn.com/en/wiki/Configure_Locales_in_Ubuntu
  3. tecmint.com/set-system-locales-in-linux

Congratulations, you’ve made it this far! To celebrate, have another laugh at Canada. I promise we’re a real country.

Leave a Reply

Discover more from LeBlanc IT Inc

Subscribe now to keep reading and get access to the full archive.

Continue reading