c++ - 如何在 C++ 中获得一个月的第三个星期五?
问题描述
我认为日期处理有点复杂。
所以,我想知道是否有任何优雅的算法可以让我在我的情况下获得任何日期,比如 2020 年 9 月的第三个星期五的日期?
让我们指定两种我想要的方法:
int get_date(int month, int weekday, int no) { // for example: get_date(202009, 3, 3)
// should return the third wednesday in sep 2020, it's 20200917
}
int date_sub(int date1, int date2) { // for example:
// date_sub(20200928, 20200925) should return 3
}
我知道 boost 有一些不错的东西,但我认为它可能太重了,有没有更好的选择?
解决方案
使用c++20 日期支持或Howard Hinnants 实现,这是微不足道的:
#include <chrono>
#include <iostream>
using namespace std::chrono;
int main()
{
constexpr year_month_day date {year(2020)/September/Friday[3]};
std::cout << date;
}
或者
#include "date.h"
#include <iostream>
using namespace date;
int main()
{
constexpr year_month_day date {year(2020)/September/Friday[3]};
std::cout << date;
}
推荐阅读
- r - 表 R Markdown qwraps2 和 knitr::kable。缺少列
- macos - aws2 命令在写入 .command 文件并使用 crontab 计划时不起作用
- android - android ANR BillingClient.queryPurchases
- jquery - 重力形式的 Wordpress 水平字段
- c# - 如何修复 C# 中 XPathDocument 使用中的 XXE 漏洞?
- python - 我想将 db 句柄从我的 perl 脚本传递给我的 python 脚本
- reactjs - 在 react 中将字符串转换为 jsx 字符串 | 如何在反应中将 Unicode 转义序列转换为 Unicode 字符
- c++ - 有没有办法在 jsoncpp 中使用变量作为键?
- angular - Angular 8 侧面导航以加载具有不同数据的相同组件
- python - Python - 关闭所有活动文档而不保存