Unix Technical Forum

Solaris 10: How can I get a date that is exactly 30 days back?

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 ...


Go Back   Unix Technical Forum > Unix Operating Systems > Solaris Operating System > comp.unix.solaris

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-20-2008, 03:40 AM
Anoop
 
Posts: n/a
Default Solaris 10: How can I get a date that is exactly 30 days back?

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-20-2008, 03:40 AM
Doug McIntyre
 
Posts: n/a
Default Re: Solaris 10: How can I get a date that is exactly 30 days back?

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.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-20-2008, 03:40 AM
Michael Vilain
 
Posts: n/a
Default Re: Solaris 10: How can I get a date that is exactly 30 days back?

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]


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 06-20-2008, 03:40 AM
Anoop
 
Posts: n/a
Default Re: Solaris 10: How can I get a date that is exactly 30 days back?

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 06-20-2008, 03:40 AM
Oscar del Rio
 
Posts: n/a
Default Re: Solaris 10: How can I get a date that is exactly 30 days back?

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/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 06-20-2008, 03:40 AM
Chris Ridd
 
Posts: n/a
Default Re: Solaris 10: How can I get a date that is exactly 30 days back?

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 06-20-2008, 03:40 AM
Anoop
 
Posts: n/a
Default Re: Solaris 10: How can I get a date that is exactly 30 days back?

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 06-23-2008, 10:46 AM
Michael Laajanen
 
Posts: n/a
Default Re: Solaris 10: How can I get a date that is exactly 30 days back?

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 06-23-2008, 10:46 AM
Chris Ridd
 
Posts: n/a
Default Re: Solaris 10: How can I get a date that is exactly 30 days back?

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 06-23-2008, 10:46 AM
Gary R. Schmidt
 
Posts: n/a
Default Re: Solaris 10: How can I get a date that is exactly 30 days back?

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-)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:55 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com