I want to change the output format of `date`.

All Gurus once were Newbies
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Please stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions prefer the other forums within the support section.
Before you post please read how to get help
Post Reply
boracay
Level 1
Level 1
Posts: 16
Joined: Fri Jul 13, 2012 7:44 am

I want to change the output format of `date`.

Post by boracay » Mon Aug 20, 2018 6:16 pm

Currently it says something like

Tue Aug 21 06:14:00 +08 2018

The time here is right now 6:14AM. The "+08" part may confuse me later on, years from now, and may make me think the time I wrote the entry was 14:14 (2:14PM), because 6 plus 8 equals 14. Can it write the time zone I'm in?
Last edited by boracay on Mon Aug 20, 2018 6:44 pm, edited 1 time in total.

gm10
Level 14
Level 14
Posts: 5196
Joined: Thu Jun 21, 2018 5:11 pm

Re: I want to change the output format of `date`.

Post by gm10 » Mon Aug 20, 2018 6:39 pm

Guess it doesn't recognize your time zone, for me it puts the time zone identifier there.

Anyway, you can format the output freely, see man date. For example date +\%Y-\%m-\%d_\%H:\%M:\%S.

User avatar
GS3
Level 4
Level 4
Posts: 477
Joined: Fri Jan 06, 2017 7:51 am

Re: I want to change the output format of `date`.

Post by GS3 » Tue Aug 21, 2018 5:24 am

Maybe I am missing something or not understanding the question. The "+8" is the "error" or time difference between the time quoted and Universal Time (UT). It is the time zone. Without that info you do not know to what time zone the timestamp refers to and it becomes meaningless.

Time zone "+8" means the time stamp is 8 hours ahead of UT (Philippines) and you need to subtract that amount of hours to get UT.

I see an email that says
Date: Thu, 16 Aug 2018 10:16:55 -0400
This means the date is the same as
Date: Thu, 16 Aug 2018 14:16:55 0000
which is UT.

As computers communicate over different time zones it is essential to note the time zone of the timestamp or else it becomes meaningless. Even the same computer can change time zone and most do twice a year.

The only "real" time used for time stamps is UT although it may be expressed as a different local time plus the time difference.
HP Compaq Elite 8300 CMT - Linux Mint 18.2 Sonya - Kernel 4.4.0-138-generic X64 - Cinnamon 3.4.4 - Nemo

Mute Ant
Level 14
Level 14
Posts: 5009
Joined: Tue Sep 03, 2013 7:45 pm
Location: Norfolk UK

Re: I want to change the output format of `date`.

Post by Mute Ant » Tue Aug 21, 2018 5:20 pm

You probably can't change the distribution version of the function. Everyone else expects it to work the same as last year... they <<rely>> on it working the same as last year. This is especially true for commands in /bin and /sbin which are used during boot.

You can parse the normal output into your own format, sometimes called screen-scraping. Your bash script 'date' calls the real binary 'date' like this...

Code: Select all

#!/bin/bash
#
### file:///usr/local/bin/date
#
### Wrapper script to modify the date() function output to show <<just>> the date ... 2018 Aug 18
#
raw="$(/bin/date)"
new="${raw: -4: 4} ${raw: 4: 6}"
echo $new
exit $?
###EOF###

rene
Level 9
Level 9
Posts: 2570
Joined: Sun Mar 27, 2016 6:58 pm

Re: I want to change the output format of `date`.

Post by rene » Tue Aug 21, 2018 6:40 pm

Note also that date's default display format is determined by your locale settings, specifically LC_TIME. If you paste back the output of locale we can try to reproduce getting your format: I've not been able to get it to display without a (textual) time zone identifier even when trying a few UTC+8 choices such as zh_CN.UTF-8 (China), id_ID.UTF-8 (Indonesia) or ph_PH.UTF-8 (Philipines).

boracay
Level 1
Level 1
Posts: 16
Joined: Fri Jul 13, 2012 7:44 am

Re: I want to change the output format of `date`.

Post by boracay » Thu Nov 29, 2018 10:47 am

