gpt4 book ai didi

c++ - C++14 中一年中小数日的计算

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:29:42 25 4
gpt4 key购买 nike

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

最佳答案

好问题(已投票)。

我认为首先我们需要确定正确答案是什么。这是您的答案,目前唯一的其他答案是 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 的基于 double 的表示,我将通过将答案存储在 <chrono> 中让 duration<double, days> 为我完成这项工作。任何时候您发现自己要转换单位时,最好让 <chrono> 为您完成。它可能不会更快。但它绝对不会更慢,也不会出错。

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

days += now - sd;

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

更新

还有一点时间来反射(reflection)......

我注意到,使用上面的简化代码,可以更轻松地将整个算法视为一个表达式。那就是(删除 namespace 限定,以便将所有内容放在一行中):

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 中的原始算法题。

关于c++ - C++14 中一年中小数日的计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55367898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com