function to get current date

  • Follow


Hello, All!

I've got two functions doing same thing in a little different way - obtain 
date. Which one is more portable and memory-leak free?
/* first */
char * cdate(char *now_date)
{
static struct timeval tv;
struct tm tm0;
struct timezone tz;

gettimeofday(&tv, &tz);
localtime_r(&tv.tv_sec, &tm0);
sprintf(now_date,"%04d/%02d/%02d", tm0.tm_year+1900, tm0.tm_mon+1, 
tm0.tm_mday);
return now_date;
}

/* second */
#define TIME_FORMAT "%02i:%02i:%02i %02i/%02i/%04i"
char *gettimestr (void)
{
static char timestr[80];
static time_t rawtime;
static struct tm *Now;

time (&rawtime);
Now = localtime (&rawtime);
snprintf (timestr, 72, TIME_FORMAT,
Now->tm_hour, Now->tm_min, Now->tm_sec, Now->tm_mday, Now->tm_mon + 1,
Now->tm_year + 1900);
return timestr;
}


With best regards, Roman Mashak.  E-mail: mrv@tusur.ru 


0
Reply Roman 12/16/2005 11:18:42 AM

"Roman Mashak" <mrv@tusur.ru> writes:

>I've got two functions doing same thing in a little different way - obtain 
>date. Which one is more portable and memory-leak free?
>...........


Portable?    ctime(3c) ?

-- 
Chris.
0
Reply Chris 12/16/2005 9:49:58 AM


"Chris McDonald" schrieb 
> "Roman Mashak" <mrv@tusur.ru> writes:
> 
> >I've got two functions doing same thing in a little
> > different way - obtain 
> >date. Which one is more portable and memory-leak free?
> >...........
> 
> 
> Portable?    ctime(3c) ?
> 
strftime(3) ?

HTH.
Martin


0
Reply Martin 12/16/2005 11:52:10 AM

2 Replies
189 Views

(page loaded in 0.766 seconds)

Similiar Articles:













7/30/2012 3:23:23 AM


Reply: