I am wondering is there any function that would return the current time in seconds, just 2 digits of seconds? I'm using gcc 4.4.2.
+1
A:
A more portable way to do this is to get the current time as a time_t
struct:
time_t mytime = time((time_t*)0);
Retrieve a struct tm
for this time_t
:
struct tm *mytm = localtime(&mytime);
Examine the tm_sec
member of mytm
. Depending on your C library, there's no guarantee that the return value of time()
is based on a number of seconds since the start of a minute.
Charles Bailey
2010-02-11 07:54:50
`time_t mytime((time_t*)0);` is invalid in C and has a type mismatch in C++. Did you mean `time_t mytime = time(0);`?
R Samuel Klatchko
2010-02-11 08:08:10
@R Samuel Klatchko: Yes, thanks. I've been doing too much C++ and then forgot the crucial `time` call itself. (Actually, I forgot the variable name originally but messed up fixing it while writing the answer.)
Charles Bailey
2010-02-11 08:12:50
+1
A:
I believe this is what you're after. If not, my apologies. :)
#include <stdio.h>
#include <string.h>
#include <time.h>
int main() {
char secstr[128];
struct tm current_time;
time_t current_secs = time(NULL);
memset(secstr, 0, sizeof(secstr));
localtime_r(¤t_secs, ¤t_time);
strftime(secstr, sizeof(secstr), "%S", ¤t_time);
fprintf(stdout, "The second: %s\n", secstr);
return(0);
}
Michael Foukarakis
2010-02-11 07:55:42
+3
A:
The following complete program shows you how to access the seconds value:
#include <stdio.h>
#include <time.h>
int main (int argc, char *argv[]) {
time_t now;
struct tm *tm;
now = time(0);
if ((tm = localtime (&now)) == NULL) {
printf ("Error extracting time stuff\n");
return 1;
}
printf ("%04d-%02d-%02d %02d:%02d:%02d\n",
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
return 0;
}
It outputs:
2010-02-11 15:58:29
How it works is as follows.
- it calls
time()
to get the best approximation to the current time (usually number of seconds since the epoch but that's not actually mandated by the standard). - it then calls
localtime()
to convert that to a structure which contains the individual date and time fields, among other things. - at that point, you can just de-reference the structure to get the fields you're interested in (
tm_sec
in your case but I've shown a few of them).
Keep in mind you can also use gmtime()
instead of localtime()
if you want Greenwich time, or UTC for those too young to remember :-).
paxdiablo
2010-02-11 08:00:46
@robUK Just in case you will use this code in a multithreaded application with lots of threads getting seconds in this way. Is is likely that `localtime` use one global mutex in its work (while accessing global locale object). So if you use it in a function that is often called your threads will spend their time waiting in `pthread_mutex_unloc()`
skwllsp
2010-02-11 09:17:14
It's possibly worse than that, @skwlisp. Unless the return value is a pointer to a thread-specific object, you may find it corrupted. In a threaded environment, you should probably use the "_r" variants of unsafe calls. I just didn't want to clutter up my answer with a possibly unnecessary detail.
paxdiablo
2010-02-11 10:22:40
I've been working on a Standard C Library implementation recently, and wanted to add a few details. `time()` doesn't necessarily return the number of seconds since the epoch (but it does on most platforms). And `tm_sec` can range from 0 to 61 (to account for leap seconds).
tomlogic
2010-02-11 20:06:55
That's a good point, @tomlogic, I wasn't aware the standard stated the "best approximation" nor that it had made the encoding implemetation-specific. Modified to suit. I think the range of tm_sec is actually 0 thru 60 rather than 61 though, but that's probably just a typo on your part. The crux of your point is made: it may not _always_ be 0 thru 59.
paxdiablo
2010-02-11 23:03:16