Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I portably add a large number of seconds to a time_t object?

Tags:

c++

c++11

Well I think the title sums it up. Suppose I have an object of type double which I obtained from running std::difftime on two time_t objects, and now I want to add the resulting number of seconds back to a time_t object. I don't mind losing fractions of seconds.

Note that the number of seconds might be large (ie. larger than the 60 seconds allowed in struct tm, but always lower than any integer primitive used to represent seconds on the respective machine / implementation, and never larger than the order of 1 year, although preferably I would not like this to be a restriction).

How would I go about doing this portably (ie. as per C standard)?

I am hoping not to have to divide into months, days, hours, minutes etc. and add them manually to struct tm object. Surely there's a better way!?

like image 238
quant Avatar asked Sep 21 '13 12:09

quant


People also ask

Can you add to time_t?

The C date/time type time_t is implemented as the number of seconds since a certain date, so to add seconds to it you simply use normal arithmetic.

Is time_t in seconds or milliseconds?

The C library function time_t time(time_t *seconds) returns the time since the Epoch (00:00:00 UTC, January 1, 1970), measured in seconds.

How do you get seconds in C++?

time() : time() function returns the time since the Epoch(jan 1 1970) in seconds. Prototype / Syntax : time_t time(time_t *tloc); Return Value : On success, the value of time in seconds since the Epoch is returned, on error -1 is returned.

What is time_ t in c++?

time_t time(time_t *time); This returns the current calendar time of the system in number of seconds elapsed since January 1, 1970. If the system has no time, . 1 is returned.


1 Answers

You are allowed to perform arithmetic with time_t values.

You can set up two struct tm objects one second apart (choose two times in the middle of a minute somewhere, where leap seconds are not allowed to occur, and at a local time where DST does not change), and call mktime() on them both to get two time_t values. Subtract the earlier of of those time_t values from the later, and this will give you a second in the same number of units in which time_t is measured on your system. Multiply this by the number of seconds you want to add, and add the result to your original time_t value. You can check the result by again calling difftime() on the original and new time_tvalues to see if the seconds difference was what you were expecting.

Note that even this isn't guaranteed to be portable, since technically, time_t isn't required to even have a resolution capable of distinguishing seconds, but it would be a pretty odd implementation that did not. Also, if time_t is a floating point value (which is unusual) and you wanted to add a really, really large number of seconds, you might run into floating point precision issues. In this case you could try adding hours for instance, instead of seconds, and set up your two struct tm structs accordingly. You might conceivably run into overflow issues with a large enough number of seconds, which you just can't do much about.

like image 190
Crowman Avatar answered Sep 28 '22 07:09

Crowman