Productive Linux


 Subscribe via Feedburner in a reader

Enter your email address:

Delivered by FeedBurner


Don't have an account?
Sign up to
Forgot your password?

Locale: Customising and Making Your Own Date and Time
2 February 2011 @ 15:05 GMT
by Paul

The 'locale' system on every Linux computer is a way of customising the way certain things work.

It determines how numbers and currencies should be displayed, sets the paper size for printing, and the formats for time and date, amongst other things. In short, the 'locale' is a way to ensure a computer respects certain practices in certain countries or language communities.

The 'locale' system forms part of glibc, the GNU C Library, which is at the base of many pieces of software.

Like everything in Linux, it is possible to change the 'locale' files to suit particular tastes or practices. You might want to do this if, for example, you don't like the way dates are displayed.

I found that the information out there about locales and how to change is fairly sparse. Here I've gathered what I've found.

On Debian systems and perhaps most other forms of Linux, the local files are in /usr/share/i18n/locales/. All locales are named in the following way: xx_YY, where xx is a two character name for a language and YY is the two character name for a country. So en_US, will be the locale for English in the US, en_GB the locale for English in Great Britain and cy_GB the locale for Welsh spoken in Great Britain.

Typing 'locale' at the command prompt will give you a list of certain environment variables and their locale settings. The environment variable LC_TIME determines how the time is displayed, whereas the environment variable LC_NUMERIC determines how numbers are displayed, for example, what characters to use for the decimal point and thousands seperator.

If you use the 'locale' command with the 'k' switch, say locale -k LC_NUMERIC, you will get a list of names and values for select keywords. For example, if your locale is en_GB, the decimal_point keyword will be set to '.', whereas if your locale is set to fr_FR, it will be set to ','.

Why I got interested in locales was because of the way that dates were being displayed in the cs_CZ locale.

How dates and times are displayed is determined by the LC_TIME environment variable. The settings can be determined by doing locale -k LC_TIME. The settings that interested me are as follows:

this determines how the 'date' command without any arguments dispays the date.
defines how the date and time are displayed when the '%c' format sequence is used, as in date +%c
defines how the date is displayed using the '%x' format as in date +%x
defines how the time is displayed as if with the '%X' format as in date +%X.

The best way to change a locale, at least on my system, is to create a custom locale, using a file name containing '@'. So, if you want to amend the en_GB locale you copy it to something like en_GB@custom, then edit it.

There are a few notes on what to do here. These are the steps I recommend:

  1. Test how you would like the date command to display a date by reference to the date man page, for example, you might like the date displayed using this format string: "%A %-e. %b %Y %H.%M:%S %:z".
  2. Convert the format string to unicode code points. A conversion tool is here.
  3. Edit the values of the formats using the string of code points. So that it looks something like this: date_fmt="<U0025><U0041><U0020><U0025><U002D><U0065><U002E><U0020><U0025><U0062><U0020><U0025><U0059><U0020><U0025><U0048><U002E><U0025><U004D><U003A><U0025><U0053><U0020><U0025><U003A><U007A>"
  4. Copy your edited locale file to /usr/share/i18n/locales.
  5. Edit the /etc/locale.gen to make sure that you compile your new custom locale file, like this echo en_XX.UTF-8 UTF-8 >> /etc/locale.gen
  6. Run locale_gen

Now, you can set the custom locale as your default or put it in your profile file on a per user basis, or just at the command line: export LC_TIME=en_GB.utf8@custom.

More information:

Comments disabled