tags:

views:

341

answers:

4

I have something like this

char *current_day, *current_time;
system("date +%F);
system("date +%T);

It prints the current day and time in the stdout, but I want to get this output or assign them in current_day and current_time variables, so that I can do some processing with those values later on.

current_day ==> current day
current_time ==> current time

Only solution that I can think of now is direct the output to some file, and then read the file and then assign the values of date and time to current_day and current_time. But I think this is not so good way. Is there any other short and elegant way ?

Hope the problem is clear !!

Thanks.

+8  A: 

Use time(2) and localtime(3) to get the time:

time_t t = time(NULL);
struct tm tm = *localtime(&t);

printf("now: %d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
Adam Rosenfield
When I try to compile, I get this error test.c:13: warning: passing argument 1 of ‘localtime’ from incompatible pointer type, what should I do ?
seg.server.fault
You don't pass a `struct tm *` to `localtime` - that line should be `tm = *localtime(`
caf
Bah, so stupid of me. Thanks, fixed now.
Adam Rosenfield
+6  A: 

time_t rawtime;
time ( &rawtime ); struct tm *timeinfo = localtime ( &rawtime );

You can also use strftime to format the time into a string.

Martin Beckett
Or indeed, `ctime()` on the `time_t` value.
caf
ctime() produces a tatty non-internationalized date/time format. It is best forgotten about.
Jonathan Leffler
A: 

instead of files use pipes and if u wana use C and not C++ u can use popen like this

#include<stdlib.h>
#include<stdio.h>

FILE *fp= popen("date +F","r");

and use *fp as a normal file pointer with fgets and all

if u wana use c++ strings, fork a child, invoke the command and then pipe it to the parent.

   #include <stdlib.h>
   #include <iostream>
   #include <string>
   using namespace std;

   string currentday;
   int dependPipe[2];

   pipe(dependPipe);// make the pipe

   if(fork()){//parent
           dup2(dependPipe[0],0);//convert parent's std input to pipe's output
           close(dependPipe[1]);
           getline(cin,currentday);

    } else {//child
        dup2(dependPipe[1],1);//convert child's std output to pipe's input
        close(dependPipe[0]);

        system("date +%F");
    }

// make a similar 1 for date +T but really i recommend u stick with stuff in time.h GL

OSaad
Calling an external program is overkill (and makes the program more brittle) and makes difficult to do things with the time afterwards (such as adding an offset, for instance). That's precisely what the OP wanted to avoid. And this is "date", not "Date".
bortzmeyer
i totally agree but um the guy asked for a way to invoke the command and use its output in his prog, i gave him what he WANTED! and yet i said at the end, stick with time.h cause thats the right thing to do :S i cant see anything wrong with my answer :S n as for "Date" thnx i fixed that.
OSaad
A: 

The answers given above are good CRT answers, but if you want you can also use the Win32 solution to this. It's almost identical but IMO if you're programming for Windows you might as well just use its API (dunno if you are programming in windows actually but whatever)

char* arrDayNames[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; // Jeez I hope this works, I haven't done this in ages and it's hard without a compiler..
SYSTEMTIME st;
GetLocalTime(&st); // Alternatively use GetSystemTime for the UTC version of the time
printf("The current date and time are: %d/%d/%d %d:%d:%d:%d", st.wDay, st.wMonth, st.wYear, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
printf("The day is: %s", arrDayNames[st.wDayOfWeek]);

Anyway, this is your windows solution. Hope it'll prove helpful for you sometime!

Ori Osherov