I have a double
containing seconds. I would like to convert this into a struct tm
.
I can't find a standard function which accomplishes this. Do I have to fill out the struct tm
by hand?
I just accidentally asked this about converting to a time_t
and http://www.StackOverflow.com will not let me post unless I link it.
MSalters answer is correct, but I thought I'd add a bit of detail on how you should convert to
time_t
and how you should convert totm
.So given a number of seconds in
double input
you can use the implementation dependent method of casting:But since
time_t
is implementation defined there is no way to know that this is a primitive that can simply be cast to. So the guaranteed method would be to use the chrono library's implementation independent method of converting:The conversion options are discussed in more detail here: https://stackoverflow.com/a/50495821/2642059 but once you have obtained your
time_t
through one of these methods you can simply uselocaltime
to converttemp
to astruct tm
.Note the dereference is important. It will use the default copy assignment operator so
output
is captured by value, which is essential because: