2

I'm curious about how to change the abbreviation of time zone shown by date command. Let the time zone unchanged, but how to change CST below to UTC+8?

clay@LAPTOP-3PP2093I:~$ date
Thu Dec  1 23:05:28 CST 2022

Thank you!

PS 1. OS is ubuntu 22.04

PP 2. My time zone is Asia/Taipei, therefore the result of date +%Z is CST. However I think this abbr is confusing, therefore hope it is shown as UTC+8.

PS 3. I tried a workaround, change time to Asia/Singapore. Yes, the result of date +%Z is +08. But I really don't want to change time zone.

3
  • Have you read man date?
    – Panki
    Commented Dec 1, 2022 at 14:50
  • 2
    Please don't post images of text. It makes searches impossible and reproducing the error difficult.
    – doneal24
    Commented Dec 1, 2022 at 14:59
  • @doneal24 thank you! modified it.
    – Clay Chen
    Commented Dec 1, 2022 at 15:06

1 Answer 1

3

With export TZ='<UTC+8>-8' you'd be defining the current timezone as being named UTC+8 and being 8 hours ahead of UTC all year round for past, current and future dates.

Then:

$ date +'%::z %Z'
+08:00:00 UTC+8
$ date
Thu  1 Dec 23:04:48 UTC+8 2022

To keep Asia/Taipei (with its historical changes in timezone), but change what %Z returns, you'd need to modify and recompile the tzdata from which it is derived.

apt source tzdata
sudo apt build-dep tzdata

Change the Taiwan section to:

# Rule  NAME    FROM    TO  -   IN  ON  AT  SAVE    LETTER/S
Rule    Taiwan  1946    only    -   May 15  0:00    1:00    9
Rule    Taiwan  1946    only    -   Oct 1   0:00    0   8
Rule    Taiwan  1947    only    -   Apr 15  0:00    1:00    9
Rule    Taiwan  1947    only    -   Nov 1   0:00    0   8
Rule    Taiwan  1948    1951    -   May 1   0:00    1:00    9
Rule    Taiwan  1948    1951    -   Oct 1   0:00    0   8
Rule    Taiwan  1952    only    -   Mar 1   0:00    1:00    9
Rule    Taiwan  1952    1954    -   Nov 1   0:00    0   8
Rule    Taiwan  1953    1959    -   Apr 1   0:00    1:00    9
Rule    Taiwan  1955    1961    -   Oct 1   0:00    0   8
Rule    Taiwan  1960    1961    -   Jun 1   0:00    1:00    9
Rule    Taiwan  1974    1975    -   Apr 1   0:00    1:00    9
Rule    Taiwan  1974    1975    -   Oct 1   0:00    0   8
Rule    Taiwan  1979    only    -   Jul 1   0:00    1:00    9
Rule    Taiwan  1979    only    -   Oct 1   0:00    0   8

# Zone  NAME        STDOFF  RULES   FORMAT  [UNTIL]
# Taipei or Taibei or T'ai-pei
Zone    Asia/Taipei 8:06:00 -   UTC+8:06    1896 Jan  1
            8:00    -   UTC+8   1937 Oct  1
            9:00    -   UTC+9   1945 Sep 21  1:00
            8:00    Taiwan  UTC+%s

Then build the new tzdata package with:

dpkg-buildpackage -b

Install the generated package.

Then:

$ export TZ=Asia/Taipei
$ date
Thu  1 Dec 23:17:41 UTC+8 2022
$ date -d 1979-08-01
Wed  1 Aug 00:00:00 UTC+9 1979
$ date -d 1892-06-01
Wed  1 Jun 00:00:00 UTC+8:6 1892

You'd need to redo that every time the Ubuntu source package is updated (which happens quite a few times a year, any time any government in any country decides of a new DST change date or rule for instance), so probably not practical.

If it's only date's output you have issues with, you can also do:

$ date +"$(locale date_fmt | sed 's/%Z/UTC%:z/')"
Fri  2 Dec 00:31:05 UTC+08:00 2022

in place of

$ date
Fri  2 Dec 00:31:05 CST 2022

Or:

$ date +"$(locale d_t_fmt | sed 's/%Z/UTC%:z/')"
Fri 02 Dec 2022 00:33:02 UTC+08:00

in place of:

$ date +%c
Fri 02 Dec 2022 00:33:02 CST

Those:

$ locale -k LC_TIME | grep fmt
d_t_fmt="%a %d %b %Y %T %Z"
d_fmt="%d/%m/%y"
t_fmt="%T"
t_fmt_ampm="%l:%M:%S %P %Z"
era_d_fmt=""
era_d_t_fmt=""
era_t_fmt=""
date_fmt="%a %e %b %H:%M:%S %Z %Y"

Can be changed by compiling a new locale with your preference:

localedef -i <(sed 's/%Z/UTC%z/g' /usr/share/i18n/locales/en_GB) \
          -f UTF-8 ~/.locales/en_GB.UTF-8@mytimezoneformat
export LOCPATH=~/.locales LC_TIME=en_GB.UTF-8@mytimezoneformat

(above installed in my home directory, but you could also do it system-wide and not to have to set $LOCPATH).

Then:

$ date
Fri  2 Dec 01:11:16 UTC+0800 2022
$ date +%c
Fri 02 Dec 2022 01:11:20 UTC+0800
2
  • Thank for your excellent response! However, there is a mystery bothered me. How do you know which package to modify? It seems there might be some kind of magic~
    – Clay Chen
    Commented Dec 4, 2022 at 13:50
  • 1
    @ClayChen assuming you're talking of the Asia/Taipei timezone file, it's the /usr/share/zoneinfo/Asia/Taipei file you need to modify. It may or may not be supplied as is as part of a package. On Debian or derivative, it is. dpkg -S /usr/share/zoneinfo/Asia/Taipei will tell you which package. On other systems it could be compiled from the tz data upon installation. Commented Dec 4, 2022 at 21:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .