在给定时间点的情况下,如何将纪元作为双倍的秒数?

时间:2022-12-15 15:59:41

I have a function from a library i'm using that requires a double as a parameter. It needs to be passed an offset of type nanoseconds plus the sytem_clock::now(). I have this code so far:

我有一个我正在使用的库中的函数,需要一个double作为参数。它需要传递一个纳秒类型的偏移加上sytem_clock :: now()。到目前为止我有这个代码:

system_clock::time_point now = std::chrono::system_clock::now(); 
    auto timepointoffset = (now + desiredOffset);

How can I make this a double?

我怎么能把它做成双倍的?

Edit: So I need to add that the problem is that I need to do it without a risk of losing data. I have this code:

编辑:所以我需要补充一点,问题是我需要这样做而不会有丢失数据的风险。我有这个代码:

    system_clock::time_point now = std::chrono::system_clock::now(); 
    auto timepointoffset =  std::chrono::time_point_cast<std::chrono::nanoseconds>(now + desiredOffset);
    double value = timepointoffset.time_since_epoch().count();

The problem is that the compiler says that there is a possible loss of data.

问题是编译器说可能会丢失数据。

5 个解决方案

#1


2  

The time_point template has a member function time_point::time_since_epoch that returns the time since epoch as a duration object. duration is a template type and the exact duration type returned by the function is the same as the Duration template parameter of the time_point. The type of time_point returned by system_clock::now() on the other hand is implementation defined.

time_point模板具有成员函数time_point :: time_since_epoch,该函数返回自纪元以来的时间作为持续时间对象。 duration是模板类型,函数返回的确切持续时间类型与time_point的Duration模板参数相同。另一方面,system_clock :: now()返回的time_point类型是实现定义的。

You want the time in a specific representation. To achieve that, you must have the duration of the desired type. In this case, you want std::duration<double, std::ratio<1>>. To get the duration returned by time_since_epoch in correct type, you can cast it using std::chrono::duration_cast. When you have the duration of the correct representation, you can use the count member function of duration to get the value in the desired representation.

您想要特定表示的时间。要实现这一点,您必须具有所需类型的持续时间。在这种情况下,您需要std :: duration >。要获得time_since_epoch以正确类型返回的持续时间,可以使用std :: chrono :: duration_cast来强制转换它。如果具有正确表示的持续时间,则可以使用持续时间的计数成员函数来获取所需表示形式的值。 ,std>

#2


4  

This program:

#include <chrono>
#include <cmath>
#include <iostream>

int
main()
{
    using namespace std::chrono;
    auto tp = system_clock::now() + 0ns;
    double t1 = tp.time_since_epoch().count();
    double t2 = std::nextafter(t1, INFINITY);
    std::cout << t2-t1 << '\n';
}

currently outputs:

256

This means that currently a double can not exactly represent the number of nanoseconds since 1970-01-01 00:00:00 UTC. A double can represent a unit equal to 256ns (about a quarter of a microsecond).

这意味着当前双倍不能准确表示自1970-01-01 00:00:00 UTC以来的纳秒数。双倍可以表示等于256ns(大约四分之一微秒)的单位。

This has been true since 2006-07-14 23:58:24.606847232. Before then (for a while), a double could represent with a precision of 128ns.

自2006-07-14 23:58:24.606847232以来一直如此。在此之前(有一段时间),双精度可以表示128ns的精度。

At 2043-01-25 23:56:49.213694464 UTC a double will start being able to represent a precision of only 512ns.

在2043-01-25 23:56:49.213694464 UTC,double将开始能够表示仅512ns的精度。

A double can store a system_clock::time_point with nanosecond precision only in the approximate range of 1969-09-18 18:00:01 to 1970-04-15 05:59:59.

double只能在1969-09-18 18:00:01到1970-04-15 05:59:59的近似范围内存储具有纳秒精度的system_clock :: time_point。

This analysis of course assumes an IEEE 64 bit double.

该分析当然假设IEEE 64位双。

#3


0  

You can cast timepointoffset to std::time_t and then cast it to double:

你可以将timepointoffset转换为std :: time_t,然后将其转换为double:

std::time_t offsetTimet = std::chrono::system_clock::to_time_t(timepointoffset);
double offsetDouble = static_cast<double>(offsetTimet);

Be aware that while std::time_t is normally an integer type, there is no guarantee.

请注意,虽然std :: time_t通常是整数类型,但无法保证。

#4


0  

So I need to add that the problem is that I need to do it without a risk of losing data. I have this code:

所以我需要补充一点,问题是我需要这样做而不会有丢失数据的风险。我有这个代码:

There is no way to do this in general. When you convert from an integer type to a floating point type, you will typically lose precision. Using floating point, the bits are split into a mantissa and an exponent. Since an integer has no bits allocated for an exponent, there are generally fewer bits of precision to represent the value.

一般来说,没有办法做到这一点。从整数类型转换为浮点类型时,通常会失去精度。使用浮点,这些位被分成尾数和指数。由于整数没有为指数分配位,因此通常用较少的精度位来表示该值。

For example, converting from int64_t to double. An int64_t has 1 sign bit, and 63 value bits. A double has 1 sign bit, 11 exponent bits, and 52 mantissa bits. So converting from int64_t (63) to double (52), you can lose up to 11 bits of information.

