Menu
  • HOME
  • TAGS

Timing in C with time.h

c,time,time.h

Here's a simple example, compiled on a Ubuntu box: #include <stdlib.h> #include <stdio.h> #include <stdint.h> #include <sys/time.h> #include <unistd.h> #include <time.h> int64_t timestamp_now (void) { struct timeval tv; gettimeofday (&tv, NULL); return (int64_t) tv.tv_sec * CLOCKS_PER_SEC + tv.tv_usec; } double timestamp_to_seconds (int64_t timestamp) { return timestamp / (double) CLOCKS_PER_SEC; }...

Infinite Loop and time

c,date,unix,infinite-loop,time.h

Your problem is that the computer runs so fast that your time() function can be called several times in the same second. What you need is to ensure that either your function stops the while loop after having run the task, or forbids the execution of the task: First: int...

a gettime() function that returns a uint16_t value in C

c,time.h,gettime,uint16

uint16_t getD() { time_t ti; ti = time(NULL); struct tm tm_time; tm_time = *localtime(&ti); //const time_t create_time; uint16_t t, d; d = tm_time.tm_mday + (tm_time.tm_mon + 1) * 32 + (tm_time.tm_year - (1980-1900)) * 512; // Print ddmmyy printf("%02d%02d%02d\n", (int) d%32, (int) (d/32)%16, (int) ((d/512)%128 + (1980-1900))%100); return d; }...

Conversion of timespec for Windows 7 VS 2010

c++,build,time.h

Assuming secs is a float value giving the time the thread shall sleep in seconds, such as e.g. float secs = 0.8312f; You can replace that for the windows version with: float secs = 0.8312f; DWORD delay = static_cast<DWORD>(fabs(secs * 1000.0f)); Sleep(delay); Possibly you could add some checks to this...

Validating Date of birth in C++

c++,time.h

Ok, so the problem is that localtime returns the same buffer when you call it multiple times. You need to copy the result (or use localtime_r which takes an extra parameter, but it's not quite as portable). Here's my debug session of your code (with uncommented section): (gdb) p norm...

Implementation of time in Zynq

c,time.h,zynq

Ok, I've done some research, and found this link. This is almost what I'v been searching, but instead of '_times()' I needed '_gettimeofday()' and this is my implementation: int _gettimeofday(struct timeval *__p, void *__tz) { __p->tv_sec = (systemUsCounter / 1000000); __p->tv_usec = systemUsCounter; return 0; } I left the '__tz'...

Why can C compile time() without its library?

c++,c,gcc,g++,time.h

You should include <time.h> for time(2) and turn on the warnings. In C, a function with no visible prototype is assumed to return int (which has been deprecated since C99). So compiling with gcc seems fine while g++ doesn't. Compile with: gcc -Wall -Wextra -std=c99 -pedantic-errors file.c and you'll see...

Random Number generator to output numbers in ascending order

c++,random,integer,srand,time.h

If you create the numbers in A as a vector, there is an algorithm header with sort, so somthing like: #include <vector> #include <algorithm> int main() { std::vector<int> numA(5); srand( time(NULL) ); for( unsigned int i(0); i < numA.size(); ++i ) numA[i] = (rand()%49+1); //After you create the vector and...

c++ program (Matrix and functions)

c++,std,iostream,ctime,time.h

this answer will probably not help you much, but perhaps you get the bits out of it that lead you to the solution. your matrix is a graph with ones being the "nodes" and the direct neighborship between two ones (north/east/south/west of a cell) defines an "edge". what you are...

Compare current time with given time

c,time,compare,time.h

Your comparison is wrong it should be if (mktime(&data) == rawtime) Or just if (mktime(&data) == time(NULL)) also, initiazlize all the fields of struct tm data; or memset(&data, 0, sizeof data); ...

Can't write the output of a loop to a file

c++,ofstream,time.h

Have to move ofstream fob; fob.open("contr.txt"); out of loop. Now you rewrite file on every iteration....