The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to use datecalc for adding hours ucbus UNIX for Dummies Questions & Answers 5 06-03-2008 04:48 PM
datecalc question. trey85stang Shell Programming and Scripting 4 05-08-2008 09:20 PM
Need a help Please- runing the perderabos script datecalc alexcol Shell Programming and Scripting 1 02-22-2007 02:10 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-21-2007
tostay2003 tostay2003 is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 23
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 (permalink)  
Old 08-21-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,720
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:48 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0