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
.
find
supports a lot of date input formats. The simplest format to obtain is YYYYMMDD HH:MM:SS. You already have the digits in the right order, all you have to do is extract the first group (${timestamp%??????}
: take all but the last 6 characters; ${timestamp#????????}
: take all but the first 8 characters), and keep going, appending punctuation then the next group as you go along.
timestamp=20130207003851
timestring=${timestamp%??????}; timestamp=${timestamp#????????}
timestring="$timestring ${timestamp%????}"; timestamp=${timestamp#??}
timestring="$timestring:${timestamp%??}:${timestamp#??}"
In bash (and ksh and zsh), but not in ash, you can use the more readable ${STRING_VARIABLE:OFFSET:LENGTH}
construct.
timestring="${timestamp:0:8} ${timestamp:8:2}:${timestamp:10:2}:${timestamp:12:2}"
To sort files by date, print out the file names preceded by the dates and sort that, then strip the date prefix. Use -printf
to control the output format. %TX
prints a part of the modification time determined by X
; if X
is @
, you get the number of seconds since the Unix epoch. Below I print three tab-separated columns: the time in sortable format, the file name, and the time in human-readable format; cut -f 2-
removes the first column and the call to expand
replaces the tab by enough spaces to accommodate all expected file names (adjust 40 as desired). This code assumes you have no newlines or tabs in file names.
find -maxdepth 1 -type f \
-newermt "$timestring" -printf '%T@\t%f\t%Tb %Td %TH:%TM\n' |
sort -k1n |
cut -f 2- |
expand -t 40
Best Answer
You may convert the time returned by
stat
orperl
to the format you want with the commanddate
(assuming you have GNU coreutils installed):date
itself can give you the modification time for files directly too:For details, see the man page for GNU date (
man date
).