4

In C/C++, suppose I have the following values:

int year = 2010;
int month = 6;
int day = 15;
int hour = 14;
int minute = 52;
int seconds = 34;
int microseconds = 141231;

What's the easiest way to convert this to a timeval? I think timeval is time since Jan 1, 1970? But to calculate this manually everytime seems very tedious. What's the easiest (and fastest) way to do this?

Thanks

Sadique
  • 22,572
  • 7
  • 65
  • 91
user8125
  • 915
  • 2
  • 7
  • 6

2 Answers2

8

You can use mktime(3) to convert a struct tm structure into a time_t. After you have the time_t, you can copy it and your microseconds value directly into a struct timeval structure.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
4

You might want to look into std::mktime().

Sadique
  • 22,572
  • 7
  • 65
  • 91
  • `std::mktime()` normally neither takes nor produces anything with microseconds, see original post. – Jonas Sep 18 '17 at 18:33