Forcing "date" to use a locale other than the machine default
Asked Answered
C

8

43

Is there a way to force the *nix command "date" to output in a specific format independent of the local? For example, if I call "date -u" today, on a US machine I get:

Mon Oct 15 13:15:29 UTC 2012

but on a German machine I get:

Mo 15. Okt 13:15:31 UTC 2012
Celebrity answered 15/10, 2012 at 13:21 Comment(0)
D
65

Sure, you can always specify the format yourself:

date +%a, %b %d

or you can use a temporary environmental variable:

:~$ LC_ALL=de_DE.utf8 date
Mo 15. Okt 15:34:11 CEST 2012
:~$ date
Mon Oct 15 15:33:24 CEST 2012

As you see, only the first command is run with the German locale.

Drawing answered 15/10, 2012 at 13:34 Comment(9)
additionally you can use locale -a to list the installed locales which will work for the LC_ALL variableIrwinirwinn
I can't make it work the other way around. For example LC_ALL=it_IT.utf8 date -d'1 gennaio 2017' +%Y%m%d says: date: invalid date ‘1 gennaio 2017’Tedious
"In the current implementation, only English is supported for words and abbreviations like ‘AM’, ‘DST’, ‘EST’, ‘first’, ‘January’, ‘Sunday’, ‘tomorrow’, and ‘year’." (info page for date)Drawing
The correct variable is LC_TIME, that is LC_TIME=de_DE.utf8 date. It might not matter for the single command, but since the page is the first search result for a general query, it's probably worth mentioning.Baronage
In my shell I had to put date's parameters in a quote like this: date +"%a, %b %d"Khudari
@Baronage You are correct that LC_TIME affects how the date is printed but LC_ALL has a higher priority. I.e. if LC_ALL is set then LC_TIME is ignored. Therefore setting just LC_TIME does not guarantee a correct language.Beatrizbeattie
@Beatrizbeattie right, that does not mean though that setting LC_ALL instead of LC_TIME just to change time format is the correct solution. Rather it means that you might want to check that LC_ALL is not set. It is often the case that one wants to get a 24h time format instead of the AM/PM one, but they do not want to change the language (which is gonna happen if you set LC_ALL).Baronage
Actually, I'm sorry, but I figured I'll give this answer a downvote until it is edited. Because LC_ALL should only really be used for debugging purposes, exactly because you lose the more fine-grained control over the rest of LC_* variables. As a result of this answer people may set LC_ALL in the environment, and later then stumble upon problems similar to what @Beatrizbeattie mentioned. Nothing good in that.Baronage
nice, however, LC_ALL=es_ES.utf8; date --utc --date "30 dic 2023 16:35:20 GMT-8" +"%Y-%m-%d %H:%M" does not work as expected.Voodooism
S
16

You can modify the environment date runs in. I don't know if this is the best way (the variables used in locale handling are numerous), but the following works:

$ LANG=de_DE date
Mo 15 Okt 2012 09:34:12 EDT

(January beat me to this answer by a minute or so, but I'll leave my answer up in the hopes that some will clarify which variable (LC_ALL, LANG, other) is most "appropriate".)

Speer answered 15/10, 2012 at 13:35 Comment(2)
Your answer is more "appropriate", examples from POSIX standard: LANG=da_DK.iso_8859-1 date, LANG=De_DE.88591 date.Saltigrade
Definitions from POSIX: Lang can be used by applications to determine the language to use for error messages and instructions, collating sequences, date formats, and so on. LC_ALL determine the values for all locale categories and has precedence over any of the other environment variables.Saltigrade
V
8

LC_TIME is enough, you can use this.

LC_TIME="de_DE.UTF-8"  date
Vanessa answered 31/3, 2020 at 1:38 Comment(0)
H
6

Install the language-pack-xx

First, make sure that the language package of the target locale is installed. On Debian-derived GNU/Linux distributions, Dutch language support is installed as follows:

$ sudo apt install language-pack-nl

This command will automatically also install language-pack-nl-base. Here is what I get by specifying LC_TIME. This is more specific than using LC_ALL. (Specifying LANG does not work on Xubuntu LTS.)

$ date '+%A %e %B %Y'
Friday 16 October 2015

$ LC_TIME='nl_BE.UTF-8' date '+%A %e %B %Y'
vrijdag 16 oktober 2015
Humidify answered 16/10, 2015 at 18:58 Comment(2)
Doesn't work for me :-( LC_TIME='de_DE.UTF-8' date '+%A %e %B %Y' gives Saturday 3 December 2022Flagpole
@framp: did you do install language-pack-de before trying? and is de_DE.utf8 included in the output of locale -a ?Unlawful
L
4

Have you tried the following? (which I got from the date manual pages)

# date -u +"%a %b %d %T %Z %Y"
Lipase answered 15/10, 2012 at 13:28 Comment(1)
Thanks, this is a step in the right direction, but I want the day and month names (which are in German on a German machine) to switch to English as well.Celebrity
P
2

UTC:

date -u -R

Local:

date -R
Parthenia answered 9/3, 2014 at 21:4 Comment(0)
K
1

First, uncomment your desired locale(s) lines in /etc/locale.gen then run locale-gen (both as root). Make sure your terminal supports the required encoding so the characters will get shown.

You can then specify a "temporary" locale for the run-time of one command like this:

LC_ALL="de_DE.UTF-8" date
Khudari answered 21/7, 2019 at 7:37 Comment(0)
C
1

you can also use this knowledge to write date and time information used in scripts for the benefit of speakers of other languages, for example:

    LC_TIME="es_PR.utf8" date '+El programa concluyó el %A, %e de %B de %Y, a las %H horas, %M minutos'
Creamer answered 30/9, 2023 at 3:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.