简体   繁体   English

将std :: chrono :: system_clock :: time_point :: min()转换为字符串时,无效的空指针错误

[英]Invalid null pointer error when converting std::chrono::system_clock::time_point::min() to string

I am following an example in Nicolai M. Josuttis' "The C++ Standard Library (Second Edition)", page 152-153, which details an example to print the epoch, current time, minimum and maximum times of the std::chrono::system_clock introduced in C++11. 我正在Nicolai M. Josuttis的“ C ++标准库(第二版)”中,第152-153页中的示例,该示例详细说明了打印std::chrono::system_clock的时代,当前时间,最小和最大时间的示例std::chrono::system_clock是C ++ 11中引入的。

I am using Microsoft Visual Studio 2012, and get an assertion triggered in <xstring> , due to an invalid null pointer. 我正在使用Microsoft Visual Studio 2012,并且由于无效的空指针而在<xstring>触发了一个断言。 This occurs on the line std::string ts = std::ctime( &t ) in the code below after setting tp = std::chrono::system_clock::time_point::min(); 在设置tp = std::chrono::system_clock::time_point::min();之后,在以下代码的std::string ts = std::ctime( &t )行中发生这种情况tp = std::chrono::system_clock::time_point::min();

#include <chrono>
#include <ctime>
#include <string>
#include <iostream>

std::string asString( const std::chrono::system_clock::time_point& tp )
{
    std::time_t t = std::chrono::system_clock::to_time_t( tp );
    std::string ts = std::ctime( &t );
    ts.resize( ts.size()-1 );
    return ts;
}

int main()
{
    std::chrono::system_clock::time_point tp;
    std::cout << "epoch: " << asString(tp) << std::endl;

    tp = std::chrono::system_clock::now();
    std::cout << "now: " << asString(tp) << std::endl;

    tp = std::chrono::system_clock::time_point::min();
    std::cout << "min: " << asString(tp) << std::endl;

    tp = std::chrono::system_clock::time_point::max();
    std::cout << "max: " << asString(tp) << std::endl;

    return 0;
}

Is this due to an implementation error by Dinkumware in the <chrono> library, or just a typo/mistake in the book? 这是由于Dinkumware在<chrono>库中的实现错误,还是书中的错字/错误? I have gone over the code given in the book again and again to see if I have copied it out incorrectly, but this does not appear to be the case. 我一次又一次地检查了书中给出的代码,以查看是否将其错误地复制了出来,但事实并非如此。 I'd be very grateful for any insights given. 对于提供的任何见解,我将不胜感激。

It looks like std::ctime returns NULL, which indicates an incorrect t value. 看起来std::ctime返回NULL,表示不正确的t值。 Probably because the call to asString uses a value of time_point that cannot be represented in time_t type. 可能是因为对asString的调用使用了无法以time_t类型表示的time_point值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 二进制&#39;&gt; =&#39;:&#39;std :: chrono :: system_clock :: time_point&#39; - binary '>=': 'std::chrono::system_clock::time_point' boost :: serialize和std :: chrono :: system_clock :: time_point - boost::serialize and std::chrono::system_clock::time_point C ++将字符串时间戳转换为std :: chrono :: system_clock :: time_point - C++ Convert a string timestamp to std::chrono::system_clock::time_point 如何正确地将字符串转换为 std::chrono::system_clock::time_point? - How to properly convert string to std::chrono::system_clock::time_point? 是 std::atomic <std::optional<std::chrono::time_point<std::chrono::system_clock> &gt;&gt; 有效/安全? </std::optional<std::chrono::time_point<std::chrono::system_clock> - Is std::atomic<std::optional<std::chrono::time_point<std::chrono::system_clock>>> valid/safe? 从持续时间构造 std::chrono::system_clock::time_point - Constructing std::chrono::system_clock::time_point from a duration 将std :: chrono :: system_clock :: time_point转换为struct timeval并返回 - Convert std::chrono::system_clock::time_point to struct timeval and back 将 C# 日期时间转换为 C++ std::chrono::system_clock::time_point - Convert C# DateTime to C++ std::chrono::system_clock::time_point 如何从std :: chrono :: system_clock :: time_point.time_since_epoch()。count()获取std :: chrono :: system_clock :: time_point? - How to get std::chrono::system_clock::time_point from std::chrono::system_clock::time_point.time_since_epoch().count()? 如何将YYYY / MM / DD HH:MM:SS转换为std :: chrono :: system_clock :: time_point? - How do I convert YYYY/MM/DD HH:MM:SS to std::chrono::system_clock::time_point?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM