Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c+中的年持续时间算法+;_C++_Algorithm_Duration - Fatal编程技术网

C++ c+中的年持续时间算法+;

C++ c+中的年持续时间算法+;,c++,algorithm,duration,C++,Algorithm,Duration,我正在制作一个需要一年时间的程序 在其他方式中,DD/MM/YYYY的时间+持续时间=DD/MM/YYYY的时间+1 因此,它可能并不总是365天(2012年2月29日将成为2013年2月28日) 这是我的算法: if YEAR is leap than if we are before the 29th feb' than return 365+1 days else if we are the 29th feb' than return 365-1 days else

我正在制作一个需要一年时间的程序

在其他方式中,DD/MM/YYYY的时间+持续时间=
DD/MM/YYYY的时间+1

因此,它可能并不总是365天(2012年2月29日将成为2013年2月28日)

这是我的算法:

if YEAR is leap than
    if we are before the 29th feb' than return 365+1 days
    else if we are the 29th feb' than return 365-1 days
    else return 365 days
else if YEAR+1 is leap than
    if we are before or the 28th feb' than return 365 days
    else return 365+1 days
else return 365 days
这里,一天是60*60*24秒

这个算法似乎有效。但我想知道是否有其他方法可以在没有所有这些条件和只有2个可能的返回值的情况下实现这一点,或者只是一些“技巧”来优化这件事

我试图从
struct tm
中增加
tm\u year
,如下所示:

// t is the input time_t
struct tm Tm (*localtime(&t));
if (Tm.tm_mon == 2 && Tm.tm_mday == 29) --Tm.tm_mday;
++Tm.tm_year;
return mktime(&Tm) - t;
但结果不是我想要的,我得到了-1小时,或-25

我想这是因为一年并不完全是365*24*60*60。

我会用它,因为它已经实现了您所寻找的:

#include <iostream>
#include <boost/date_time/gregorian/gregorian_types.hpp>
namespace date = boost::gregorian;

int main() {
   date::date_period dp(date::date(2012, 6, 4), date::date(2013, 6, 4));
   long days = dp.length().days();
   std::cout << "Days between dates: " << days << std::endl;

通常,
时间
以秒为单位。因此,您只需调用td.total_seconds()
即可获得所需的值。

一个太阳年的长度不是一个固定的数字。政府发明了一种补偿闰年的方法,但这种方法并不完全精确。也就是说,“如果一年可以被4整除,那么它就是闰年,除非它可以被100整除,但是如果它可以被400整除,那么它又是闰年。”

我们在伊朗有一个例子,在这个例子中,第二个地球绕太阳转了一整圈。在同一个链接中,你可以看到平均太阳年为365.2422天,春分之间的平均间隔为365.2424天

更多细节以秒为单位给出了太阳年(热带年)的长度

if YEAR is leap than
    if we are before the 29th feb' than return 365+1 days
    else if we are the 29th feb' than return 365-1 days
    else return 365 days
else if YEAR+1 is leap than
    if we are before or the 28th feb' than return 365 days
    else return 365+1 days
else return 365 days
简化为:

if (YEAR is leap)
    if (< 29th Feb) return 365+1
    if (= 29th Feb) return 365-1
else if (YEAR+1 is leap)
    if (> 29th Feb) return 365+1

return 365
if(年份为闰年)
如果(<2月29日)返回365+1
如果(=2月29日)返回365-1
否则,如果(年份+1为闰年)
如果(>2月29日)返回365+1
返回365
但是为什么要这样做呢?拥有可读的代码比“技巧”优化要好得多


正如@betabando所建议的,类似于
date(year+1,mon,day)-date(year,mon,day)
会简单得多,可读性强得多,并且能够处理闰年、闰秒和。

是的,我知道一年比365天多一点,但实际上我并不想找到地球绕太阳运行的绝对秒数,这要感谢它的精确性。@perelo,那么你到底想要什么?天数“今年呢?如果闰年是366,否则是365。你不需要检查今天是否是2月29日!我需要得到今天和一年后的同一天之间的秒数,所以这可能不是一个完整的时间year@Perelo,我明白了。您的
mktime
解决方案应该可以工作,您确定您是否正确测试了它?它返回过去的某个时间是没有意义的,因为它增加了一年!似乎当我将struct tm初始化为2月29日之前的日期时,mktime减去了一个小时!Boost将是一个很好的选择,但我不确定我是否会为一个功能包含这个相当大的库……Pelelo给你的项目添加一个新的依赖项是你必须仔细考虑的。然而,我已经把Boost看作是C++的一部分,因为它在C++中简化了很多编程。此外,如果您在Linux中编程,并且不需要从_string函数中
到_string
/
,那么boost::date_time只是一个标题库(即,您不需要将程序与boost链接,只需要包含几个标题)。@perelo实际上,它在Windows中也是一个仅标题依赖项(除非您使用Visual C++ 6或X编译器或Borland编译器)。更多信息。@ Pelelo,您可以查看使用Boost的利弊。谢谢。Boosid/DATEYTIME只包含<代码> HPP< /COD>文件,所以我可以只包括:)是的,但是使用StuttTM不太好,虽然我正在测试boost atmLeap秒,但它将天数更改为1秒,但这并不能解释3600秒。
if (YEAR is leap)
    if (< 29th Feb) return 365+1
    if (= 29th Feb) return 365-1
else if (YEAR+1 is leap)
    if (> 29th Feb) return 365+1

return 365