2

How do I change the format of date command by modifying LC_TIME in locale?

Currently the day of month uses %e format. I need it to be displayed in %d format.

Below is the Current Format:

#date
Thu Aug 9 18:26:11 IST 2018

Expected format:

#date
Thu Aug 09 18:26:11 IST 2018

Here is my locale:

#locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE=en_IN
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

LC_TIME section in /usr/share/i18n/locales/en_US:

    LC_TIME
abday   "Sun";"Mon";"Tue";"Wed";"Thu";"Fri";"Sat"
day "Sunday";/
    "Monday";/
    "Tuesday";/
    "Wednesday";/
    "Thursday";/
    "Friday";/
    "Saturday"

week 7;19971130;1
abmon   "Jan";"Feb";/
    "Mar";"Apr";/
    "May";"Jun";/
    "Jul";"Aug";/
    "Sep";"Oct";/
    "Nov";"Dec"
mon "January";/
    "February";/
    "March";/
    "April";/
    "May";/
    "June";/
    "July";/
    "August";/
    "September";/
    "October";/
    "November";/
    "December"
% Appropriate date and time representation (%c)
d_t_fmt "%a %d %b %Y %r %Z"
%
% Appropriate date representation (%x)
d_fmt   "%m//%d//%Y"
%
% Appropriate time representation (%X)
t_fmt   "%r"
%
% Appropriate AM/PM time representation (%r)
t_fmt_ampm "%I:%M:%S %p"
%
% Strings for AM/PM
%
am_pm   "AM";"PM"
date_fmt "%F %Z"
END LC_TIME

Please let me know what I can do in order to get the expected format.

NOTE: I need to execute just date command without any formatting options.

Gowtham
  • 291
  • 1
  • 5
  • 16
  • SInce `strings $(type -p date) | grep LC` shows no output, it is clear that `date` is not influenced by `LC_TIME`. You cannot "Change Default Date format using LC_TIME" – waltinator Aug 10 '18 at 13:32
  • @waltinator `date` is indeed influenced by `LC_TIME`. `date`'s texinfo also explicitly recommends to set `LC_TIME` to `C` in order to produce locale independent output. – danzel Aug 10 '18 at 14:35
  • @danzel do you have the solution?.. – Gowtham Aug 10 '18 at 14:37
  • @Goron I'm not sure what you want to achieve. Why don't you just use a custom format string like `date +"%a %b %d %T %Z %Y"`? – danzel Aug 10 '18 at 14:51
  • @danzel yes I can do it.. but the thing is.. I have few 3rd party libraries relied upon date output. Currently it's expecting format with %d.. – Gowtham Aug 10 '18 at 14:56
  • The `dt_t_fmt` looked like Unicoded ASCII, so I decoded it. It is `%A %d %B %Y %I:%M:%S %p %Z` `date "+%A %d %B %Y %I:%M:%S %p %Z" --date=yesterday` (today is the 10th) gives `Thursday 09 August 2018 02:12:57 PM EDT` – waltinator Aug 10 '18 at 18:14
  • But `date` gives `Thu Aug 9 18:26:11 IST 2018 `... Which is different from `LC_TIME d_t_fmt` format defined in `en_IN`.. .. – Gowtham Aug 11 '18 at 15:20

2 Answers2

2

In LC_TIME, there seems to be a separate way to specify the format for the date(1) command. This can be seen in 16.04's en_US locale definition:

% Appropriate date representation (date(1))   "%a %b %e %H:%M:%S %Z %Y"
date_fmt    "<U0025><U0061><U0020><U0025><U0062><U0020><U0025><U0065>/
<U0020><U0025><U0048><U003A><U0025><U004D><U003A><U0025><U0053><U0020>/
<U0025><U005A><U0020><U0025><U0059>"
END LC_TIME

These lines are missing from 17.04 and newer's en_US locale definition, but is still present in the C and POSIX locale files (so date maybe using these as a fallback).

If I do edit the en_US locale to add a date_fmt setting before END LC_TIME and update the locales, it works fine:

# grep date_fmt /usr/share/i18n/locales/en_US
date_fmt "%F %Z"
# env LC_TIME=en_US.UTF-8 date
2018-08-13 JST
# date
Mon Aug 13 15:25:14 JST 2018
# sed -i 's/date_fmt.*/date_fmt "%Y"/' /usr/share/i18n/locales/en_US
# locale-gen en_US.UTF-8
Generating locales (this might take a while)...
  en_US.UTF-8... done
Generation complete.
# env LC_TIME=en_US.UTF-8 date
2018
muru
  • 193,181
  • 53
  • 473
  • 722
  • Thanks for the answer @muru.. I have done the same way as you said. But still i get the same format as before.. `#date Mon Aug 13 06:40:12 UTC 2018 # cat en_US | grep date_fmt date_fmt "%F %Z" # locale-gen en_US.UTF-8 Generating locales (this might take a while)... en_US.UTF-8... done Generation complete. # env LC_TIME=en_US.UTF-8 date Mon Aug 13 06:48:44 UTC 2018` ... Am I missing anything?.. – Gowtham Aug 13 '18 at 07:06
  • @Goron where in `/usr/share/i18n/locales/en_US` did you add `date_fmt`? Please edit the question to add more details. – muru Aug 13 '18 at 07:08
  • I have edited the `/usr/share/i18n/locales/en_US` please check and confirm... or should I edit `d_t_fmt "%a %d %b %Y %r %Z"` line?... – Gowtham Aug 13 '18 at 07:15
  • Hmm, then I have no idea. I just tried out what you did in a fresh docker container and had no problems. :/ – muru Aug 13 '18 at 07:34
  • Ok @muru .. You tried in Ubuntu server 18? – Gowtham Aug 13 '18 at 07:38
  • @Goron on Docker images of 17.04, 17.10 and 18.04. – muru Aug 13 '18 at 07:41
  • then it should work ... I Will check what's wrong and get back.. – Gowtham Aug 13 '18 at 07:47
  • Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/81567/discussion-between-goron-and-muru). – Gowtham Aug 13 '18 at 08:05
  • `locate LC_TIME` gives `/usr/lib/locale/C.UTF-8/LC_TIME` does this mean I need to modify `C` in locales?? – Gowtham Aug 13 '18 at 13:41
  • @muru This change works for date command but not for Telegram desktop. But Telegram desktop shows 24 hour format with locale C. – Smart Manoj Dec 14 '22 at 10:25
  • @SmartManoj is it a snap? – muru Dec 14 '22 at 13:09
  • @muru Github version – Smart Manoj Dec 15 '22 at 14:36
  • @SmartManoj what does that mean? You compiled it from source? Or is it a prebuilt flatpak or appimage or something? – muru Dec 15 '22 at 15:21
  • @muru pre-compiled https://github.com/telegramdesktop/tdesktop/releases/tag/v4.4.1 – Smart Manoj Dec 16 '22 at 06:02
0

I think you'll find the answers you're looking for here:

https://unix.stackexchange.com/a/748687/279250

Short version, you can do it with command-line options, environment variables, or by adding a new locale. The post gives details on each of these. You may want slightly different format specifiers in yours, but you'll see where to put them and how to make them take effect.

Joe
  • 11
  • 1
  • 7
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/1302952) – Mordoc Jun 15 '23 at 22:08
  • This is a link to another StackExchange answer that I wrote, not to an outside site. I see many "duplicates an answer elsewhere on StackExchange, here's the link" answers on StackExchange fora, often without even the summary I gave. If that link were somehow to change, it would be because StackExchange itself changed its URL format. It would either have to go update all the internal links or StackExchange itself would break. – Joe Jun 16 '23 at 03:37