How to do date arithmetic in Linux shell scripts using GNU date command?

-d

--date

date -d "last month" +%m

date -d "2 month ago" +%b

date -d "-2 month" +%B

date -d "-1 hours" +%H

date -d "tomorrow"

date -d "yesterday" +"%d %B %y"

date -d "last week" +%a

date -d "-1 month" +%B

date -d "$(date +%Y-%m-15) -1 month" +'Last month was %B!'

date -d "1970-01-01 00:00:01 UTC" +%s

date -d "1970-01-01 1234567890 sec" +"%F %T %z"

How to write and compile a simple date arithmetic C program?

gcc -o program_name program_source_code.c

#include <stdio.h> #include <time.h> #define c_DaySecs 86400 int main (int argc, char ** argv) { int Day; time_t TotSec; struct tm * TimeStruct; if ( argc != 2 ) { printf("Syntax

\t%s nth-Day

",argv[0]); return 1; } else { Day = atoi( argv[1] ); TotSec = time( NULL ); TotSec = TotSec + ( Day * c_DaySecs ); TimeStruct = localtime( &TotSec ); printf("%i days is %s",Day,ctime(&TotSec)); printf("%i days is %i-%i-%i

", \ Day, TimeStruct->tm_mday, \ TimeStruct->tm_mon + 1, \ TimeStruct->tm_year + 1900 ); TimeStruct = NULL; return 0; } }

#include <stdio.h> #include <time.h> int main (int argc, char ** argv) { time_t TimeNow; struct tm * TimeStruct; if ( argc < 2 || argc > 3) { printf("Syntax

\t%s SEC FLAG

",argv[0]); return 1; } else { TimeNow = atoi( argv[1] ); if (argv[2] == NULL || atoi( argv[2] ) == 0) { TimeStruct = localtime( &TimeNow ); printf( "%s in Local Timezone is %s", \ argv[1], asctime( TimeStruct ) ); } else { TimeStruct = gmtime( &TimeNow ); printf( "%s in GMT is %s", \ argv[1], asctime( TimeStruct ) ); } return 0; } }

#include <stdio.h> #include <time.h> int main (int argc, char ** argv) { int DD, MM, YYYY = 0; time_t TimeNow; struct tm * TimeStruct; if ( argc != 4 ) { printf("Syntax

\t%s DD MM YYYY

",argv[0]); return 1; } else { DD = atoi( argv[1] ); MM = atoi( argv[2] ); YYYY = atoi( argv[3] ); TimeNow = time( NULL ); TimeStruct = localtime( &TimeNow ); TimeStruct->tm_year = YYYY - 1900; TimeStruct->tm_mon = MM - 1; TimeStruct->tm_mday = DD; TimeNow = mktime( TimeStruct ); printf( "%ld

", TimeNow ); return 0; } }

Custom Search

How to write a Linux shell scripts that backup previous month data files on monthly basis? Of course, it involves date time calculation! Unless you’re using GNU Coreutils / GNU date, doing date arithmetic in shell scripts will not be that straight-forward as a novice think!I just can’t recall that SCO Unix and Concurrent Real Time Unix (RTU) have GNU date installed, when I was exposed to Unix for the first time in year 2000. But, I believe that GNU Coreutils is a standard package that available on most Linux distributions as of today.GNU date is one of the many common Linux programs in GNU Coreutils package. Here are some of the examples of performing date time arithmetic with GNU date command, usingoroption switch:Return as last month in numeric formatReturn as last month in abbreviated month nameReturn as last month in full month nameReturns previous hour in 24-hours format (00-23)Return the date and time of tomorrowReturn the date of yesterday in dd MMM yyReturn last week in abbreviated weekday nameTo avoid getting result of “July” when executingon 31 July 2006, because “2006-07-31 -1 month” might evaluate to 2006-07-01, because 2006-06-31 is an invalid date!Return the Unix Epoch time (the total seconds elapse since midnight of 1970-01-01), which is 1 secs in this case.Return the literal date format of the given Unix Epoch time.Isn’t it easier for date time calculation related shell scripts with GNU date installed? As said just now, GNU date is not a standard command in Unix variants.In case GNU date is not available, you might need to write a rather long shell scripts to accommodate some simple date time calculations. Potentially, it could end up as a messy or hard to maintain shell scripts!In case GNU Coreutils / GNU Date is not installed, you can write a simple C program in few lines of source code to satisfy the date arithmetic in shell scripts! The compiled C program is highly portable among Unix / Linux variants, as the date arithmetic library (time.h) is a standard C library!To compile the C program source code using GCC compiler, the general syntax for these simple source code isFor C/C++ tutorial or standard C/C++ library reference, I’m referring to cplusplus.com Calculate the nth-day from today:Return the literal date of a given Unix Epoch time:To convert a give literal date to Unix Epoch Time (total seconds elapsed since midnight of 1970-01-01):