How can I have `date` output the time from a different timezone?
I have a server running with the timezone set to
UTC. It seemed like that was generally a good practice (please correct me if I'm wrong).
Anyhow, one of the servers I connect to, in order to
scpfiles, is running on
EDTand stores files that I need to copy in the format
I looked at trying to
rsyncfiles using something like find's
-mtime -1flag for files modified in the last day, but I didn't have any luck.
I don't mind just using
scpto copy the current day's file, but as of right now there is a 4-hour window where running
date +%Y%m%dwill give a different day on each server and that bugs me a little.
man dateI see that I can have the time output as
UTC, but I don't see a way to have it output as another timezone like
I suppose I could also use something like the
date -d 20100909 +%sto get the date in seconds from the epoch, apply a manual
4 * 60 * 60second calculation, and see about rendering that as a date - but then when daylight time kicks in it will still be an hour off.
Is there a simpler way to output the date in a
EDTon a server that is set to
EDT is not a recognized timezone- in Linux, at least. See my "Be careful!" answer below for the whole ugly story. If EDT is recognized on your brand of *NIX, you should still exercise caution and double-check your timezone string.
You can set a timezone for the duration of the query, thusly:
Note the whitespace between the
TZsetting and the
datecommand. In Bourne-like and
rc-like shell, that sets the
TZvariable only for the command line. In other shells (
fish), you can always use the
env TZ=America/New_York date
On Linux systems. timezones are defined in files in the
/usr/share/zoneinfodirectory. 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_Yorkas 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]]]
stdis the standard component name and
dstis the daylight saving one. Each name consists of three or more characters. The
offsetis 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
endtime 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
EST5EDTin 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/tztaband 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:
@JRFerguson - If a timezone is not included in the list e.g. Las Vegas or Nevada, what are the alternatives in validating the time?
@Motivated If your city or state isn't included, choose one that uses the same zone info as yours. For instance, I'm in Florida, which uses the same timezone as New York, so my machines synchronize to New York time.
@JRFerguson - That assumes that the person is aware of the various timezones and is able to use an alternative e.g. New York in your case. Would that be the only option? For example, i don't live in Europe and i won't know the overlapping timezones.
@Motivated You can use something like this site to assist you in finding other points in your zone.
@JRFerguson Maybe you can. But then that shouldn't be necessary. What if there is no Internet connectivity? What if the person is unaware of such a website? What if ... It shouldn't be necessary and at best what you propose is a workaround. That's not the right way to go about it. I'm not saying that you're behind this I'm just saying that it's not the right way (I hope that makes sense - I'm dead tired).