9

how to get local date & time in linux terminal while server configured in UTC or different timezone?

here is what I get now but I'd like to see in local timezone. For eg: PST/PDT.

[jenkins@myServer ~]$ date
Thu Jul 28 18:16:48 UTC 2016

I'm not looking to change system time using hwclock or updating /etc/localtime. Just want to change it for a user.

Also, please let me know - how to persist it for future logins.

Robert Ranjan
  • 1,538
  • 3
  • 19
  • 17

3 Answers3

11

Use the TZ environment variable to pass the desired timezone to date:

TZ=<timezone> date

You can find the available timezones in the /usr/share/zoneinfo/ directory and subdirectories. For example, /usr/share/zoneinfo/America/New_York defines TZ=America/New_York.

Example:

$ date                         
Fri Jul 29 06:31:53 BDT 2016

$ TZ='America/New_York' date   
Thu Jul 28 20:31:58 EDT 2016

$ TZ='America/Los_Angeles' date
Thu Jul 28 17:31:54 PDT 2016
heemayl
  • 39,294
  • 7
  • 70
  • 76
4

For a local time, use "date". For UTC time, use "date -u". Note that, if you use "date" in the server's terminal it returns the server local time.

Ebram
  • 161
  • 1
  • 6
  • 2
    How did this attract the most upvotes? Not only does it not answer the actual question at hand, it suggests to use local time on the server instead. The latter being pretty silly - the convenience of **not** having to set the TZ in one's local shell is fat outweighed by the fact that UTC doesn't have the stupid issue of daylight savings times... – tink Feb 10 '21 at 16:41
  • I agree with @tink, My host/dev box is always set at UTC so all my logs default to UTC so I can compare times across boxes in different timezones. I'm looking for a way to say based on my current internet provider what time zone am I likely in. If that doesn't work then I'll just have to hard-code GMT+4 and deal with daylight savings times on a manual basis. – PatS Jul 21 '21 at 15:32
1

You can show local time by overriding the TZ environment variable for the process which prints the date. POSIX says a lot about the topic, beginning with

This variable shall represent timezone information. The contents of the environment variable named TZ shall be used by the ctime(), ctime_r(), localtime(), localtime_r() strftime(), mktime(), functions, and by various utilities, to override the default timezone.

Conventional 3-character timezone values were some time ago (more or less) standardized to deprecate the 3-character forms, using the combined standard and daylight savings time form. The preferred form used for PDT is PST8PDT.

There's a page on VMware showing the names and mentioning that they are used on Linux; you may notice that very few of those are 3-character form (aside from the generic UTC+offset).

Further reading:

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105