Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Displaying Current Date in C Program

0 views
Skip to first unread message

Donald W. Ottens

unread,
Jul 6, 1992, 8:57:14 AM7/6/92
to
Hi! I am new at programming in C within the UNIX environment. Currently,
I am using ULTRIX 4.2.

My question is this:

I have a completed C program and now I want to print (display) the
current date for my intended users (i.e., the users have the option
for screen displays or a hard copy print out). Is there a way to
get the current date and time using the "date" command from the UNIX
shell? How do I execute this from within a C program?

Thanks a gazillion anyone!


The Don Mon
dw...@gte.com

Howie Michalski

unread,
Jul 8, 1992, 11:41:12 AM7/8/92
to


SURE!
system("date");

would do it but you couldn't do anything with the output.
It would be THROWN onto the screen/stdout wherever your cursor was...

Now if you were to open a pipe() to the date command you could load it
into a character array, and do with it what you please:

FILE *pptr;
char Date[40];

pptr = popen("date", "r");
fgets(Date , (int)sizeof(Date) , pptr);

fprintf(stdout , "%s\n" , Date);

This is pretty messy programming, however. It's not a good idea to rely on
OS commands to run your program. To get the date (in a useable form for display,
etc..) check out the time.h library of functions:

localtime()
ctime()

in particular...

#include <time.h>

char *ptr; /* To hold your string */
time_t *clock; /* Standard "time" pointer */

/* First get the current time */

localtime(clock);

/* Then you convert that number to a "pretty" string,
pointed to by "ptr" */

ptr = ctime(clock);

/* And print it, or WHATEVER! */

or, make a little function...

char *Date()
{
time_t *clock;
localtime(clock);
return(ctime(clock));
}

You would call it by...

char *ptr;
ptr = Date();

Hope this helps out!
Howie

PS -> You can easily control the format of that date string using strftime()!
Check your manula pages for that one ;-)

---
#include <std_disclaimer.h> /***************************************
#define OPINIONS my_own * Howard K Michalski, ho...@fnma.COM *
* Fannie Mae Washington, DC, USA *
main(){for(;;);} ***************************************/

Michael P. Mack

unread,
Jul 10, 1992, 2:25:39 AM7/10/92
to
In article <2...@ceylon.gte.com> dw...@harvey.gte.com (Donald W. Ottens) writes:

>[...] Is there a way to


>get the current date and time using the "date" command from the UNIX
>shell? How do I execute this from within a C program?

yes, use system("date");

Art Neilson

unread,
Jul 11, 1992, 4:42:59 PM7/11/92
to

NO. This is a lame solution.

Use the ctime(3) function, like so:

long ticks;
char *str;

ticks = time((char *)0);
str = ctime(&ticks);
printf("%s", str);

--
Arthur W. Neilson III | INET: a...@pilikia.pegasus.com
Bank of Hawaii Tech Support | UUCP: uunet!ucsd!nosc!pilikia!art

Michael P. Mack

unread,
Jul 15, 1992, 5:52:54 PM7/15/92
to

In article <1992Jul11.2...@pilikia.pegasus.com>
a...@pilikia.pegasus.com (Art Neilson) writes:

>In article <52...@ucsbcsl.ucsb.edu>
6500...@ucsbuxa.ucsb.edu (Michael P. Mack) writes:
>>In article <2...@ceylon.gte.com> dw...@harvey.gte.com (Donald W. Ottens) writes:>>
>>>[...] Is there a way to
>>>get the current date and time using the "date" command from the UNIX
>>>shell? How do I execute this from within a C program?
>>
>>yes, use system("date");

>NO. This is a lame solution.

I agree, but he asked specifically how to do it that way.

Mike Mack

0 new messages