Hi Rene,
Thanks for your reply. The output of locale is
`~ $ locale
LANG=en_CA.UTF-8
LANGUAGE=en_CA:en
LC_CTYPE="en_CA.UTF-8"
LC_NUMERIC="en_CA.UTF-8"
LC_TIME="en_CA.UTF-8"
LC_COLLATE="en_CA.UTF-8"
LC_MONETARY="en_CA.UTF-8"
LC_MESSAGES="en_CA.UTF-8"
LC_PAPER="en_CA.UTF-8"
LC_NAME="en_CA.UTF-8"
LC_ADDRESS="en_CA.UTF-8"
LC_TELEPHONE="en_CA.UTF-8"
LC_MEASUREMENT="en_CA.UTF-8"
LC_IDENTIFICATION="en_CA.UTF-8"
LC_ALL=

User avatar
GS3
Level 4
Level 4
Posts: 477
Joined: Fri Jan 06, 2017 7:51 am

Re: I want to change the output format of `date`.

Post by GS3 » Thu Nov 29, 2018 12:17 pm

I believe locale is just the time display format but does not show the time zone to which the time is set. For instance, you can have locale set to US but time zone set to Central European Time (CET). The time would be CET but displayed in US format.

Again, the time zone or time difference between computer time and UT is essential information in order to avoid ambiguity. If you want to not have to take it into account then just set your computer to UT time (or UK time) and the difference will be zero. There is no way around this that I can think of.

To change time zones I double click on the time and date on the corner of the screen and then on "date and time settings".
HP Compaq Elite 8300 CMT - Linux Mint 18.2 Sonya - Kernel 4.4.0-138-generic X64 - Cinnamon 3.4.4 - Nemo

User avatar
JerryF
Level 11
Level 11
Posts: 3511
Joined: Mon Jun 08, 2015 1:23 pm
Location: Rhode Island, USA

Re: I want to change the output format of `date`.

Post by JerryF » Thu Nov 29, 2018 1:02 pm

boracay wrote:
Mon Aug 20, 2018 6:16 pm
Currently it says something like

Tue Aug 21 06:14:00 +08 2018

The time here is right now 6:14AM. The "+08" part may confuse me later on, years from now, and may make me think the time I wrote the entry was 14:14 (2:14PM), because 6 plus 8 equals 14. Can it write the time zone I'm in?
Is the result you're getting from the date command from Terminal and you want to change that?

Or somewhere else? I have my date and time show in the panel and it's customized:
Shutter_023.jpg
Shutter_023.jpg (1.55 KiB) Viewed 119 times
and the custom formatting is:
Shutter_024.jpg
Click to enlarge
IF your problem has been solved, please edit your original post and add [SOLVED] to the beginning of the Subject Line. It helps other members.

gm10
Level 14
Level 14
Posts: 5196
Joined: Thu Jun 21, 2018 5:11 pm

Re: I want to change the output format of `date`.

Post by gm10 » Thu Nov 29, 2018 1:11 pm

boracay wrote:
Thu Nov 29, 2018 10:47 am
Hi Rene,
Thanks for your reply.
How rude. A lot of people answered you yet you only acknowledge one? Also why is this thread still going on, anyway? The first response you received (mine) told you all you needed to know.

User avatar
GS3
Level 4
Level 4
Posts: 477
Joined: Fri Jan 06, 2017 7:51 am

Re: I want to change the output format of `date`.

Post by GS3 » Thu Nov 29, 2018 4:29 pm

boracay wrote:
Mon Aug 20, 2018 6:16 pm
Currently it says something like

Tue Aug 21 06:14:00 +08 2018

The time here is right now 6:14AM. The "+08" part may confuse me later on, years from now, and may make me think the time I wrote the entry was 14:14 (2:14PM), because 6 plus 8 equals 14. Can it write the time zone I'm in?
Yeah, the question is unclear and the OP is not putting any effort into helping us understand what he's trying to do. "Date" in terminal command outputs the current date and time. What's with the "years from now"? You're not going to leave the screen frozen in time for years. I guess maybe the OP has some script which records the date and time to some file?

The only problem I see is that the OP is confused by the information provided as output and the solution is to know how to interpret it. It seems to me the answer is not to change the format but to understand that the information is necessary and essential to avoid uncertainty and to interpret it correctly.
HP Compaq Elite 8300 CMT - Linux Mint 18.2 Sonya - Kernel 4.4.0-138-generic X64 - Cinnamon 3.4.4 - Nemo

rene
Level 9
Level 9
Posts: 2570
Joined: Sun Mar 27, 2016 6:58 pm

Re: I want to change the output format of `date`.

Post by rene » Thu Nov 29, 2018 4:41 pm

boracay wrote:
Thu Nov 29, 2018 10:47 am
The output of locale is
[ ... ]
LC_TIME="en_CA.UTF-8"
I'm still unable to find a time zone which in en_CA.UTF-8 displays by default numerically rather than mnemonically but there seems something wrong anyway. The +08 zone you were quoting would put you somewhere in Indonesia or the Philippines; if I can take en_CA.UTF-8 as indication that you are in fact in Canada you are on a negative offset. If Yukon, -08 when not in DST (but in fact -07 on your quoted "Aug 21 2018" when DST was seemingly still in effect for you so rather unsure where and when you are).

It in any case seems necessary to first all have you set or confirm your correct time zone. There's probably a GUI somewhere for it but so as to avoid edition differences, do

Code: Select all

ls -l /etc/localtime
If absent or incorrect, do e.g.

Code: Select all

ln -sf /usr/share/zoneinfo/Canada/Yukon /etc/localtime
Look in /usr/share/zoneinfo/Canada what's available. Then make sure you in fact generate your locales. Come to think of it, I suppose that may be the issue you have with the zone being displayed numerically:

Code: Select all

sudo locale-gen
At this point I see no reason why date would still display the zone numerically for you:

Code: Select all

rene@hp8k:~$ LC_TIME=en_CA.UTF-8
rene@hp8k:~$ sudo ln -sf /usr/share/zoneinfo/Canada/Yukon /etc/localtime
rene@hp8k:~$ date
Thu Nov 29 12:32:26 PST 2018
rene@hp8k:~$ date -d "2018-08-21 06:14:00"
Tue Aug 21 06:14:00 PDT 2018
If you have the clock synchronized with a time server you may also need/want to adjust zone by (Xfce edition) right clicking the clock applet on your panel and choosing Properties.

User avatar
GS3
Level 4
Level 4
Posts: 477
Joined: Fri Jan 06, 2017 7:51 am

Re: I want to change the output format of `date`.

Post by GS3 » Thu Nov 29, 2018 7:20 pm

I suspect the computer is configured as "en_CA" and the time zone is +8, probably Philippines.
HP Compaq Elite 8300 CMT - Linux Mint 18.2 Sonya - Kernel 4.4.0-138-generic X64 - Cinnamon 3.4.4 - Nemo

rene
Level 9
Level 9
Posts: 2570
Joined: Sun Mar 27, 2016 6:58 pm

Re: I want to change the output format of `date`.

Post by rene » Thu Nov 29, 2018 7:32 pm

Whichever (mis-)configurations, you try to come up with a way of reproducing date printing a numeric rather than mnemonic time zone by default (%:::z is the syntax specifier for something like "+08"; it is of course assumed in all of this that OP would know if he he was specifying a custom format).

That's what OP wanted, a mnemonic rather than numeric time zone identifier, and the point of this thread is (well, should be) us trying to figure out why he's not getting that by default. Yes, he probably has configuration issues and, yes, he can force things with explicit format specifiers for date but at the very least as far as I am myself concerned that's all rather besides the point, said point being that his requested format should be default.

rene
Level 9
Level 9
Posts: 2570
Joined: Sun Mar 27, 2016 6:58 pm

Re: I want to change the output format of `date`.

Post by rene » Thu Nov 29, 2018 8:12 pm

Upon noticing "boracay" to be a "small island in the central Philippines" it is now assumed that the timezone is in fact correctly set to the +08 zone. Furthermore, I finally managed to reproduce OP's format:

Code: Select all

rene@hp8k:~$ TZ="<+08>" LC_TIME=en_CA.UTF-8 date
Fri Nov 30 00:06:21 +08 2018
That is to say.... OP: is there any output when you in a terminal enter echo $TZ?

If yes, seems you will have defined TZ somewhere in the startup scripts or configuation files:

Code: Select all

sudo find /etc -type f -print0 | sudo xargs -0 grep " TZ"
Also check /etc/timezone. Undefining it should be all you need.

Post Reply

Return to “Newbie Questions”