![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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. |
|
||||
|
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;
}
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|