c++ 获取自 01/01/0001 以来的毫秒数 00:00:00
c++ get milliseconds since 01/01/0001 00:00:00
我需要一个特殊格式的时间戳用于 API 调用:
Dates are converted to UTC milliseconds elapsed since 12:00:00 midnight, January 1, 0001.
我的第一个假设是使用:
auto now = std::chrono::system_clock::now();
std::cout << "millisceconds since epoch: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(
now.time_since_epoch()).count()
当然输出的是从UNIX纪元开始的时间间隔
Thu Jan 1 00:00:00 1970
所以对于 now = "Wed Dec 12 13:30:00 2018"
它 returns 1544617800000
毫秒。
如何获取自 0001 年 1 月 1 日午夜 12:00:00 以来经过的毫秒数?
上下文 OSISoft API
OSISoft API 指定日期范围的文档很奇怪
Numeric Range Queries
The previous examples were Range Queries against string fields. Numeric values > can also be searched for with Range Queries.
The only fields that are indexed as numeric fields are the CreationDate and ChangeDate fields for the respective PI Point attributes. To index these fields > add them to the list of PI Point Attributes. This configuration may be viewed > or modified on the Settings page.
These date time values are indexed as numeric values via a conversion: Dates are converted to UTC milliseconds elapsed since 12:00:00 midnight, January 1, 0001.
In the following example query is a request for last changed date equal to or > greater than February 26th, 22:16:50.000 (This is Universal Time). This DateTime, following the aforementioned conversion, would be represented as numeric value: 63655280210000. Therefore the query submitted is:
https://MyServer/piwebapi/search/query?q=changedate:[63655280210000 TO *]
从这个文档中,我问了这个问题,关于如何获得自 12:00:00 0001 年 1 月 1 日午夜以来经过的毫秒数。
我还将问题链接到 PISquare
没有定义的计算方式:
UTC milliseconds elapsed since 12:00:00 midnight, January 1, 0001.
根据示例判断,他们使用与 https://www.epochconverter.com/seconds-days-since-y0 相同的算法。要获得相同的结果,您只需将 719162 天添加到 unix 纪元即可:
auto now = std::chrono::system_clock::now();
std::cout << "millisceconds since epoch: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(
now.time_since_epoch() + std::chrono::hours(24 * 719162)).count()
注意 c++20 引入了 std::chrono::days
,您可以使用它来代替 24 小时。
根据系统时钟的分辨率,您可能需要在添加偏移量之前转换为毫秒以避免溢出(719162 天超过 2^64 纳秒)。
这很容易使用 Howard Hinnant's date/time library:
#include "date/date.h"
#include <iostream>
std::chrono::milliseconds
convert(std::chrono::system_clock::time_point tp)
{
using namespace date;
using namespace std::chrono;
return (floor<milliseconds>(tp) +
(sys_days{1970_y/January/1} - sys_days{1_y/January/1})).time_since_epoch();
}
int
main()
{
using namespace date;
using namespace std::chrono;
std::cout << convert(system_clock::now()) << '\n';
}
convert
只是将两个epoch的差值加到system_clock::time_point
,截断到milliseconds
精度,提取duration
到return就可以了作为 milliseconds
.
这个程序对我来说只是输出:
63680221359193ms
milliseconds
上的范围足以处理此计算,但 system_clock::time_point
上的范围可能不够大。因此,重要的是像上面的代码一样立即截断到 milliseconds
以避免溢出。
我需要一个特殊格式的时间戳用于 API 调用:
Dates are converted to UTC milliseconds elapsed since 12:00:00 midnight, January 1, 0001.
我的第一个假设是使用:
auto now = std::chrono::system_clock::now();
std::cout << "millisceconds since epoch: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(
now.time_since_epoch()).count()
当然输出的是从UNIX纪元开始的时间间隔
Thu Jan 1 00:00:00 1970
所以对于 now = "Wed Dec 12 13:30:00 2018"
它 returns 1544617800000
毫秒。
如何获取自 0001 年 1 月 1 日午夜 12:00:00 以来经过的毫秒数?
上下文 OSISoft API
OSISoft API 指定日期范围的文档很奇怪
Numeric Range Queries
The previous examples were Range Queries against string fields. Numeric values > can also be searched for with Range Queries.
The only fields that are indexed as numeric fields are the CreationDate and ChangeDate fields for the respective PI Point attributes. To index these fields > add them to the list of PI Point Attributes. This configuration may be viewed > or modified on the Settings page.
These date time values are indexed as numeric values via a conversion: Dates are converted to UTC milliseconds elapsed since 12:00:00 midnight, January 1, 0001.
In the following example query is a request for last changed date equal to or > greater than February 26th, 22:16:50.000 (This is Universal Time). This DateTime, following the aforementioned conversion, would be represented as numeric value: 63655280210000. Therefore the query submitted is:
https://MyServer/piwebapi/search/query?q=changedate:[63655280210000 TO *]
从这个文档中,我问了这个问题,关于如何获得自 12:00:00 0001 年 1 月 1 日午夜以来经过的毫秒数。
我还将问题链接到 PISquare
没有定义的计算方式:
UTC milliseconds elapsed since 12:00:00 midnight, January 1, 0001.
根据示例判断,他们使用与 https://www.epochconverter.com/seconds-days-since-y0 相同的算法。要获得相同的结果,您只需将 719162 天添加到 unix 纪元即可:
auto now = std::chrono::system_clock::now();
std::cout << "millisceconds since epoch: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(
now.time_since_epoch() + std::chrono::hours(24 * 719162)).count()
注意 c++20 引入了 std::chrono::days
,您可以使用它来代替 24 小时。
根据系统时钟的分辨率,您可能需要在添加偏移量之前转换为毫秒以避免溢出(719162 天超过 2^64 纳秒)。
这很容易使用 Howard Hinnant's date/time library:
#include "date/date.h"
#include <iostream>
std::chrono::milliseconds
convert(std::chrono::system_clock::time_point tp)
{
using namespace date;
using namespace std::chrono;
return (floor<milliseconds>(tp) +
(sys_days{1970_y/January/1} - sys_days{1_y/January/1})).time_since_epoch();
}
int
main()
{
using namespace date;
using namespace std::chrono;
std::cout << convert(system_clock::now()) << '\n';
}
convert
只是将两个epoch的差值加到system_clock::time_point
,截断到milliseconds
精度,提取duration
到return就可以了作为 milliseconds
.
这个程序对我来说只是输出:
63680221359193ms
milliseconds
上的范围足以处理此计算,但 system_clock::time_point
上的范围可能不够大。因此,重要的是像上面的代码一样立即截断到 milliseconds
以避免溢出。