datecalc


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting datecalc
# 1  
Old 08-21-2007
datecalc

Is there no simple way using 'date' command just to get number of days from given date.

ie. if I pass 2007-01-15 as an argument. I want to obtain result as 015

Simarly for 2007-02-10. I want to obtain result as 41.

Can't this be achieved by simple 1 or 2 lines of command (date). As this is very small part in my script.


date -d option is not working on unix but it is in linux where I am able to achieve proper results using it.
# 2  
Old 08-21-2007
gnu date is available as a binary or as source for almost every platform - the date -d capability is in that code.

Other than that you have to resort to some language and write your own "date -d".
This is the way it is in UNIX. There are lots of ways to get what you want, but just because it involves doing something you don't like does not mean it is necessarily bad.

this is a simple C utility that does exactly what you specified:
Code:
/* date_d.c  YYYY-MM-DD format */
/* compile: cc date_d.c -o date_d
*  usage:   date_d 2007-08-17
*           echo "2007-08-17" | date_d
*  ouput from example: 228
*/

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

int process(const char *src)
{
    struct tm tmptr;
    int retval=0;
    
    if(strptime(src, "%Y-%m-%d", &tmptr)!=NULL)    
		retval=tmptr.tm_yday;
	return retval;
}

int main(int argc, char **argv)
{
    char tmp[128]={0x0};
	
	if(argc>1)
	{
	   	int i=0;
		for(i=1; i<argc; i++) 
	   		fprintf(stdout,"%d\n", process(argv[i]));
	}
	else
	{
		while(fgets(tmp, sizeof(tmp), stdin) !=NULL) 		
			fprintf(stdout,"%d\n", process(tmp));
	}   		
    return 0;
}

You now have a one-line command, just be sure date_d is in the PATH.
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to subtract time by 10 minutes in datecalc tool

Hi guys. I am trying to subtract 10 minutes from the current Unix system date and time. I have the datecalc provided here but it is mainly the date and not the time. Please check on how can i subtract 10 minutes from the current time using datecalc or any other shell scripting that will... (2 Replies)
Discussion started by: bantiloe
2 Replies

2. UNIX for Dummies Questions & Answers

Doubt in datecalc.....

I have a doubt in date conversion. Can anyone explain the logic behind the date2jd and jd2date in datecalc function specified in this forum. I could not understand what the number are actually representing. Please explain...bcoz i am doing a program with this one.. Note: No need to... (1 Reply)
Discussion started by: sivakumar.rj
1 Replies

3. UNIX for Dummies Questions & Answers

How to use datecalc for adding hours

Hello: I need to generate two datetimestamps in one hour apart. the start date is 05262008000000 then generate the following dates 05262008010000 05262008020000 05262008030000 ... ... ... ... ... ... 05262008230000 05272008000000 05272008010000 05272008020000 05272008030000 ...... (5 Replies)
Discussion started by: ucbus
5 Replies

4. Shell Programming and Scripting

datecalc question.

I have a script that works perfectly on a linux box, but I am going to have to move it over to solaris which I have never really worked with. I was a bit miffed that date -d was not available on solaris so I started looking for alternative soltuoins and found this forum and datecalc. But I... (4 Replies)
Discussion started by: trey85stang
4 Replies

5. Shell Programming and Scripting

Need a help Please- runing the perderabos script datecalc

i have a script in bourne called cdrsnokia.sh and inside of it calls a script called resta_dias where it calls the datecalc script (perderabos date calculator). The purpose is to rest (-) one day arithmetical operation the content of each line and the result is passed to another file lista2;after... (1 Reply)
Discussion started by: alexcol
1 Replies
Login or Register to Ask a Question