This is a discussion on Solaris 10: How can I get a date that is exactly 30 days back? within the comp.unix.solaris forums, part of the Solaris Operating System category; --> I am unable to do this - I used to use a variation of the following command to get ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| I am unable to do this - I used to use a variation of the following command to get a date that is a few hours back: TZ=GMT+6 date '+%y%m%d%H%M' But I think this has its limits - what is the best way I can get a date that is x number of days back? Thanks, Anoop |
| |||
| Anoop <anoopkumarv@gmail.com> writes: >I am unable to do this - I used to use a variation of the following >command to get a date that is a few hours back: >TZ=GMT+6 date '+%y%m%d%H%M' >But I think this has its limits - what is the best way I can get a >date that is x number of days back? Use GNU date.. If you absolutely must use something only pre-installed, then you'll have to write a perl/C/C++/whatever program to do the same thing GNU date already has written. |
| |||
| In article <485a93f3$0$52555$8046368a@newsreader.iphouse.net> , Doug McIntyre <merlyn@geeks.org> wrote: > Anoop <anoopkumarv@gmail.com> writes: > >I am unable to do this - I used to use a variation of the following > >command to get a date that is a few hours back: > > >TZ=GMT+6 date '+%y%m%d%H%M' > > >But I think this has its limits - what is the best way I can get a > >date that is x number of days back? > > Use GNU date.. > > If you absolutely must use something only pre-installed, then you'll > have to write a perl/C/C++/whatever program to do the same thing GNU > date already has written. And if you aren't allowed to write code or scripts on the machine, there's always that wonderful thing called a calendar. Banks used to give them away. -- DeeDee, don't press that button! DeeDee! NO! Dee... [I filter all Goggle Groups posts, so any reply may be automatically by ignored] |
| |||
| On Jun 19, 2:34 pm, Michael Vilain <vil...@NOspamcop.net> wrote: > In article <485a93f3$0$52555$80463...@newsreader.iphouse.net> , > Doug McIntyre <mer...@geeks.org> wrote: > > > Anoop <anoopkum...@gmail.com> writes: > > >I am unable to do this - I used to use a variation of the following > > >command to get a date that is a few hours back: > > > >TZ=GMT+6 date '+%y%m%d%H%M' > > > >But I think this has its limits - what is the best way I can get a > > >date that is x number of days back? > > > Use GNU date.. > > > If you absolutely must use something only pre-installed, then you'll > > have to write a perl/C/C++/whatever program to do the same thing GNU > > date already has written. > > And if you aren't allowed to write code or scripts on the machine, > there's always that wonderful thing called a calendar. Banks used to > give them away. > > -- > DeeDee, don't press that button! DeeDee! NO! Dee... > [I filter all Goggle Groups posts, so any reply may be automatically by ignored] OK - at the risk of being flamed as off-topic, can someone please help me with a perl command that gives me the date that is 30 days back? Thanks. |
| |||
| Anoop wrote: > OK - at the risk of being flamed as off-topic, can someone please help > me with a perl command that gives me the date that is 30 days back? heard of google? http://www.itworld.com/nl/unix_insider/05062004/ |
| |||
| On 2008-06-19 19:51:25 +0100, Anoop <anoopkumarv@gmail.com> said: > OK - at the risk of being flamed as off-topic, can someone please help > me with a perl command that gives me the date that is 30 days back? perl -e 'print scalar(localtime(time-30*24*60*60)),"\n"' Cheers, Chris |
| |||
| On Jun 19, 3:31 pm, Chris Ridd <chrisr...@mac.com> wrote: > On 2008-06-19 19:51:25 +0100, Anoop <anoopkum...@gmail.com> said: > > > OK - at the risk of being flamed as off-topic, can someone please help > > me with a perl command that gives me the date that is 30 days back? > > perl -e 'print scalar(localtime(time-30*24*60*60)),"\n"' > > Cheers, > > Chris Thanks a lot for the help. I finally ended up using a c program that someone had written [Link: http://groups.google.com/group/comp....f0c8348ef3ef1d ] I created the file dateback.c with the content: #include <time.h> #include <stdio.h> #define ONEDAY (60*60*24) #define DEFAULT_FORMAT "%d-%b-%Y" int main(int argc,char *argv[]) { time_t now; char *date_format=NULL; struct tm *now_s; int daysback; char stamp[80]; if (argc < 2 || argc > 3) { fprintf(stderr,"usage: daysback <number of days> [\"<date format>\"]\n"); exit(2); } daysback=atoi(argv[1]); if (argc==3) { date_format=(char *)malloc(strlen(argv[2])); strcpy(date_format,argv[2]); } else if (getenv("DAYSBACK")) date_format=(char *)getenv("DAYSBACK"); if ((!date_format) || (*date_format=='\0')) { date_format=(char *)malloc(strlen(DEFAULT_FORMAT)); strcpy(date_format,DEFAULT_FORMAT); } now=time(0)-daysback*ONEDAY; now_s=localtime(&now); strftime(stamp,80,date_format,now_s); puts(stamp); return(0); } Then I could compile this using: cc backdate.c This created the output: a.out. Renamed the a.out file to backdate. Now I could include the backdate just as any command in my shell scripts and it worked perfectly. thanks again, Anoop |
| |||
| Hi, Anoop wrote: > On Jun 19, 3:31 pm, Chris Ridd <chrisr...@mac.com> wrote: >> On 2008-06-19 19:51:25 +0100, Anoop <anoopkum...@gmail.com> said: >> >>> OK - at the risk of being flamed as off-topic, can someone please help >>> me with a perl command that gives me the date that is 30 days back? >> perl -e 'print scalar(localtime(time-30*24*60*60)),"\n"' >> >> Cheers, >> >> Chris > > Thanks a lot for the help. > > I finally ended up using a c program that someone had written [Link: > http://groups.google.com/group/comp....f0c8348ef3ef1d > ] > > I created the file dateback.c with the content: > > #include <time.h> > #include <stdio.h> > > #define ONEDAY (60*60*24) > #define DEFAULT_FORMAT "%d-%b-%Y" > > int main(int argc,char *argv[]) > { > time_t now; > char *date_format=NULL; > struct tm *now_s; > int daysback; > char stamp[80]; > > if (argc < 2 || argc > 3) > { > fprintf(stderr,"usage: daysback <number of days> > [\"<date format>\"]\n"); > exit(2); > } > > daysback=atoi(argv[1]); > > if (argc==3) > { > date_format=(char *)malloc(strlen(argv[2])); > strcpy(date_format,argv[2]); > } > else if (getenv("DAYSBACK")) > date_format=(char *)getenv("DAYSBACK"); > > if ((!date_format) || (*date_format=='\0')) > { > date_format=(char *)malloc(strlen(DEFAULT_FORMAT)); > strcpy(date_format,DEFAULT_FORMAT); > } > > now=time(0)-daysback*ONEDAY; > now_s=localtime(&now); > > strftime(stamp,80,date_format,now_s); > puts(stamp); > > return(0); > > } > > Then I could compile this using: cc backdate.c > This created the output: a.out. > Renamed the a.out file to backdate. > Now I could include the backdate just as any command in my shell > scripts and it worked perfectly. > > thanks again, > Anoop Just wounder, why did you use c code compared to the nice perl script? speed? /michael |
| |||
| On 2008-06-20 00:59:15 +0100, Anoop <anoopkumarv@gmail.com> said: > On Jun 19, 3:31 pm, Chris Ridd <chrisr...@mac.com> wrote: >> On 2008-06-19 19:51:25 +0100, Anoop <anoopkum...@gmail.com> said: >> >>> OK - at the risk of being flamed as off-topic, can someone please help >>> me with a perl command that gives me the date that is 30 days back? >> >> perl -e 'print scalar(localtime(time-30*24*60*60)),"\n"' >> >> Cheers, >> >> Chris > > Thanks a lot for the help. > > I finally ended up using a c program that someone had written [Link: > http://groups.google.com/group/comp....f0c8348ef3ef1d > ] > > I created the file dateback.c with the content: You do have a couple of buffer overflows in this code. Fixes are inline below: > > #include <time.h> > #include <stdio.h> > > #define ONEDAY (60*60*24) > #define DEFAULT_FORMAT "%d-%b-%Y" > > int main(int argc,char *argv[]) > { > time_t now; > char *date_format=NULL; > struct tm *now_s; > int daysback; > char stamp[80]; > > if (argc < 2 || argc > 3) > { > fprintf(stderr,"usage: daysback <number of days> > [\"<date format>\"]\n"); > exit(2); > } > > daysback=atoi(argv[1]); > > if (argc==3) > { > date_format=(char *)malloc(strlen(argv[2])); date_format=(char *)malloc(strlen(argv[2])+1); > strcpy(date_format,argv[2]); > } > else if (getenv("DAYSBACK")) > date_format=(char *)getenv("DAYSBACK"); > > if ((!date_format) || (*date_format=='\0')) > { > date_format=(char *)malloc(strlen(DEFAULT_FORMAT)); date_format=(char *)malloc(strlen(DEFAULT_FORMAT)+1); > strcpy(date_format,DEFAULT_FORMAT); > } > > now=time(0)-daysback*ONEDAY; > now_s=localtime(&now); > > strftime(stamp,80,date_format,now_s); > puts(stamp); > > return(0); > > } You're leaking the things you're mallocing too, but that doesn't matter in something this small. Cheers, Chris |
| ||||
| More fixes in-line. Chris Ridd wrote: > On 2008-06-20 00:59:15 +0100, Anoop <anoopkumarv@gmail.com> said: > >> On Jun 19, 3:31 pm, Chris Ridd <chrisr...@mac.com> wrote: >>> On 2008-06-19 19:51:25 +0100, Anoop <anoopkum...@gmail.com> said: >>> >>>> OK - at the risk of being flamed as off-topic, can someone please help >>>> me with a perl command that gives me the date that is 30 days back? >>> >>> perl -e 'print scalar(localtime(time-30*24*60*60)),"\n"' >>> >>> Cheers, >>> >>> Chris >> >> Thanks a lot for the help. >> >> I finally ended up using a c program that someone had written [Link: >> http://groups.google.com/group/comp....f0c8348ef3ef1d >> ] >> >> I created the file dateback.c with the content: > > You do have a couple of buffer overflows in this code. Fixes are inline > below: > >> >> #include <time.h> >> #include <stdio.h> #include <stdlib.h> >> >> #define ONEDAY (60*60*24) >> #define DEFAULT_FORMAT "%d-%b-%Y" >> >> int main(int argc,char *argv[]) >> { >> time_t now; >> char *date_format=NULL; >> struct tm *now_s; >> int daysback; >> char stamp[80]; >> >> if (argc < 2 || argc > 3) >> { >> fprintf(stderr,"usage: daysback <number of days> >> [\"<date format>\"]\n"); >> exit(2); >> } >> >> daysback=atoi(argv[1]); >> >> if (argc==3) >> { >> date_format=(char *)malloc(strlen(argv[2])); > > date_format=(char *)malloc(strlen(argv[2])+1); date_format = malloc(strlen(argv[2])+1); > >> strcpy(date_format,argv[2]); >> } >> else if (getenv("DAYSBACK")) >> date_format=(char *)getenv("DAYSBACK"); >> >> if ((!date_format) || (*date_format=='\0')) >> { >> date_format=(char *)malloc(strlen(DEFAULT_FORMAT)); > > date_format=(char *)malloc(strlen(DEFAULT_FORMAT)+1); date_format = malloc(strlen(DEFAULT_FORMAT)+1); > >> strcpy(date_format,DEFAULT_FORMAT); >> } >> >> now=time(0)-daysback*ONEDAY; >> now_s=localtime(&now); >> >> strftime(stamp,80,date_format,now_s); >> puts(stamp); >> >> return(0); >> >> } > > You're leaking the things you're mallocing too, but that doesn't matter > in something this small. > Don't cast the return values of malloc/calloc/realloc in C code. Ever. Cheers, Gary B-) |
| Thread Tools | |
| Display Modes | |
|
|