C ++ 14中一年中的分数日计算

问题描述 投票:6回答:1

我使用Howard Hinnants date.h库编写了以下代码来计算当前时间的年份的小数日。我想知道是否有更短的方法,因为我的代码感觉像std::chronodate调用过度。我可以直接计算自年初以来的分数天数(以微秒精度计算)并避免我的两步法吗?

#include <iostream>
#include <chrono>
#include "date.h"

int main()
{
    // Get actual time.
    auto now = std::chrono::system_clock::now();

    // Get the number of days since start of the year.
    auto ymd = date::year_month_day( date::floor<date::days>(now) );
    auto ymd_ref = date::year{ymd.year()}/1/1;
    int days = (date::sys_days{ymd} - date::sys_days{ymd_ref}).count();

    // Get the fractional number of seconds of the day.
    auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(now - date::floor<date::days>(now));
    double seconds_since_midnight = 1e-6*microseconds.count();

    // Get fractional day number.
    std::cout << "Fractional day of the year: " << days + seconds_since_midnight / 86400. << std::endl;

    return 0;
}
c++ date datetime time chrono
1个回答
6
投票

好问题(upvoted)。

我认为首先我们需要确定正确答案是什么。这是你的答案,目前唯一的答案是Matteo's。为了演示目的,我修改了两个答案以替换为“假现在”,以便我们可以比较苹果和苹果:

using namespace std::chrono_literals;
auto now = date::sys_days{date::March/27/2019} + 0h +  32min + 22s + 123456us;

(大概现在我正在写这篇文章)

Chiel's代码给出:

Fractional day of the year: 85.0225

Matteo's代码给出:

Fractional day of the year: 85.139978280740735

它们很接近,但不够接近,两者都被认为是正确的。

Matteo's代码与“平均年份”一起使用:

auto this_year = date::floor<date::years>(now);

date::years的长度是365.2425天,如果你平均400年期间的所有民用年份,这是完全正确的。使用平均年份长度非常有用,尤其是在处理不关心人造日历(例如物理或生物学)的系统时。

我猜想因为编写Chiel's代码的方式,他更希望得到的结果更精确地指向这个特定年份。因此,下面给出的代码是Chiel's的算法,产生完全相同的结果,只是稍微更有效和简洁。

// Get actual time.
auto now = std::chrono::system_clock::now();

// Get the number of days since start of the year.
auto sd = date::floor<date::days>(now);
auto ymd = date::year_month_day( sd );
auto ymd_ref = ymd.year()/1/1;
std::chrono::duration<double, date::days::period> days = sd - date::sys_days{ymd_ref};

// Get the fractional number of seconds of the day.
days += now - sd;

// Get fractional day number.
std::cout << "Fractional day of the year: " << days.count() << std::endl;

我注意到的第一件事是date::floor<date::days>(now)是在3个位置计算的,所以我计算它一次并将其保存在sd中。

接下来,由于最终答案是days的双重表示,我将让<chrono>通过将答案存储在duration<double, days>中来为我工作。每当你发现自己转换单位时,最好让<chrono>为你做。它可能不会更快。但它绝对不会慢或者错。

现在,将小数日添加到结果中是一件简单的事情:

days += now - sd;

使用任何精度now(微秒或其他)。结果现在只是days.count()

更新

只需要更多的时间来反映......

我注意到,使用上面的简化代码,可以更容易地将整个算法看作单个表达式。那就是(删除名称空间限定以便将所有内容都放在一行上):

duration<double, days::period> days = sd - sys_days{ymd_ref} + now - sd;

这显然在代数上简化为:

duration<double, days::period> days = now - sys_days{ymd_ref};

综上所述:

using namespace std::chrono;
using namespace date;

// Get actual time.
auto now = system_clock::now();

// Get the start of the year and subract it from now.
using ddays = duration<double, days::period>;
ddays fd = now - sys_days{year_month_day{floor<days>(now)}.year()/1/1};

// Get fractional day number.
std::cout << "Fractional day of the year: " << fd.count() << '\n';

在这种情况下,让<chrono>为我们进行转换,允许代码充分​​简化,以便算法本身可以代数简化,从而产生更清晰,更有效的代码,可以证明它与OP问题中的原始算法相当。

© www.soinside.com 2019 - 2024. All rights reserved.