mktime [-f] [-F format] [-H hours] [-M minutes] [-S seconds] [-T hh:mm[:ss]] [-z] [-y year] [-m month] [-d month_day] [-D [yyyy-]mm-dd] [-t time_t]
The mktime command is a command-line interface to the mktime(3) function from Standard C, and can be used to manipulate times and dates in shell scripts. It has a few enhancements over mktime(3), and a few minor differences which are detailed below.
The mktime command takes a base date and time (by default the current date and time), modifies it according to its options, and displays the result. Options for specifying the date and time can either specify an adjustment to the base value or an absolute number. To specify an adjust ment, put an explicit sign on the value (eg. +1 or -5); to specify an absolute number, leave the value unsigned (eg. 7). See the EXAMPLES section below for details.
Normally, if the resulting time can not be represented (for example a time ``skipped'' during a change to day light savings time), mktime tries to adjust the result to a valid time, first by assuming that the time is not dur ing Daylight Savings Time (that is, it sets tm_isdst in the struct tm to 0), and if that fails, by assuming that it is (by setting tm_isdst to 1). The -f option alters this behaviour; see the EXAMPLES section. Since this behaviour is not clearly defined by the C Standard, it is possible that some implementations may fail despite these attempts, or that different implementations will give dif ferent results, so if you are concerned, you should set the time to one that is not likely to be affected by a time change, such as midnight or noo.
Most options to mktime are processed in order, and so adjustments are cumulative. The exception is -t which, since it is setting the base time, happens before other options.
If mktime succeeds, it prints the resulting time according to the given format (or the default format if none was supplied), and exits with status 0.
If mktime cannot represent the resulting time, it exits with status 1.
If mktime was invoked incorrectly, it exits with status 2.
If mktime encounters a runtime error, such as being unable to get the system time, or being unable to allocate memory for the output string, it exits with status 3. Note that this may indicate an invalid format, since strftime(3) does not distinguish between an invalid format and having insufficient space to store its result.
mktime's notion of adjusting versus setting values does not exist with mktime(3), where you always fill in the absolute values.
mktime(3) uses a zero-based month (ie. January is 0); mktime uses a one-based month because it is more ``natu ral'' and it matches the output with the %m format.
mktime(3) always uses years since 1900; mktime does not (see -y).
These examples assume the use of the Bourne shell, and the
results assume the commands were run Wednesday May 14,
1997 at 22:30:00 PDT, using the American (and Canadian)
rules for Daylight Savings Time changes. Shell prompts
are shown as "$", and user input is shown
# Display the current time in the default format
# Display the date and time 4 hours from now
mktime -H +4
# Display the date for the first of this month
mktime -F '%A %B %d, %Y' -d 1
Thursday May 01, 1997
# Display the day of the week and month two weeks ago
mktime -F '%A %d' -d -14
# Display the last day this month
mktime -m +1 -d 0
# Display the 256th day of this year
mktime -m 1 -d 256 -F '%a %b %d'
Sat Sep 13
# Display the day 2 days before March 1, 1996 $ mktime -D
1996-03-01 -d -2 -F '%a %b %d'
Wed Feb 28
# Display the time 2:30AM on April 6, 1997 PDT
# (North American Pacific timezone).
# NOTE: this is an invalid time; use -f to catch the error
mktime -f -D 1997-04-06 -T 2:30
mktime: invalid time
# Now let mktime adjust to a valid value:
mktime -D 1997-04-06 -T 2:30
# Display midnight January 1, 2000, as a time_t
mktime -F '%t' -z -D 2000-01-01
# Display one day before the previouse time_t result
mktime -d -1 -t 946713600
Sometimes it may be necessary to use multiple mktime com mands to get the desired date, or other shell commands may be required:
# The third last day of last month
mktime -F '%a %b %d' -d -3 -t `mktime -F '%t' -d 1`
Mon Apr 28
# The date of next Friday (Friday is day 5)
wday=`mktime -F '%w'`
mktime -F '%a %b %d' -d +`expr \( 7 + 5 - $wday \) % 7`
Fri May 16