Walker News

GNU Coreutils: GNU Date Is Easier For Date Time Calculation In Linux Shell Script

I mentioned about date time calculation shell script in an earlier post. Though, the GNU date examples related to Unix Epoch time (a.k.a. Unix time, POSIX time, Julian time) in that post aren’t really successful (so as the Julian time calculation in C program code) :-(

(Time is not an easy-to-understand topic, especially people who keen on “A Brief History Of Time” that written by Stephen Hawking).

OK, put aside that Physics topic about time. Back to the question: How easy is to do date time calculation in Linux shell script (and not about Windows Vista Date Time Matter)?

First of all, GNU date is one of the many Linux programs that grouped in a great package, with a given name GNU Coreutils.

GNU Coreutils is bundled with Red Hat Linux distribution, and is believed being a ubiquitous GNU package among Linux distributions.

For example, in my RHEL4 machine, ls -laSh /bin/date shows it’s only 47KB in size and the rpm -qf /bin/date shows it’s part of the coreutils-5.2.1-31 package.

This GNU date can either set or change the current system date/time or to display date/time format according to user queries. For those serious programmers who use Julian time, you should happy to know that GNU date understands UNIX Epoch time too!
What is that Epoch Time exactly?

The computer times are represented as a count of seconds since an epoch, i.e. a well-defined point of time (hence, it’s called Epoch time).

On GNU and POSIX systems, the epoch time is defined as 1970-01-01 00:00:00 UTC. Thus, it’s also known as UNIX time, POSIX time, or some simply calls it Julian time.

Here are some GNU date arithmetic and time calculation examples, which I’ve referred to the official GNU date documentation (also appears in info date):
walker@walkernews.net # date
Thu Aug 28 17:10:38 BST 2008
walker@walkernews.net # date -d "3 hours ago"
Thu Aug 28 14:10:41 BST 2008
walker@walkernews.net # date -d "+3 hours"
Thu Aug 28 20:12:01 BST 2008
walker@walkernews.net # date -d "last month"
Mon Jul 28 17:13:11 BST 2008
walker@walkernews.net # date -d "next month"
Sun Sep 28 17:14:15 BST 2008
walker@walkernews.net # date -d "+2 months"
Tue Oct 28 17:14:56 GMT 2008
walker@walkernews.net # date -d "2 months ago"
Sat Jun 28 17:16:44 BST 2008
walker@walkernews.net # date -d "yesterday"
Wed Aug 27 17:13:35 BST 2008
walker@walkernews.net # date -d "tomorrow"
Fri Aug 29 17:13:55 BST 2008
walker@walkernews.net # date -d "3 days ago"
Mon Aug 25 17:22:42 BST 2008
walker@walkernews.net # date -d "+3 days"
Sun Aug 31 17:23:08 BST 2008
walker@walkernews.net # date -d "last friday"
Fri Aug 22 00:00:00 BST 2008
walker@walkernews.net # date -d "2008-08-22 +3 days"
Mon Aug 25 00:00:00 BST 2008
walker@walkernews.net # date -d "2008-08-22 -5 days"
Sun Aug 17 00:00:00 BST 2008

How to format the GNU date output format? For example, to get the day, weekday, month, year of 23 days ago can be coded as such:
walker@walkernews.net # date -d "23 days ago"
Tue Aug  5 18:23:24 BST 2008
walker@walkernews.net # date -d "23 days ago" +%e
walker@walkernews.net # date -d "23 days ago" +%d
walker@walkernews.net # date -d "23 days ago" +%m
walker@walkernews.net # date -d "23 days ago" +%-m
walker@walkernews.net # date -d "23 days ago" +%b
walker@walkernews.net # date -d "23 days ago" +%B
walker@walkernews.net # date -d "23 days ago" +"%e-%-m-%Y, %B"
 5-8-2008, August
walker@walkernews.net # date -d "23 days ago" +"%e-%m-%Y, %B"
 5-08-2008, August

You may refer to man date for detail list of GNU date output format modifiers. Also, take note the difference of +%m and +%-m is that the later remove the zero padding.
The zero-padding looks neatly when display in string format. However, zero-padded numeric is not valid in Bash shell script arithmetic interpretation, i.e. 08 – 05 will fail as the valid syntax is 8 – 5.

Unix Epoch time calculation examples with GNU date

The +%s is another GNU date output modifier, i.e. to output number of seconds since Unix Epoch. And, the date/time format for -d option switch is close to ISO 8601 date/time format, i.e. YYYY-MM-DD HH:MM:SS (it’s not a strict ISO 8601 Date/Time format, but close to it).

For example, the UNIX time of 25th of December 2008 at 12:25PM 08 seconds is given as this:
walker@walkernews.net # date -u -d "2008-12-25 12:25:08" +%s

So, the Julian time of 2008-08-08 20:08:08 (the date and time of Beijing Olympic Opening Ceremony) is as such:
walker@walkernews.net # date -u -d "2008-08-08 20:08:08" +%s

That’s 1218226088 seconds elapsed since Unix Epoch time at 2008-08-08 20:08:08.

To convert UNIX Epoch time or Julian time to human readable date/time format:
walker@walkernews.net # date -u -d "1970-01-01 1218226088 secs"
Fri Aug  8 20:08:08 UTC 2008

Custom Search

  1. Using TRK To Enable Vista SP1 Administrator Account And Reset Windows Password – Walker News 07-12-08@17:19

    […] TRK owner, Tom Kerremans (a.k.a. Harakiri), creates a shell script calls winpass to automatically search and mount NTFS file system followed by calling chntpw to edit […]

2018  •  Privacy Policy