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.
Practical Task Scheduling Deployment
One of the best things about the UNIX environment (aside from being stable and efficient) is the vast array of software tools available to help you do your job. Traditionally, a UNIX tool does only one thing, but does that one thing very well. For example, grep is very easy to use and can search vast amounts of data quickly. The find tool can find a particular file or files based on all kinds of criteria. It's pretty easy to string these tools together to build even more powerful tools, such as a tool that finds all of the .log files in the /home directory and searches each one for a particular entry. This erector-set mentality allows UNIX system administrators to seem to always have the right tool for the job.
Cron traditionally has been considered another such a tool for job scheduling, but is it enough? This webinar considers that very question. The first part builds on a previous Geek Guide, Beyond Cron, and briefly describes how to know when it might be time to consider upgrading your job scheduling infrastructure. The second part presents an actual planning and implementation framework.
Join Linux Journal's Mike Diehl and Pat Cameron of Help Systems.
Free to Linux Journal readers.View Now!
|The Firebird Project's Firebird Relational Database||Jul 29, 2016|
|Stunnel Security for Oracle||Jul 28, 2016|
|SUSE LLC's SUSE Manager||Jul 21, 2016|
|My +1 Sword of Productivity||Jul 20, 2016|
|Non-Linux FOSS: Caffeine!||Jul 19, 2016|
|Murat Yener and Onur Dundar's Expert Android Studio (Wrox)||Jul 18, 2016|
- The Firebird Project's Firebird Relational Database
- Stunnel Security for Oracle
- My +1 Sword of Productivity
- SUSE LLC's SUSE Manager
- Non-Linux FOSS: Caffeine!
- Managing Linux Using Puppet
- Murat Yener and Onur Dundar's Expert Android Studio (Wrox)
- Parsing an RSS News Feed with a Bash Script
- Google's SwiftShader Released
- Doing for User Space What We Did for Kernel Space
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