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

  • Follow


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
0
Reply anoopkumarv (37) 6/19/2008 4:51:47 PM

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. 


0
Reply Doug 6/19/2008 5:14:27 PM


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]


0
Reply Michael 6/19/2008 6:34:45 PM

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.
0
Reply Anoop 6/19/2008 6:51:25 PM

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/
0
Reply Oscar 6/19/2008 7:24:05 PM

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

0
Reply Chris 6/19/2008 7:31:11 PM

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.unix.solaris/msg/50f0c8348ef3ef1d
]

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
0
Reply Anoop 6/19/2008 11:59:15 PM

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.unix.solaris/msg/50f0c8348ef3ef1d
> ]
> 
> 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
0
Reply Michael 6/20/2008 6:54:57 AM

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.unix.solaris/msg/50f0c8348ef3ef1d
> ]
> 
> 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

0
Reply Chris 6/20/2008 7:27:19 AM

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.unix.solaris/msg/50f0c8348ef3ef1d
>> ]
>>
>> 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-)
0
Reply Gary 6/20/2008 7:52:57 AM

On 2008-06-20 08:52:57 +0100, "Gary R. Schmidt" <grschmidt@acm.org> said:

> Don't cast the return values of malloc/calloc/realloc in C code.  Ever.

Not even to void *? ;-)

Good point though.

Cheers,

Chris

0
Reply Chris 6/20/2008 9:06:02 AM

Michael Laajanen wrote:
> Just wounder, why did you use c code compared to the nice perl script?

that code is indeed horrible, my eyes were bleeding after reading 
dateback.c...

so next time make sure you turn on compiler warnings, use lint, and if 
you must use malloc, test it with umem or watchmalloc (see 
http://blogs.sun.com/pnayak/entry/finding_memory_leaks_within_solaris 
for example)

I also strongly recommend to use a language like perl/python/ruby for 
such tasks, your code will be much slimmer and a lot less error-prone

just my 0.02 cents
0
Reply ISO 6/20/2008 9:35:55 AM

In article <6c1a7pF3ev8fnU1@mid.individual.net>,
	Chris Ridd <chrisridd@mac.com> writes:
> On 2008-06-20 08:52:57 +0100, "Gary R. Schmidt" <grschmidt@acm.org> said:
> 
>> Don't cast the return values of malloc/calloc/realloc in C code.  Ever.
> 
> Not even to void *? ;-)

No. If you find you get an error without, you are missing the header
file (prototype). Without the prototype, the return will be treated
as an int. If you then cast the int to void *, you won't get a compile
error, but in a 64 bit world, you lost the top 32 bits during the
transition to int and back, so you'll crash at runtime instead.

$ cat x.c
int main(int argc, char **argv)
{
        char *ptr;

        ptr = (void *)malloc(sizeof(char));
        *ptr = 1;

        return 0;
}

$ cc x.c
"x.c", line 5: warning: implicit function declaration: malloc
$ ./a.out
$
$ cc -m64 x.c
"x.c", line 5: warning: implicit function declaration: malloc
$ ./a.out
Segmentation Fault (core dumped)
$ 

Note this is on sparc.
On x86, you happen to get lucky in this case.

-- 
Andrew Gabriel
[email address is not usable -- followup in the newsgroup]
0
Reply andrew 6/20/2008 10:05:51 AM

On Jun 19, 11:54 pm, Michael Laajanen <michael_laaja...@yahoo.com>
wrote:
>
> 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"'
[ questionable code trimmed ]
> > 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.
> Just wounder, why did you use c code compared to the nice perl script?
> speed?

The specification called for "exactly 30 days back" so in C you could
calculate
program start,runtime,exit and factor that into the result and be
accurate to a few
more nano seconds than perl...... : >
0
Reply usenetpersongerryt 6/21/2008 3:09:51 AM

On 2008-06-21 04:09:51 +0100, usenetpersongerryt@gmail.com said:

> On Jun 19, 11:54 pm, Michael Laajanen <michael_laaja...@yahoo.com>
> wrote:
>> 
>> 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"'
> [ questionable code trimmed ]
>>> 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.
>> Just wounder, why did you use c code compared to the nice perl script?
>> speed?
> 
> The specification called for "exactly 30 days back" so in C you could
> calculate
> program start,runtime,exit and factor that into the result and be
> accurate to a few
> more nano seconds than perl...... : >

Heh. Well the other useful thing the C version did was to use strftime 
to get a locale-specific output format, so for completeness and our 
foreign friends here's a perl equivalent:

perl -MPOSIX -e 'print strftime("%c%n",localtime(time-30*24*60*60));'

Cheers,

Chris

0
Reply Chris 6/21/2008 4:49:40 AM

14 Replies
363 Views

(page loaded in 1.423 seconds)

Similiar Articles:


















7/17/2012 12:11:19 PM


Reply: