I want to convert milliseconds into timespec structure used by GNU Linux. I have tried following code for the same.
timespec GetTimeSpecValue(unsigned long milisec)
{
struct timespec req;
//long sec = (milisecondtime /1000);
time_t sec = (time_t)(milisec/1000);
req->tv_sec = sec;
req->tv_nsec = 0;
return req;
}
Running this code gives me the following error.
expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘GetTimeSpecValue’
I have also include time.h file in the code.
The timespec
structure represents time in two portions — seconds and nanoseconds. Thus, the algorithm for conversion from milliseconds is pretty darn simple. One seconds has thousand milliseconds, one milliseconds has a thousand microseconds and one microsecond has a thousand nanoseconds, for which we are grateful to SI. Therefore, we first need to divide milliseconds by a thousand to get a number of seconds. Say, for example, 1500 milliseconds / 1000 = 1.5 seconds. Given integer arithmetics (not a floating point), the remainder is dropped (i.e. 1500 / 1000 is equal to just 1, not 1.5). Then we need to take a remainder that denotes a number of milliseconds that is definitely less than one second, and multiply it by a million to convert it to nanoseconds. To get a remainder of dividing by 1000, we use a module operator (%
) (i.e. 1500 % 1000 is equal to 500
). For example, let's convert 4321 milliseconds to seconds and nanoseconds:
- 4321 (milliseconds) / 1000 = 4 (seconds)
- 4321 (milliseconds) % 1000 = 321 (milliseconds)
- 321 (milliseconds) * 1000000 = 321000000 (nanoseconds)
Knowing the above, the only thing that is left is to write a little bit of C code. There are few things that you didn't get right:
- In C, you have to prefix structure data types with
struct
. For example, instead of saying timespec
you say struct timespec
. In C++, however, you don't have to do it (unfortunately, in my opinion).
- You cannot return structures from the function in C. Therefore, you need to pass a structure by pointer into a function that does something with that structure.
Edit: This contradicts (Return a `struct` from a function in C).
OK, enough talking. Below is a simple C code example:
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
static void ms2ts(struct timespec *ts, unsigned long ms)
{
ts->tv_sec = ms / 1000;
ts->tv_nsec = (ms % 1000) * 1000000;
}
static void print_ts(unsigned long ms)
{
struct timespec ts;
ms2ts(&ts, ms);
printf("%lu milliseconds is %ld seconds and %ld nanoseconds.\n",
ms, ts.tv_sec, ts.tv_nsec);
}
int main()
{
print_ts(1000);
print_ts(2500);
print_ts(4321);
return EXIT_SUCCESS;
}
Hope it helps. Good Luck!
try this:
struct timespec GetTimeSpecValue(unsigned long millisec) {
struct timespec req;
req.tv_sec= (time_t)(millisec/1000);
req.tv_nsec = (millisec % 1000) * 1000000;
return req;
}
I don't think struct timespec is typedef'ed,hence you need to prepend timespec with struct. And work out the nano second part if you want to be precise. Note that req is not a pointer. Thus members cannot be accessed with '->'
Incorporating a few tweaks to the answer including Geoffrey's comment, the code below avoids divides for small delay and modulo for long delay:
void msec_to_timespec(unsigned long msec, struct timespec *ts)
{
if (msec < 1000){
ts->tv_sec = 0;
ts->tv_nsec = msec * 1000000;
}
else {
ts->tv_sec = msec / 1000;
ts->tv_nsec = (msec - ts->tv_sec * 1000) * 1000000;
}
}