例如,从int64_t转换为double。 int64_t有1个符号位和63个值位。 double有1个符号位,11个指数位和52个尾数位。因此,从int64_t(63)转换为double(52),您最多可以丢失11位信息。

#5


0  

std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(
  std::chrono::system_clock::now().time_since_epoch()).count() << std::endl;

appears to be the correct way to get a long: http://en.cppreference.com/w/cpp/chrono/time_point/time_since_epoch which you can then convert to double.

似乎是获得漫长的正确方法:http://en.cppreference.com/w/cpp/chrono/time_point/time_since_epoch然后您可以将其转换为double。

#1


2  

The time_point template has a member function time_point::time_since_epoch that returns the time since epoch as a duration object. duration is a template type and the exact duration type returned by the function is the same as the Duration template parameter of the time_point. The type of time_point returned by system_clock::now() on the other hand is implementation defined.

time_point模板具有成员函数time_point :: time_since_epoch,该函数返回自纪元以来的时间作为持续时间对象。 duration是模板类型,函数返回的确切持续时间类型与time_point的Duration模板参数相同。另一方面,system_clock :: now()返回的time_point类型是实现定义的。

You want the time in a specific representation. To achieve that, you must have the duration of the desired type. In this case, you want std::duration<double, std::ratio<1>>. To get the duration returned by time_since_epoch in correct type, you can cast it using std::chrono::duration_cast. When you have the duration of the correct representation, you can use the count member function of duration to get the value in the desired representation.

您想要特定表示的时间。要实现这一点,您必须具有所需类型的持续时间。在这种情况下,您需要std :: duration >。要获得time_since_epoch以正确类型返回的持续时间,可以使用std :: chrono :: duration_cast来强制转换它。如果具有正确表示的持续时间,则可以使用持续时间的计数成员函数来获取所需表示形式的值。 ,std>

#2


4  

This program:

#include <chrono>
#include <cmath>
#include <iostream>

int
main()
{
    using namespace std::chrono;
    auto tp = system_clock::now() + 0ns;
    double t1 = tp.time_since_epoch().count();
    double t2 = std::nextafter(t1, INFINITY);
    std::cout << t2-t1 << '\n';
}

currently outputs:

256

This means that currently a double can not exactly represent the number of nanoseconds since 1970-01-01 00:00:00 UTC. A double can represent a unit equal to 256ns (about a quarter of a microsecond).

这意味着当前双倍不能准确表示自1970-01-01 00:00:00 UTC以来的纳秒数。双倍可以表示等于256ns(大约四分之一微秒)的单位。

This has been true since 2006-07-14 23:58:24.606847232. Before then (for a while), a double could represent with a precision of 128ns.

自2006-07-14 23:58:24.606847232以来一直如此。在此之前(有一段时间),双精度可以表示128ns的精度。

At 2043-01-25 23:56:49.213694464 UTC a double will start being able to represent a precision of only 512ns.

在2043-01-25 23:56:49.213694464 UTC,double将开始能够表示仅512ns的精度。

A double can store a system_clock::time_point with nanosecond precision only in the approximate range of 1969-09-18 18:00:01 to 1970-04-15 05:59:59.

double只能在1969-09-18 18:00:01到1970-04-15 05:59:59的近似范围内存储具有纳秒精度的system_clock :: time_point。

This analysis of course assumes an IEEE 64 bit double.

该分析当然假设IEEE 64位双。

#3


0  

You can cast timepointoffset to std::time_t and then cast it to double:

你可以将timepointoffset转换为std :: time_t,然后将其转换为double:

std::time_t offsetTimet = std::chrono::system_clock::to_time_t(timepointoffset);
double offsetDouble = static_cast<double>(offsetTimet);

Be aware that while std::time_t is normally an integer type, there is no guarantee.

请注意,虽然std :: time_t通常是整数类型,但无法保证。

#4


0  

So I need to add that the problem is that I need to do it without a risk of losing data. I have this code:

所以我需要补充一点,问题是我需要这样做而不会有丢失数据的风险。我有这个代码:

There is no way to do this in general. When you convert from an integer type to a floating point type, you will typically lose precision. Using floating point, the bits are split into a mantissa and an exponent. Since an integer has no bits allocated for an exponent, there are generally fewer bits of precision to represent the value.

一般来说,没有办法做到这一点。从整数类型转换为浮点类型时,通常会失去精度。使用浮点,这些位被分成尾数和指数。由于整数没有为指数分配位,因此通常用较少的精度位来表示该值。

For example, converting from int64_t to double. An int64_t has 1 sign bit, and 63 value bits. A double has 1 sign bit, 11 exponent bits, and 52 mantissa bits. So converting from int64_t (63) to double (52), you can lose up to 11 bits of information.

例如,从int64_t转换为double。 int64_t有1个符号位和63个值位。 double有1个符号位,11个指数位和52个尾数位。因此,从int64_t(63)转换为double(52),您最多可以丢失11位信息。

#5


0  

std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(
  std::chrono::system_clock::now().time_since_epoch()).count() << std::endl;

appears to be the correct way to get a long: http://en.cppreference.com/w/cpp/chrono/time_point/time_since_epoch which you can then convert to double.

似乎是获得漫长的正确方法:http://en.cppreference.com/w/cpp/chrono/time_point/time_since_epoch然后您可以将其转换为double。