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.
|Designing Electronics with Linux||May 22, 2013|
|Dynamic DNS—an Object Lesson in Problem Solving||May 21, 2013|
|Using Salt Stack and Vagrant for Drupal Development||May 20, 2013|
|Making Linux and Android Get Along (It's Not as Hard as It Sounds)||May 16, 2013|
|Drupal Is a Framework: Why Everyone Needs to Understand This||May 15, 2013|
|Home, My Backup Data Center||May 13, 2013|
- Designing Electronics with Linux
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- Build a Skype Server for Your Home Phone System
- Validate an E-Mail Address with PHP, the Right Way
- A Topic for Discussion - Open Source Feature-Richness?
- Why Python?
- Tech Tip: Really Simple HTTP Server with Python
1 hour 25 min ago
- Reply to comment | Linux Journal
1 hour 33 min ago
- Understanding the Linux Kernel
3 hours 48 min ago
6 hours 18 min ago
- Kernel Problem
16 hours 21 min ago
- BASH script to log IPs on public web server
20 hours 48 min ago
1 day 23 min ago
- Reply to comment | Linux Journal
1 day 56 min ago
- All the articles you talked
1 day 3 hours ago
- All the articles you talked
1 day 3 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi
It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?