Convert double to time_t

Jonathan Mee picture Jonathan Mee · Jun 23, 2015 · Viewed 8.8k times · Source

I have a double containing seconds. I would like to convert this into a time_t.

I can't find a standard function which accomplishes this. Do I have to fill out the time_t by hand?

Answer

masoud picture masoud · Jun 23, 2015

The type of std::time_t is unspecified.

Although not defined, this is almost always an integral value holding the number of seconds (not counting leap seconds) since 00:00, Jan 1 1970 UTC, corresponding to POSIX time.

So, just a safe casting between them could be fine. Also be carefull about portability (because it's type is not specified in the standard) and consider about the values than can not fit while casting from double to integrals.