Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 scp files, is running on EDT and stores files that I need to copy in the format /path/to/filename/data20120913

I looked at trying to rsync files using something like find's -mtime -1 flag for files modified in the last day, but I didn't have any luck.

I don't mind just using scp to copy the current day's file, but as of right now there is a 4-hour window where running date +%Y%m%d will give a different day on each server and that bugs me a little.

Looking through man date I 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 EDT

I suppose I could also use something like the GNU date extension date -d 20100909 +%s to get the date in seconds from the epoch, apply a manual 4 * 60 * 60 second 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 YYYYMMDD format for EDT on a server that is set to UTC ?

share|improve this question

3 Answers 3

up vote 30 down vote accepted

You should be able to 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. This sets the TZ variable only for the command line.

share|improve this answer
1  
simple and beautiful. <3 linux –  cwd Sep 13 '12 at 21:45
    
+1 Beat me to it. –  James Sneeringer Sep 13 '12 at 21:59
1  
I was able to find all of the timezones by inspecting the following directory: /usr/share/zoneinfo –  neowulf33 Oct 9 at 21:41

A bit funny, but I thought the obvious answer was TZ=EDT, since the one asking the question already knows his or her time zone code.

Alternatively, for those using shells other than bash and its cousins simply invoking TZ=EDT. For those shells there's the env command. Thus:

env TZ=EDT date
share|improve this answer

You can do this by manipulating the TZ environment variable. The following will give you the local time for US/Eastern, which will also be smart enough to handle DST when that rolls around:

# all on one line
TZ=":US/Eastern" date +%Y%m%d

The zone name comes from the files and directories inside /usr/share/zoneinfo.

share|improve this answer
1  
+1 - good call on the DST and thanks for sharing /usr/share/zoneinfo –  cwd Sep 13 '12 at 22:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.