Well, you can do something sneaky like:
$ echo "`date +%s` - (1125 * 24 * 60 *60)" |bc
1194478815
$ date -r 1194478689
Wed, 07 Nov 2007 18:38:09 -0500
Tested on OpenBSD (definitely non gnu based date), and seems to work.
Breaking it down in steps:
- get the current unixtime (seconds since beginning of unix epoch):
$ date +%s
1291679934
- get the number of seconds in 1125 days
$ echo "1125 * 24 * 60 *60" | bc
97200000
subtract one from the other (1291679934 - 97200000) = 1194478815
use the new unixtime (1194478815) to print a pretty date
$ date -r 1194478689
Wed, 07 Nov 2007 18:38:09 -0500
As an alternative, on solaris you can do this to print the date*:
/bin/echo "0t1194478815>Y\n<Y=Y" |adb
* referenced from http://www.sun.com/bigadmin/shellme/
Also, an alternative on Solaris for getting the current timestamp from the date command** is:
/usr/bin/truss /usr/bin/date 2>&1 | nawk -F= '/^time()/ {gsub(/ /,"",$2);print $2}'
** referenced from http://www.commandlinefu.com/commands/view/7647/unix-timestamp-solaris
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
.
Best Answer
Here's an extension of the_velour_fog's answer, adapted to use second/minute/day/month/year.
Here's how it decides which unit to use: it uses the largest unit that gives a number of at least two.
Example: If something happened 72 hours ago, it will output in days. If something happened exactly an hour ago, it will use minutes.