You can set a timezone for the duration of the query, thusly:
TZ=America/New_York date
Note the whitespace between the TZ
setting and the date
command. In Bourne-like and rc
-like shell, that sets the TZ
variable only for the command line. In other shells (csh
, tcsh
, fish
), you can always use the env
command instead:
env TZ=America/New_York date
tl;dr
On Linux systems. timezones are defined in files in the /usr/share/zoneinfo
directory. This structure is often referred to as the "Olson database" to honor its founding contributor.
The rules for each timezone are defined as text file lines which are then compiled into a binary file. The lines so compiled, define the zone name; a range of data and time during which the zone applies; an offset from UTC for the standard time; and the notation for defining how transition to-and-from daylight saving time occurs, if applicable.
For example, the directory "America" contains the requisite information for New York in the file America/New_York
as used, above.
Beware that the specification of a non-existent zone (file name) is silently ignored and UTC times are reported. For example, this reports an incorrect time:
TZ="America/New York" date ### WRONG ###
The Single UNIX Specification, version-3, known as SUSv3 or POSIX-2001, notes that for portability, the character string that identifies the timezone description should begin with a colon character. Thus, we can also write:
TZ=":America/New_York" date
TZ=":America/Los_Angeles" date
As an alternative method to the specification of timezones using a pathname to a description file, SUSv3 describes the POSIX model. In this format, a string is defined as:
std offset [dst[offset][,start-date[/time],end-date[/time]]]
where std
is the standard component name and dst
is the daylight saving one. Each name consists of three or more characters. The offset
is positive for timezones west of the prime meridian and negative for those east of the meridian. The offset is added to the local time to obtain UTC (formerly known as GMT). The start
and end
time fields indicate when the standard/daylight transitions occur.
For example, in the Eastern United States, standard time is 5-hours earlier than UTC, and we can specify EST5EDT
in lieu of America/New_York
. These alternatives are not always recognized, however, especially for zones outside of the United States and are best avoided.
HP-UX (an SUSv3 compliant UNIX) uses textual rules in /usr/lib/tztab
and the POSIX names like EST5EDT, CST6CDT, MST7MDT, PST8PDT. The file includes all of the historical rules for each time zone, akin to the Olson database.
NOTE: You should be able to find all of the timezones by inspecting the following directory: /usr/share/zoneinfo
.
The date
command, like almost all programs, relies on the standard library to access timezone data. On Linux (except for some embedded systems) and *BSD, the standard library determines the current timezone from the content of /etc/localtime
.
It appears that on your system, /etc/localtime
does not contain what it seems to contain. Like Stéphane Chazelas and derobert, I strongly suspect that the file /usr/share/zoneinfo/Etc/UTC
, which /etc/localtime
is a symbolic link to, contains incorrect information, probably because someone who didn't know what they were doing attempted to change the system timezone and ended up overwriting a system file.
I recommend reinstalling the time zone information to make sure that your system isn't corrupted. Run rpm -qf /usr/share/zoneinfo/Etc/UTC
to see which package contains that file and reinstall it with yum reinstall
. Then set the timezone properly, either with the timedatectl
command or by changing the /etc/localtime
symbolic link and the text file /etc/zoneinfo
or /etc/sysconfig/clock
(I think Fedora uses the latter):
ln -snf /usr/share/zoneinfo/Europe/Madrid /etc/localtime
echo Europe/Madrid >/etc/zoneinfo
sed -i -e '/^ *ZONE=/d' /etc/sysconfig/clock
echo 'ZONE="Europe/Madrid"' >>/etc/sysconfig/clock
Java does things differently — it bypasses the standard library and reads /etc/timezone
or /etc/sysconfig/clock
instead. That's why you're seeing different timezone information from Java programs.
Best Answer
You have have the timezone set incorrect. To see if it works at all use some others (the extremes):
and if you double check your entry against the list you can see you're missing an underscore in the value of
$TZ
.So try
and see if your problem persists.
I haven't found any policy for the naming of the timezones, but spaces are never in them and replaced by underscore, but sometimes with a dash. It is probably best to look it up and copy/paste the value, something you, or your source didn't.