Convert milliseconds to timespec for GNU port

jparthj picture jparthj · Feb 22, 2013 · Viewed 34.9k times · Source

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.

Answer

user405725 picture user405725 · Feb 22, 2013

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:

  1. 4321 (milliseconds) / 1000 = 4 (seconds)
  2. 4321 (milliseconds) % 1000 = 321 (milliseconds)
  3. 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:

  1. 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).
  2. 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!