如何将大量秒数可移植地添加到time_t对象? [英] How do I portably add a large number of seconds to a time_t object?

查看:48
本文介绍了如何将大量秒数可移植地添加到time_t对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我认为标题是对它的总结。假设我有一个类型为 double 的对象,该对象是通过在两个 std :: difftime 而获得的> time_t 对象,现在我想将生成的秒数加回到 time_t 对象中。我不介意丢掉几分之一秒。

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.

请注意,秒数可能很大(即大于中允许的60秒struct tm ,但始终小于用于表示相应机器/实现上的秒数的任何整数基元,并且永远不大于1年的量级,尽管最好我不希望这是一个限制) 。

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).

我该如何便携地做到这一点(即按照C标准)?

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

我希望不必分成几个月,几天,几小时,几分钟等,然后将它们手动添加到 struct tm 对象中。当然有更好的方法!?

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!?

推荐答案

您可以使用 time_t 值。

您可以相隔一秒设置两个 struct tm 对象(在一分钟的某个地方,不允许出现leap秒,并且在当地时间DST不变),然后分别对它们调用 mktime()两个 time_t 值。从较晚的 time_t 值中减去较早的值,这将使您在与 time_t 是在您的系统上测量的。将此乘以要添加的秒数,然后将结果添加到原始的 time_t 值。您可以通过在原始值和新值 time_t 上再次调用 difftime()来查看结果,以查看秒数是否不同正是您所期望的。

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.

请注意,即使这样也不保证具有可移植性,因为从技术上讲, time_t 甚至不需要具有能够区分秒的分辨率,但这不是一个很奇怪的实现。另外,如果 time_t 是浮点值(这是不寻常的),并且您想添加非常多的秒数,则可能会遇到浮点精度问题。在这种情况下,您可以尝试添加小时(而不是秒),然后相应地设置两个 struct tm 结构。可以想象,您可能会在足够多的时间内遇到溢出问题,而这只是做不了多少。

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.

这篇关于如何将大量秒数可移植地添加到time_t对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