Questions tagged [time.h]

time.h is a C language header, supplying date and time utility types and functions; it is also available in C++, as either time.h or ctime.

time.h is a C language header, supplying date and time utility types and functions; it is also available in C++, as either time.h or ctime (with the latter being preferred). It supplies types tm, time_t, clock_t, and timespec (as of C11), constant CLOCKS_PER_SEC, and functions such as clock(), time(), mktime(), and so on.

266 questions
170
votes
4 answers

C++ error: undefined reference to 'clock_gettime' and 'clock_settime'

I am pretty new to Ubuntu, but I can't seem to get this to work. It works fine on my school computers and I don't know what I am not doing. I have checked usr/include and time.h is there just fine. Here is the code: #include #include…
naspinski
  • 34,020
  • 36
  • 111
  • 167
29
votes
6 answers

How can i get UTCTime in millisecond since January 1, 1970 in c language

Is there any way to get milliseconds and its fraction part from 1970 using time.h in c language?
Siddiqui
  • 7,662
  • 17
  • 81
  • 129
22
votes
3 answers

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

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…
Cristi
  • 1,195
  • 6
  • 17
  • 24
18
votes
4 answers

Why return a static pointer instead of an out parameter?

char* asctime (const struct tm * timeptr); char* ctime (const time_t * timer); I found that many functions inside of time.h return pointers to static variables, which could be altered by any subsequent call to those functions. That means I have to…
13
votes
3 answers

How to get the current time in milliseconds in C Programming

Possible Duplicate: How to measure time in milliseconds using ANSI C? How can I get the Windows system time with millisecond resolution? We want to calculate the time which a player have taken to finish the game. But with time.h we could only…
kk-dev11
  • 2,654
  • 5
  • 37
  • 48
11
votes
3 answers

Does struct tm store time zone information as its data member

Consider the following C++ code #include #include int main() { std::time_t now = std::time(nullptr); struct tm local = *std::localtime(&now); struct tm gm = *std::gmtime(&now); char str[20]; std::strftime(str,…
aafulei
  • 2,085
  • 12
  • 27
11
votes
1 answer

What is the proper way to use clock_gettime()?

I was trying out this function in a C program, and it keeps printing the wrong time. This is my code at the moment: #include #include #include #include int main( int argc, char **argv ){ struct…
Zen Hacker
  • 733
  • 2
  • 6
  • 13
9
votes
5 answers

Constants not loaded by compiler

I started studying POSIX timers, so I started also doing some exercises, but I immediately had some problems with the compiler. When compiling this code, I get some strange messages about macros like CLOCK_MONOTONIC. Those are defined in various…
Germano Massullo
  • 2,572
  • 11
  • 40
  • 55
8
votes
1 answer

C - How to convert time_t to tm?

I have a variable which using time_t data type. I want to convert this type into "YYYY-MM-DD HH:MM:SS". I just know if it's only works in localtime() example: char buff[20]; time_t now = time(NULL); strftime(buff, 20, "%Y-%m-%d %H:%M:%S",…
Angga
  • 95
  • 1
  • 1
  • 7
7
votes
2 answers

Why can C compile time() without its library?

When I use the time() function (i.e., just randomize seed for rand() ) but not include the header file time.h, it works for C. For example: #include #include int main() { int i; srand(time(NULL)); for(i=0;i<10;i++){ …
maynak
  • 173
  • 6
7
votes
5 answers

How do I get milliseconds since midnight UTC in C?

The time function in time.h gives milliseconds since the epoch.
Jake
6
votes
3 answers

difference between two date in C

I am trying to get the difference between two date by using below C code. but code always giving difference 0. Help me to where i am making mistake. I am using gcc compiler under linux. #include #include int main () { …
sujin
  • 2,813
  • 2
  • 21
  • 33
6
votes
3 answers

Ncurses and realtime (implemented in C, unix)

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…
Silent Control
  • 614
  • 10
  • 22
5
votes
2 answers

Why is the tm_year member in struct tm relative to 1900 rather than 1970 in C on macosx?

I was trying out the examples in expert C programming while I encountered this problem. My program basically does one thing: use the standard gmtime function and see how many years has past since 1970. Here is my program: #include #include…
Rainbow Fizz
  • 77
  • 1
  • 1
  • 4
5
votes
1 answer

clockid_t (clock_gettime first argument) portability

Most POSIX-compatible systems provide a function to get or set one of high-resolution timers: int clock_gettime(clockid_t clock_id, struct timespec *tp); Documentation for each system usually lists several symbolic names as possible clock_id…
Anton Samsonov
  • 1,380
  • 17
  • 34
1
2 3
17 18