time.h

questions on clock(),time() and difftime() in time.h?

廉价感情. 提交于 2019-12-12 01:26:50
问题 int fib(int n,int a,int b){ if (n==2){ return a+b; } return fib(n-1,b,a+b); } int main() { time_t begin,end; begin = clock(); fib(10000,0,1); end = clock(); printf("%f",difftime(end,begin)); } int main() { time_t begin,end; time(&begin); fib(10000,0,1); time(&end); printf("%f",(double)(end-begin)/CLOCKS_PER_SEC); } 1)First question Why does my first main give prints me a time 288.000000 ,I'm assuming this is 288 milliseconds but in the documentation it is supposed to give me the result in

strptime giving “implicit declaration” and “undefined reference”

≯℡__Kan透↙ 提交于 2019-12-11 12:35:43
问题 So, when I use the function strptime I get both a warning: warning: implicit declaration of function 'strptime' and an error after that: undefined reference to 'strptime' Yes, I've included time.h . Here is a small sample code of me using it. #include <time.h> void my_function() { char buf* = "2016-02-05 12:45:10"; struct tm time*; ... strptime(buf, "%F %T", &time); ... } I know time.h is working because in the same .c file, I'm using strftime , time_t , and 'struct tm from time.h without a

time.h clock() broken on OS X?

帅比萌擦擦* 提交于 2019-12-11 10:37:33
问题 Am I going mad? I'm running this on x86_64. #include <stdio.h> #include <time.h> #include <unistd.h> int main(int argc, char *argv[]) { printf("Clock: %f\n", clock() / (double)CLOCKS_PER_SEC); sleep(1); printf("Clock: %f\n", clock() / (double)CLOCKS_PER_SEC); sleep(1); printf("Clock: %f\n", clock() / (double)CLOCKS_PER_SEC); return 0; } This prints Clock: 0.002880 Clock: 0.002968 Clock: 0.003019 It clearly waits for a second at the sleep(1) lines, but the output is clearly wrong. If that

Declaration conflicts between time.h and linux/time.h prevent me from using CLOCK_TAI

﹥>﹥吖頭↗ 提交于 2019-12-11 08:58:00
问题 I would like to use #include <time.h> clock_gettime(CLOCK_TAI, &...); but unfortunately CLOCK_TAI is not defined in stock time.h header (in openSUSE 13.2 at least). It is however defined in linux/time.h and actually supported by the operating system. But if I include the latter header, it provokes a bunch of declaration conflicts — versus both time.h and bits/types.h . Including only the linux/time.h does not help, as time.h and/or bits/types.h will be implicitly included by common headers,

c/c++ microsecond timestamp

妖精的绣舞 提交于 2019-12-08 07:52:37
问题 I used this piece of code to get timestamp in microsecond in c/c++. but it doesn't look like microsecond. also i don't know if there is any way to format it. timeval curTime; gettimeofday(&curTime, NULL); int milli = curTime.tv_usec / 1000; unsigned long micro = curTime.tv_usec*(uint64_t)1000000+curTime.tv_usec; char buffer [80]; //localtime is not thread safe strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", localtime(&curTime.tv_sec)); char currentTime[84] = ""; char currentTime2[80] = ""; sprintf

Ncurses and realtime (implemented in C, unix)

橙三吉。 提交于 2019-12-05 09:13:29
I am trying to implement a game using ncurses in C. I have to show the current time (the time must update each second) and my while loop looks like this while(1) { clk = time(NULL); cur_time = localtime(&clk); mvprintw(0,1,"%d %d %d",cur_time->tm_hour,cur_time->tm_min,cur_time->tm_sec); int key = getch() //other stuff } My problem is that the time will refresh only when I press a key. Is it a way to make the time refresh without the need of pressing a key (and to implement this in the same while)? There are a couple of functions you could use: nodelay timeout int nodelay(WINDOW *win, bool bf);

How to decompose unix time in C

冷暖自知 提交于 2019-12-05 02:30:41
问题 This seems like something no one should ever have to do, but I'm working on a kernel module for an embedded system (OpenWRT) in which it seems that time.h does include the timespec and time_t types, and the clock_gettime and gmtime functions, but does not include localtime , ctime , time , or, critically, the tm type. When I attempt to cast the return pointer from gmtime to my own struct, I get a segfault. So I guess I'd be content to solve the problem either of two ways—it'd be great to

What is the difference between clock_t, time_t and struct tm?

这一生的挚爱 提交于 2019-12-04 09:05:38
问题 What is the difference between clock_t, time_t and struct tm? struct tm looks like this: struct tm{ int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; }; But how does clock_t and time_t look like? 回答1: time_t is an absolute time, represented as the integer number of seconds since the UNIX epoch (midnight GMT, 1 January 1970). It is useful as an unambiguous, easy-to-work-with representation of a point in time. clock_t is a

How to decompose unix time in C

荒凉一梦 提交于 2019-12-03 20:08:34
This seems like something no one should ever have to do, but I'm working on a kernel module for an embedded system (OpenWRT) in which it seems that time.h does include the timespec and time_t types, and the clock_gettime and gmtime functions, but does not include localtime , ctime , time , or, critically, the tm type. When I attempt to cast the return pointer from gmtime to my own struct, I get a segfault. So I guess I'd be content to solve the problem either of two ways—it'd be great to figure out how to get access to that missing type, or alternatively, how to roll my own method for

What is the difference between clock_t, time_t and struct tm?

╄→尐↘猪︶ㄣ 提交于 2019-12-03 01:56:00
What is the difference between clock_t, time_t and struct tm? struct tm looks like this: struct tm{ int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; }; But how does clock_t and time_t look like? time_t is an absolute time, represented as the integer number of seconds since the UNIX epoch (midnight GMT, 1 January 1970). It is useful as an unambiguous, easy-to-work-with representation of a point in time. clock_t is a relative measurement of time, represented by an integer number of clock ticks since some point in time (possibly