When working on Linux/UNIX platforms, I frequently find it useful to obtain the date and time in the past or future. Whether scheduling jobs, searching for files from a certain date or determining the day on which a certain date falls, countless scenarios need a routine to compute and display the date and time in the past or future. I searched for a suitable program, but ultimately had to write one myself. This program is called showdate. It is written in the C language originally on UNIX and has been ported over to Linux as well. You can download the code from the LJ FTP site: ftp.linuxjournal.com/pub/lj/listings/issue163/9877.tgz.
After obtaining the source code, assemble the showdate executable using an ANSI C compiler (either cc or gcc) as shown:
# cc showdate.c -o showdate
Store the executable in /usr/local/bin or a directory of your choice, and invoke it without any arguments to obtain usage:
# showdate usage: showdate [-y [+|-]years] [-m [+|-]months] [-d [+|-]days] [-h [+|-]hours] [-M [+|-]minutes] [-s [+|-]seconds] [-e | -f format]
showdate recognizes the following command-line options and arguments:
-y [+|-]years: Number of years in the past (-) or future (+) offset from the current year.
-m [+|-]months: Number of months in the past (-) or future (+) offset from the current month.
-d [+|-]days: Number of days in the past (-) or future (+) offset from the current day.
-h [+|-]hours: Number of hours in the past (-) or future (+) offset from the current hour.
-M [+|-]minutes: Number of minutes in the past (-) or future (+) offset from the current minute.
-s [+|-]seconds: Number of seconds in the past (-) or future (+) offset from the current second.
-e: Display the time elapsed in seconds since the UNIX epoch (January 1, 1970 UTC).
-f format: Display the date and time according to formatting directives specified in format.
Options e and f are incompatible. Specifying them together on the command line terminates the program abnormally. The default output of showdate can be tweaked with the formatting directives and argument to -f, which are identical to the ones used by the standard date command. The important thing is that all computations are performed by taking either a positive (future) or negative (past) offset from the current date and time (now), which is its datum.
A good way to become familiar with any tool quickly is to understand how it is used. For example, the command to display the date and time ten years ago, relative to now, would be (output of showdate has been omitted as the results depend on the value of now):
# showdate -y -10
To find out the epoch seconds elapsed for the above scenario, use:
# showdate -y -10 -e
A futuristic date of five years, two months and 23 days from now in the YY-MM-DD format would be output as shown below. The plus sign is optional for future dates and the two forms of the command line below are equivalent (the minus sign is mandatory for past dates):
# showdate -y +5 -m +2 -d +23 -f %Y-%m-%d # showdate -y 5 -m 2 -d 23 -f %Y-%m-%d
The options can appear in any order, as long as their contextual usage is unambiguous; therefore, the command line above could be written as:
# showdate -m 2 -f %Y-%m-%d -d 23 -y 5
The +- offsets can be combined in a single command line; however, mixing them up can lead to unexpected and erroneous results. If now is January 1st 2003 12:00:00 AM UTC, showdate outputs:
# showdate -m -8 -M 32 Wed May 1 00:32:00 2002
The above command displays the date and time in the past—eight months ago but 32 minutes from now, while the one below displays the date and time in the future—8 months from now but 32 minutes ago:
# showdate -m 8 -m -32 Sun Aug 31 23:28:00 2003
The capabilities of showdate can be leveraged to specify subminute job scheduling times. A quick way to schedule a batch job 12 minutes and 35 seconds from now would be:
# showdate -M 12 -s 35 -f %Y%m%d%H%M.%S | xargs at -f job-file -t
The current date and time is tracked as the number of seconds that have elapsed since the epoch. This number is stored in a signed long, which means that on a 32-bit system, the timekeeping will break on Tuesday January 19, 2038 at 03:14:08 UTC, when the value overflows and becomes negative. An error is returned if the desired date and time exceeds this limit as shown here:
# showdate -y 1000 showdate: required time exceeds system limit
The presence of whitespace characters in the formatting directive needs to be escaped or enclosed in quotes (single/double). So, the command to display the date and time 18 hours, 30 minutes ago in Year-Month-Day Hour:Minute:Second format would be:
# showdate -h -18 -M -30 -f "%Y-%m-%d %H:%M:%S"
showdate cannot obtain the date and time by specifying a weekly offset and by taking a positive or negative offset from any datum, not just the current date and time. Even though showdate has been tried and tested rigorously, it is not perfect. And, if anyone encounters a bug or feels that redesigning the algorithm, implementing coding shortcuts or efficiently using system resources can improve the program, please contact me by e-mail at email@example.com.
showdate was designed for computing and displaying the date and time in the past or future depending on the command-line options, specified as an offset from the current date and time. The next step would be to augment showdate to specify weeks and the ability to change its datum.
|PostgreSQL, the NoSQL Database||Jan 29, 2015|
|HPC Cluster Grant Accepting Applications!||Jan 28, 2015|
|Sharing Admin Privileges for Many Hosts Securely||Jan 28, 2015|
|Red Hat Enterprise Linux 7.1 beta available on IBM Power Platform||Jan 23, 2015|
|Designing with Linux||Jan 22, 2015|
|Wondershaper—QOS in a Pinch||Jan 21, 2015|
- PostgreSQL, the NoSQL Database
- Sharing Admin Privileges for Many Hosts Securely
- HPC Cluster Grant Accepting Applications!
- Internet of Things Blows Away CES, and it May Be Hunting for YOU Next
- Ideal Backups with zbackup
- Wondershaper—QOS in a Pinch
- Designing with Linux
- Red Hat Enterprise Linux 7.1 beta available on IBM Power Platform
- Slow System? iotop Is Your Friend
- January 2015 Issue of Linux Journal: Security
Editorial Advisory Panel
Thank you to our 2014 Editorial Advisors!
- Jeff Parent
- Brad Baillio
- Nick Baronian
- Steve Case
- Chadalavada Kalyana
- Caleb Cullen
- Keir Davis
- Michael Eager
- Nick Faltys
- Dennis Frey
- Philip Jacob
- Jay Kruizenga
- Steve Marquez
- Dave McAllister
- Craig Oda
- Mike Roberts
- Chris Stark
- Patrick Swartz
- David Lynch
- Alicia Gibb
- Thomas Quinlan
- Carson McDonald
- Kristen Shoemaker
- Charnell Luchich
- James Walker
- Victor Gregorio
- Hari Boukis
- Brian Conner
- David Lane