- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
如何将chrono::system_clock::time_point值加数月?
谢谢!
最佳答案
概述
这是一个非常有趣的问题,有几个答案。 “正确”的答案是您必须针对特定应用程序决定的。
使用月份,您可以选择按时间顺序进行计算或进行日历计算。按时间顺序的计算处理时间点和持续时间的常规单位,例如小时,分钟和秒。日历计算处理不规则的日历,该日历主要用来给日子起令人难忘的名字。
年表计算
如果问题是关于 future 几个月的物理过程,那么物理学并不关心不同的月份有不同的长度,因此按时间顺序计算就足够了:
std::chrono::duration
,它的长度正好是一个公历月的长度。最简单的方法是定义一系列以
days
开头的持续时间:
days
是24小时:
using days = std::chrono::duration
<int, std::ratio_multiply<std::ratio<24>, std::chrono::hours::period>>;
years
是365.2425
days
或146097/400
days
:
using years = std::chrono::duration
<int, std::ratio_multiply<std::ratio<146097, 400>, days::period>>;
months
是
years
的1/12:
using months = std::chrono::duration
<int, std::ratio_divide<years::period, std::ratio<12>>>;
auto t = system_clock::now() + months{8};
system_clock::time_point
转换为日历。 system_clock::time_point
。 #include "date.h"
int
main()
{
using namespace date;
using namespace std::chrono;
// Get the current time
auto now = system_clock::now();
// Get a days-precision chrono::time_point
auto sd = floor<days>(now);
// Record the time of day
auto time_of_day = now - sd;
// Convert to a y/m/d calendar data structure
year_month_day ymd = sd;
// Add the months
ymd += months{8};
// Add some policy for overflowing the day-of-month if desired
if (!ymd.ok())
ymd = ymd.year()/ymd.month()/last;
// Convert back to system_clock::time_point
system_clock::time_point later = sys_days{ymd} + time_of_day;
}
now + months{8}
进行比较并得到:
now is 2017-03-25 15:17:14.467080
later is 2017-11-25 15:17:14.467080 // calendrical computation
now + months{8} is 2017-11-24 03:10:02.467080 // chronological computation
Side note:
system_clock
is not specified to be UTC, but the de facto standard is that it is Unix Time which is a very close approximation to UTC.
system_clock::time_point
(UTC):
#include "tz.h"
int
main()
{
using namespace date;
using namespace std::chrono;
// Get the current local time
auto lt = make_zoned(current_zone(), system_clock::now());
// Get a days-precision chrono::time_point
auto ld = floor<days>(lt.get_local_time());
// Record the local time of day
auto time_of_day = lt.get_local_time() - ld;
// Convert to a y/m/d calendar data structure
year_month_day ymd{ld};
// Add the months
ymd += months{8};
// Add some policy for overflowing the day-of-month if desired
if (!ymd.ok())
ymd = ymd.year()/ymd.month()/last;
// Convert back to local time
lt = local_days{ymd} + time_of_day;
// Convert back to system_clock::time_point
auto later = lt.get_sys_time();
}
now is 2017-03-25 15:17:14.467080
later is 2017-11-25 15:17:14.467080 // calendrical: UTC
later is 2017-11-25 16:17:14.467080 // calendrical: America/New_York
now + months{8} is 2017-11-24 03:10:02.467080 // chronological computation
current_zone()
来获取当前位置,但是我也可以使用特定的时区(例如
"Asia/Tokyo"
)。
std::chrono::months
由<chrono>
提供,因此您不必自己进行计算。 #include "date.h"
并改为使用#include <chrono>
,然后删除using namespace date
,一切将正常进行。 #include "tz.h"
并改为使用#include <chrono>
,删除using namespace date
,然后将make_zoned
替换为zoned_time
,您可以开始使用。 关于c++ - C++向chrono::system_clock::time_point添加月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43010362/
boost::chrono::steady_clock::time_point 之间有什么区别?和 boost::chrono::time_point ,为什么不能相互转换? 这似乎非常多余。 最佳答
有没有什么优雅的方法可以将 boost chrono time_point 转换为标准库中的等价物? 最佳答案 恐怕你不能保证转换,除非你先验地接受 boost::chrono::steady_clo
我有一个变量存储为 long 来自 std::chrono::system_clock::time_point.time_since_epoch().count() 的值。我现在想从 long 变量中
如何将计时持续时间转换为时间点,该时间点晚于具有给定持续时间的时钟纪元?我试图在计时时钟中找到纪元时间,但没有成功。 最佳答案 来自 20.12.6 [time.point] 的概要: templat
我有一个用户提供的持续时间 const int period_seconds = cfg.period_seconds; 我想floor一个time_point到那个duration的粒度。 我看到
我有一个用户提供的持续时间 const int period_seconds = cfg.period_seconds; 我想floor一个time_point到那个duration的粒度。 我看到
给定两个分割值,自纪元以来的秒数和 µs,以下哪个更可取? auto timestamp = system_clock::time_point(seconds(time_seconds) + micr
我将 time_point 设置为下一个完整的五分钟,这很容易: template using minutes = std::chrono::duration, std::chrono::m
简单的问题,如何使用尽可能少的代码将 std::chrono::time_point 正确转换为 std::string? 注意:我不想将 cout 与 put_time() 一起使用。接受 C++1
我正在开发一个类 Timer它的一些成员属于 high_resolution_clock::time_point 类型其中 time_point定义为 typedef chrono::time_poi
我发现了以下问题。我有一个带时间的字符串(从我使用反向操作输出的文件中获取)。但是,当将其转换为 time_point 并将其输出到终端时,我发现相差一小时。我一直无法找出原因。有什么想法吗? #in
我不明白为什么这段代码会被 g++ 4.7.2 卡住: #include main () { std::chrono::system_clock::time_point t1, t2 ;
如果我有一个 std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now(); 变量,我如何定义另一个 time
我正在转换 std::chrono::time_point使用 的时间戳 std::chrono::duration_cast( getTimestamp().time_since_epoch()
如何仅更改现有 std::chrono::system_clock::time_point 的小时? 例如,假设我想实现这个功能: void set_hour(std::chrono::system_
如何仅更改现有 std::chrono::system_clock::time_point 的小时? 例如,假设我想实现这个功能: void set_hour(std::chrono::system_
如何从 std::chrono::system_clock::time_point 设置平台(windows、linux、android)的系统时间。 最佳答案 您不能使用 std::chrono 工
我在 12 天前发布了关于将 std::chrono::time_point 转换为字符串的问题并解决了问题。我想对你说声谢谢。 我使用以下代码解决了我的问题: char no[15]; string
我需要在 C++ 中获取当前日期和时间。我可以使用 chrono 来获取 系统时间 但我还需要将其作为字符串保存在 json 文件中。此外,我尝试过的计时时间格式如下: auto time = std
我对这些库还是很陌生,我可以在 std::chrono 上找到的文档对我不起作用。 我正在尝试实现一个包含时间戳的对象容器。这些对象都将按从最近到最不最近的顺序存储,我决定尝试使用 std::chro
我是一名优秀的程序员,十分优秀!