===================================================================== LinuxDeveloperNews- Date Math In Linux Shell Script June 26, 2007 http://www.linuxdevelopernews.com ********************************************************************* ********************************************************************* Increase Your Sales With Speaking Animated Characters For Your Site SitePal's animated speaking characters enable you to deliver your messaging using both site and sound, which reinforces your messaging and establishes trust. Try the Free Demo here: http://aj.600z.com/aj/30859/0/cc?z=1&b=30440&c=30853 ********************************************************************* ********************************************************************* Date Math In Linux Shell Script Dave Taylor | Expert Author 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. ********************************************************************* ********************************************************************* You need to make a conference call, and you need it now. Whether you are a Fortune 500 company or a small business just getting on your feet, you need to meet when and where it’s convenient for you. Set Up Your Account For Free And Start Conferencing Today! Try ConferenceCall.com Now! http://aj.600z.com/aj/30698/0/cc?z=1&b=30632&c=30697 ********************************************************************* ********************************************************************* 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 #include #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! ********************************************************************* ********************************************************************* 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/ ********************************************************************* ********************************************************************* For ad details and prices... mailto:susan@ientry.com ********************************************************************* Signup for free newsletters: http://www.ientry.com/page/newsletters ********************************************************************* --- LinuxDeveloperNews is an iEntry.com publication --- http://www.iEntry.com iEntry, Inc. 2549 Richmond Rd Second Floor Lexington, KY 40509 *********************************************************************