NetworkingFiles
SecurityProNews
ITmanagement




Date Math In Linux Shell Script

By: Dave Taylor
Expert Author
2007-06-25

Within a Linux shell script to be used as a cron job, how do I calculate the current date, the current date - n days, and the current date + n days ?

This script is to be used to partition an oracle database, and automatically drop old partitions (n+1) and create a new partition (n+1).

While I'm sure this will amaze some people, I am going to actually take the daring step of suggesting that date math is rather difficult to accomplish in many Unix and Linux shell script environments, and it's much easier to actually write a brief C program to accomplish your task. Yes, I've written one of the most popular shell script programming books ever written (Wicked Cool Shell Scripts), but even I don't think that you can solve every problem with a script! :-)

There's a version of the date command (commonly known as GNU date) that allows some rudimentary mathematical calculations, but that's only useful if you have the more sophisticated version of this particular utility.

Instead, the key to working with date math on a Unix system is to remember that Unix (and Linux) work on what's called epoch time, the number of seconds since a specific date back in 1970. As I write this entry, for example, the "epoch time" is 1131948327.

Once you have that, and a utility that turns epoch time into a nice, readable date, it's easy enough to add or delete days: just add or subtract 60*60*24 seconds for each day in question. Here it is as a C utility:

/** DATEMATH - Demonstration program that shows how to
    do date mathematics by utilizing Unix procedures.
    This is missing some error checking, etc., but will
    show how to do "+n" and "-n" date math well enough.
    (C) Copyright 2005 by Dave Taylor. Free to redistribute
    if this copyright is left intact. Thanks.
***/

#include <stdio.h>
#include <time.h>

#define ONEDAY       60*60*24

time_t time(time_t *tloc);
char *ctime(const time_t *clock);

main(int argc, char **argv)
{
      time_t theTime;
      int offset = 1;

      if (argc > 1) offset = atoi(argv[1]);

      theTime = time((time_t *) NULL);

      theTime += (long) (offset * ONEDAY);

      printf("Offset by %d, the date is: %s\n",
          offset, ctime(&theTime));
}


As you can see, the utility grabs the current epoch time by using the time() function, adds any days necessary by multiplying the requested offset by the seconds in a day ONEDAY, then uses the ctime() function to output a standard date string.

Here's the utility at work:

$ ./a.out +41
Offset by 41, the date is: Sun Dec 25 06:16:44 2005

Yes, that means there are only 41 shopping days until Christmas!

$ ./a.out -41
Offset by -41, the date is: Tue Oct 4 06:16:50 2005

$ ./a.out -5000
Offset by -5000, the date is: Sat Mar 7 06:16:55 1992

$ ./a.out -999
Offset by -999, the date is: Wed Feb 19 06:17:13 2003

$ ./a.out 365
Offset by 365, the date is: Tue Nov 14 06:17:28 2006


I hope that helps you out!

Comments


About the Author:
Dave Taylor has been involved with the Internet since 1980 and is internationally known as an expert on both business and technology issues. Holder of an MSEd and MBA, author of twenty books and founder of four startups, he also runs a strategic marketing company and consults with firms seeking the best approach to working with weblogs and social networks. Dave is an award-winning speaker and frequent guest on radio and podcast programs.

AskDaveTaylor.com
http://www.intuitive.com/blog/
Newsletter Archive | Submit Article | Advertising Information | About Us| Contact

LinuxDeveloperNews is an iEntry, Inc.® publication © 1998-2008 All Rights Reserved Privacy Policy and Legal
iEntry Contact Advertise iEntry Jayde WebProWorld Forums Downloads News Article Archive About iEntry LinuxDeveloperNews Home Page LinuxDeveloperNews News