0

For example, I want to count how many seconds I have lived on earth. With gettimeofday()/time(), I can get the seconds count since Epoch, what else I want is the diff from Epoch to my birthday, so my question is is there function like:

time_t gettimeof_specified_day(my_birthday);

It return the seconds from Epoch to my birthday, then I can use difftime(now, birthday) to get what I want.

Null
  • 55
  • 6
  • Note that [Epoch time](https://en.wikipedia.org/wiki/Unix_time) is not the same as number of seconds since epoch as it omits the leap seconds. Depending on the purpose it might be or might not be a duplicate and answer below might or might not be an answer (it's not a duplicate and the answer below is incorrect if you ask for the exact number of seconds). – Maciej Piechotka May 08 '14 at 04:52

1 Answers1

1

You can use the mktime(3) function to convert any particular calendar date and time (expressed as a struct tm instance) into a time_t value:

// Convert April 14, 1980 00:00:00 (local time, based on your computer's
// current time zone) into a time_t
struct tm my_birthday;
memset(&my_birthday, 0, sizeof(my_birthday));
my_birthday.tm_year = 80;  // Number of years since 1900
my_birthday.tm_mon = 3;    // Month from 0-11
my_birthday.tm_mday = 14;  // Day of month from 1-31
time_t t = mktime(&my_birthday);
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589