【问题标题】:Fractional day of the year computation in C++14C++14 中一年中的小数天数计算
【发布时间】:2019-08-17 11:18:08
【问题描述】:

我使用 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;
}

【问题讨论】:

  • 代码是否按预期工作?那么您可能会在Code Review Stack Exchange 上得到更好的答案。
  • @MikeBorkland。如何使用datechrono 跳过两步法对我来说似乎是一个有效的问题。我需要改写吗?

标签: c++ date datetime time chrono


【解决方案1】:

好问题(赞成)。

我认为首先我们需要确定正确的答案是什么。这是你的答案,目前唯一的其他答案是Matteo's。出于演示目的,我修改了两个答案以替换为“fake now”,以便我们可以将苹果与苹果进行比较:

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&lt;date::days&gt;(now) 在 3 个地方被计算,所以我计算它一次并将它保存在 sd

接下来,由于最终答案是days 的双基表示,我将让&lt;chrono&gt; 为我完成这项工作,方法是将答案存储在duration&lt;double, days&gt; 中。每当您发现自己在转换单位时,最好让&lt;chrono&gt; 为您完成。它可能不会更快。但绝对不会慢,也不会出错。

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

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';

在这种情况下,让&lt;chrono&gt; 为我们进行转换,可以使代码充分简化,从而算法本身可以进行代数简化,从而产生更清洁、更高效的代码,可证明与OP的问题。

【讨论】:

  • 那个名字好眼熟,就像我刚从github上得到的库上的同名:)说说支持的库!
  • 太棒了。这显着减少了代码行数,更重要的是,降低了复杂性。
猜你喜欢
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 2017-06-24
  • 2020-02-19
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多