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 firstname.lastname@example.org.
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.
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Profiles and RC Files
- Understanding Ceph and Its Place in the Market
- Astronomy for KDE
- The Giant Zero, Part 0.x
- Maru OS Brings Debian to Your Phone
- Git 2.9 Released
- OpenSwitch Finds a New Home
- What's Our Next Fight?
- Snappy Moves to New Platforms
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide