Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi All, Anyone know how to call the UNIX command "date" from C?

Status
Not open for further replies.

christheprogrammer

Programmer
Jul 10, 2000
258
CA
Hi All,
Anyone know how to call the UNIX command "date" from C? I just want to print out the date using C. Thanks
Chris [sig][/sig]
 
This will produce output similar to Unix date with no arguments. You can, of course, print it out however you like, look into the standard time libraries.

#include <stdio.h>
#include <time.h>
int main(void)
{
struct tm *p;
long tmm;
char *wk_days[] = { &quot;Sun&quot;,&quot;Mon&quot;,&quot;Tue&quot;,&quot;Wed&quot;,&quot;Thu&quot;,&quot;Fri&quot;,&quot;Sat&quot; };
char *mo_days[] = { &quot;Jan&quot;,&quot;Feb&quot;,&quot;Mar&quot;,&quot;Apr&quot;,&quot;May&quot;,&quot;Jun&quot;,&quot;Jul&quot;,
&quot;Aug&quot;,&quot;Sep&quot;,&quot;Oct&quot;,&quot;Nov&quot;,&quot;Dec&quot; };
time(&tmm);
if ((p=localtime(&tmm))!=NULL) {
printf(&quot;%s %s %2d %2.2d:%2.2d:%2.2d %s %d\n&quot;,wk_days[p->tm_wday],
mo_days[p->tm_mon],
p->tm_mday,
p->tm_hour,
p->tm_min,
p->tm_sec,
tzname[1],
p->tm_year+1900);
}
return 0;

}

Regards [sig]<p>Russ<br><a href=mailto:bobbitts@hotmail.com>bobbitts@hotmail.com</a><br><a href= is in</a><br>[/sig]
 
hi chris,

rbobbitt 's method is the right way to do it. one should avoid using system (&quot;date&quot;); in code. but that is the way to call command line prog in a C program.

int main { exit(system(&quot;date&quot;));}

will do the trick...

-afaik,
shail

[sig][/sig]
 
Right, if you _really_ want to call the 'date' program, you can use system(). The reason you want to avoid this generally is that your code is less portable as you're making assumptions about the implementation and, for that matter, the existence of the program you're calling.

For instance, if I run shail's example on my win98 system, I'm also prompted to change the date.

Whoops, forgot the ()'s in main :)

int main() { exit(system(&quot;date&quot;));}

[sig]<p>Russ<br><a href=mailto:bobbitts@hotmail.com>bobbitts@hotmail.com</a><br><a href= is in</a><br>[/sig]
 
Thanks Guys,
I actually solved the problem this way:

#include <time.h>

void current_time()
{
time_t time_struct;
char *time_now;

time(&time_struct);
time_now = ctime(&time_struct);
while (*time_now != '\0')
{
if (*time_now != '\n')
printf(&quot;%c&quot;,*time_now);
time_now++;
}

printf(&quot;\n&quot;);
} [sig][/sig]
 
hi chris,

if u have the while loop only to print up to the end of string and if block to not print the new line character... then simply change time_now[24] = '0'; or whatever u want.
and print (&quot;%s&quot;, time_now);

ctime returns a 26 byte string ending with \n\0 no problems with using %s directly on it. ofcourse don't forget to free(time_now) memory... u will need another var. to store the base pointer.

-shail [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top