I am using C++. I want to get computer time (I know how to do with ctime function). I want to take time like a int variable (sec, min, etc.). Any help?
Something like: int sec = get.sec(); int min = get.min();
I don`t want a time_t variable.
I am using C++. I want to get computer time (I know how to do with ctime function). I want to take time like a int variable (sec, min, etc.). Any help?
Something like: int sec = get.sec(); int min = get.min();
I don`t want a time_t variable.
In C++11 you could use <chrono>
. You might also use time(2), localtime(3), strftime(3), clock(3), clock_gettime(2) (if your system have them). Probably
time_t now=0;
time(&now);
char nowbuf[64];
strftime(nowbuf, sizeof(nowbuf), "%c", localtime(now));
might be relevant if you want some string. Otherwise, notice that localtime
returns a pointer to a struct tm
which has many numerical fields. e.g.
struct tm* lt = localtime(now);
int hours = lt->tm_hour;
int minutes = lt->tm_min;
Of course, in principle you should need to test against failure of time
, localtime
etc... (but I never had these functions fail).
Details are usually operating system specific. If on Linux, read time(7); some framework libraries like POCO or Qt might provide a common (OS independent) abstraction above them.
BTW, you may or not care about time zones and you might want gmtime
instead of localtime
.