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 ssahore@yahoo.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.
The dirs command, combined with pushd and popd, is very effective for tracking users' directory changes. Suppose you have to make some changes to the files present in the following directories:
/home/sangeeth/soft/release2/src/
/home/sangeeth/soft/release2/src/show/
/home/sangeeth/soft/release2/doc/
Instead of noting down the directories on paper, do the following:
$ pushd /home/sangeeth/soft/release2/src/ $ pushd /home/sangeeth/soft/release2/src/show/ $ pushd /home/sangeeth/soft/release2/doc/
To list all the directories, do the following:
$ dirs -l -p -d
Suppose you make all the required changes in the first directory (/home/sangeeth/soft/release2/src/). To remove that directory entry from the list of directories that you created earlier, do the following:
$ popd
The above command removes the topmost directory entry (/home/sangeeth/soft/release2/src/) and performs a cd to the new top directory, which in my case, will be the second directory (/home/sangeeth/soft/release2/src/show/).
Alternatively, one can pop a particular directory from the list of directories by giving the directory ID (the ID is displayed beside a directory when using dirs -l -p -d) to the popd command:
$ popd +1
More options available for using the above commands can be found by viewing the man pages.
Special Magazine Offer -- Free Gift with Subscription
Receive a free digital copy of Linux Journal's System Administration Special Edition as well as instant online access to current and past issues. CLICK HERE for offer
Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.
Subscribe now!
The Latest
Newsletter
Featured Videos
Set up a secure virtual host in Apache
December 22nd, 2008 by Elliot Isaacson in
Setting up an https server in Apache is easy. This tutorial covers how to create and sign your ssl certificate as well as how to configure the web server.
Recently Popular
From the Magazine
January 2009, #177
It's a battle as old as time: good vs. evil. Fortunately, Linux and FOSS are on our side as we wage the battle against those who try to steal our secrets and invade our systems.
Checking your system's security is best done sooner rather than later. Test the locks with our article on security verification; find out how to use PAM to help secure your systems; use MinorFS and AppArmor to implement discretionary access control; learn more about Samba security in part III of our series; use Darknet to help detect bots and secure your systems; use the Yubikey to increase your site's security; and don't forget to lock the doors, because a cold boot attack could render your security useless if somebody has physical access to your computer.
But, we're not just about sowing the seeds of fear. We also show you how to use memcached in Rails, how to manage multiple servers efficiently, how to deploy applications easily with Capistrano, how to manage your videos with MythVideo, how to mix it up a bit (your audio that is), and even play a few games.
Delicious
Digg
Reddit
Newsvine
Technorati





Post new comment