C++14 中一年中小数日的计算
Fractional day of the year computation in C++14
我使用 Howard Hinnants date.h
库编写了以下代码,以计算当前时间一年中的小数日。我想知道是否有更短的方法来做到这一点,因为我的代码感觉像是 std::chrono
和 date
调用的过度杀伤力。我可以直接计算自年初以来的小数天数(以微秒为精度)并避免我的两步法吗?
#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。出于演示目的,我修改了两个答案以替换为 "fake now" 以便我们可以比较苹果与苹果:
using namespace std::chrono_literals;
auto now = date::sys_days{date::March/27/2019} + 0h + 32min + 22s + 123456us;
(大约在我写这篇文章的时候)
代码给出:
Fractional day of the year: 85.0225
Matteo's 代码给出:
Fractional day of the year: 85.139978280740735
它们很接近,但还不够接近,不能认为两者都是正确的。
Matteo's 代码适用于 "average years":
auto this_year = date::floor<date::years>(now);
date::years
的长度是 365.2425 天,如果您对 400 年期间的所有民用年进行平均,则完全正确。使用平均年长可能非常有用,尤其是在处理不关心人工日历的系统时(例如物理学或生物学)。
我猜测由于 代码的编写方式,他更喜欢更精确地引用 这个 特定年份的结果。因此下面给出的代码是 的算法,结果完全相同,只是效率和简洁性稍高。
// 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
的基于双精度的表示,我将通过将答案存储在 duration<double, days>
中让 <chrono>
为我完成这项工作].任何时候您发现自己需要转换单位时,最好让 <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问题中的原始算法。
我使用 Howard Hinnants date.h
库编写了以下代码,以计算当前时间一年中的小数日。我想知道是否有更短的方法来做到这一点,因为我的代码感觉像是 std::chrono
和 date
调用的过度杀伤力。我可以直接计算自年初以来的小数天数(以微秒为精度)并避免我的两步法吗?
#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。出于演示目的,我修改了两个答案以替换为 "fake now" 以便我们可以比较苹果与苹果:
using namespace std::chrono_literals;
auto now = date::sys_days{date::March/27/2019} + 0h + 32min + 22s + 123456us;
(大约在我写这篇文章的时候)
Fractional day of the year: 85.0225
Matteo's 代码给出:
Fractional day of the year: 85.139978280740735
它们很接近,但还不够接近,不能认为两者都是正确的。
Matteo's 代码适用于 "average years":
auto this_year = date::floor<date::years>(now);
date::years
的长度是 365.2425 天,如果您对 400 年期间的所有民用年进行平均,则完全正确。使用平均年长可能非常有用,尤其是在处理不关心人工日历的系统时(例如物理学或生物学)。
我猜测由于
// 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
的基于双精度的表示,我将通过将答案存储在 duration<double, days>
中让 <chrono>
为我完成这项工作].任何时候您发现自己需要转换单位时,最好让 <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问题中的原始算法。