将持续时间添加到时间点无效

时间:2017-06-17 07:24:25

标签: c++ c++14 chrono libc++

我尝试解决的问题由daymonthyear以及days_to_add提供日期,并将days_to_add添加到给定日期。

我能够将日期转换为std::chrono::time_point<system_clock, ...>。现在我想给它添加几天:

template <typename Clock, typename Duration>
auto add_days(const std::chrono::time_point<Clock, Duration>& timepoint,
                                                  int days_to_add)
{
    constexpr unsigned int seconds_in_day = 60 * 60 * 24;
    //                                      mm   hh   dd

    return timepoint + Duration(seconds_in_day * days_to_add);
}

但结果没有变化timepoint。以下是重现的完整程序:

#include <iostream>
#include <chrono>
#include <iomanip>

template <typename Clock, typename Duration>
std::ostream& operator<<(std::ostream& os, const std::chrono::time_point<Clock, Duration>& timep)
{
    auto converted_timep = Clock::to_time_t(timep);
    os << std::put_time(std::localtime(&converted_timep), "%Y %b %d %H:%M:%S");
    return os;
}

template <typename Clock, typename Duration>
auto add_days(const std::chrono::time_point<Clock, Duration>& timepoint,
                                                  int days_to_add)
{
    constexpr unsigned int seconds_in_day = 60 * 60 * 24;
    //                                      mm   hh   dd

    return timepoint + Duration(seconds_in_day * days_to_add);
}

int main()
{
    auto today = std::chrono::system_clock::now();
    auto tomorrow = add_days(today, 1);
    std::cout << tomorrow;

    return 0;
}

gccclang的Wandbox链接。

2017 Jun 17 16:23:18
2017 Jun 17 16:23:18

1 个答案:

答案 0 :(得分:3)

问题是<?php if(isset($_POST['importSubmit'])){ $csvMimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain'); if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'],$csvMimes)){ if(is_uploaded_file($_FILES['file']['tmp_name'])){ $csvFile = fopen($_FILES['file']['tmp_name'], 'r'); fgetcsv($csvFile); while(($line = fgetcsv($csvFile)) !== FALSE){ require_once('../../mysqlConnector/mysql_connect.php'); $query="UPDATE communitysurvey SET stationID = '".$line[0]."',userID= '{$_SESSION['username']}', question1 = '".$line[1]."', question2 = '".$line[2]."', question3 = '".$line[3]."', question4 = '".$line[4]."', question5 = '".$line[5]."', question6 = '".$line[6]."', question7 = '".$line[7]."', question8 = '".$line[8]."', question9 = '".$line[9]."',question10 = '".$line[10]."', question11 = '".$line[11]."', question12 = '".$line[12]."', question13 = '".$line[13]."', question14 = '".$line[14]."',question15 = '".$line[15]."', question16 = '".$line[16]."', question17 = '".$line[17]."', question18 = '".$line[18]."', question19 = '".$line[19]."',question20 = '".$line[20]."', question21 = '".$line[21]."',question22 = '".$line[22]."', comments1 = '".$line[23]."', comments2 = '".$line[24]."'"; $result=mysqli_query($dbc,$query); } fclose($csvFile); } } } ?> 不会以秒为单位存储时间,也不会以毫秒为单位。你觉得微秒?不。它以纳秒为单位存储时间(至少在gcc上,你似乎也在使用它)!实际上,时间是实现定义的,所以最好使用像这里的最后一个便携式解决方案。

你只认为时间点没有改变,但实际上,它是,但是时间尺度小于毫秒。

如果您计算一天中的纳秒数,您将得到正确答案:

std::system_clock

但是只有从template <typename Clock, typename Duration> auto add_days(const std::chrono::time_point<Clock, Duration>& timepoint, int days_to_add) { constexpr auto nano_seconds_in_day = 1'000'000'000ul * 60 * 60 * 24; // (ns in s) mm hh dd return timepoint + Duration(nano_seconds_in_day * days_to_add); } 传递时间点才会有效。其他时钟可以具有不同的std::system_clock,因此需要添加不同的值。您应该使用此版本(这也更清晰):

duration