草庐IT

c++ - std::chrono:将自定义持续时间添加到 time_point

以下代码可以编译(g++4.7.2):#includetypedefstd::chrono::durationdouble_prec_seconds;typedefstd::chrono::time_pointtimepoint_t;voiddo_something(consttimepoint_t&tm){//...}intmain(intargc,char**argv){timepoint_tt0=std::chrono::system_clock::now();timepoint_tt1=t0+std::chrono::seconds(3);//timepoint_tt3=t0+

C++ time_t问题

我在使用C++(VS2008)进行日期管理时遇到了问题。根据MSDNspecifications,time_t代表:自1970年1月1日0:00UTC以来的秒数因此,我写了这段代码:#include#includetime_tGetDate(intyear,intmonth,intday,inthour,intmin,intsec){time_trawtime;structtm*timeinfo;time(&rawtime);timeinfo=gmtime(&rawtime);timeinfo->tm_year=year-1900;timeinfo->tm_mon=month-1;ti

c++ - 将日期和时间数字转换为 time_t 并指定时区

我有以下整数:inty,mon,d,h,min,s;它们的值为:2012、06、27、12、47,53分别。如果我在我的应用程序中的其他地方选择了“UTC”,我想表示“2012/06/2712:47:53UTC”的日期时间,或者如果我选择了“2012/06/2712:47:53AEST”在我的应用程序的其他地方选择了“AEST”。我想将其转换为time_t,这是我目前使用的代码:structtmtimeinfo;timeinfo.tm_year=year-1900;timeinfo.tm_mon=mon-1;timeinfo.tm_mday=day;timeinfo.tm_hour=h

c++ - 如何将分数纪元时间戳( double )转换为 std::chrono::time_point?

我有一个小数纪元时间戳,表示为double,我想将其转换为适当的std::chrono::time_point。纪元是自1/1/1970以来的常用UNIX纪元。我知道存在std::chrono::system_clock::from_time_t,但是time_t没有小数部分。使用C++11方法执行此操作的最佳方法是什么?这个问题与unixtimestamptoboost::posix_time::ptime有关,除了它要求的是C++11而不是它的Boost版本。 最佳答案 假设纪元与已知的时钟类型相同,您可以使用具有double表

c++ - 将 steady_clock::time_point 转换为 time_t

我正在使用steady_clock来保存一些消息的时间戳。出于调试目的,拥有日历(或类似的东西)很有用。对于其他时钟,有静态函数to_time_t,但在GCC(MinGW4.8.0)上,此函数不存在。现在我打印类似的东西:Timestamp:26735259098242对于时间戳,我需要一个steady_clock,所以我不能使用system_clock或其他。编辑之前的打印是从time_since_epoch().count()给出的 最佳答案 假设您需要内部计算的稳定行为,而不是显示,这里有一个函数可用于转换为time_t以供显

c++ - time() 返回的 time_t 是否特定于区域?

我刚接触并对time_t有疑问和time().我阅读了如下记录的函数time():time_ttime(time_t*timer);GetcurrenttimeGetthecurrentcalendartimeasatime_tobject.Thefunctionreturnsthisvalue,andiftheargumentisnotanullpointer,thevalueisalsosettotheobjectpointedbytimer.文档没有讨论时区。因此,对于以下C++代码:time_tt=time(NULL);如果两台机器,一台在美国,一台在英国,都执行函数调用tim

c++ - 将 struct tm 转换为 time_t

我有以下代码:structtmtime;strptime("27052010","%d%m%Y",&time);cout输出是:sec:1474116832min:32767hour:4238231day:27month:5year:110sec:52min:0hour:6day:2month:9year:640time:18008625652(Fri,02Sep254004:00:52GMT)我的问题是为什么mktime()改变了time的值,为什么转换后的time_t不等于我输入的日期。我希望输出是自1970年以来以秒数表示的日期(27.05.2010=1330905600)。提前

c++ - 获取 POSIX 纪元作为 system_clock::time_point

我知道std::chrono::system_clock::time_point的默认值是时钟的纪元,但我在C++11标准中找不到任何规定system_clock的纪元与POSIX纪元(1970-01-01T00:00:00Z)相同。在Linux和Windows上假设是这种情况是否安全?或者使用std::chrono::system_clock::from_time_t(0)会更聪明吗? 最佳答案 标准离开std::chrono::system_clock::time_point的纪元未指定。std::chrono::system_

c++ - 以秒为单位获取 boost::posix_time::time_duration

我正在使用boost::posix_time::ptime来测量我的模拟运行时间和其他东西。假设boost::posix_time::ptimestart,stop;boost::posix_time::time_durationdiff;start=boost::posix_time::microsec_clock::local_time();sleep(5);stop=boost::posix_time::microsec_clock::local_time();diff=stop-stop;现在std::cout以hh:mm:ss.ssssss格式返回时间,我也希望以ss.sss

c++ - Clang 问题 : implicit type conversion at construction time

概要我正在努力使C++11代码与Clang兼容,并遇到了GCC>=4.6接受代码而Clang>=3.1不接受的情况。Clang认为候选构造函数不可行。详情这里是一个精简的例子来说明这个问题:#includetemplatestructT;templatestructT{typedefTsuper;constexprT(){}templateT(Args&&...){}};templatestructT:T{typedefTsuper;Headhead;T(Headarg):super(),head(std::move(arg)){}};structvoid_type{constexpr