Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 自大纪元以来的时间是-1_C++_Epoch_Ctime - Fatal编程技术网

C++ 自大纪元以来的时间是-1

C++ 自大纪元以来的时间是-1,c++,epoch,ctime,C++,Epoch,Ctime,我正在尝试获取自epoch以来经过的秒数。守则: long parseTime(string time) { cout << "Time entered = " << time << endl; long timeSinceEpoch; //takes in time in string format - date + time and returns seconds from epoch. /* Steps: * 1. Remov

我正在尝试获取自epoch以来经过的秒数。守则:

long parseTime(string time) {

  cout << "Time entered = " << time << endl;

  long timeSinceEpoch;

  //takes in time in string format - date + time and returns seconds from epoch.
  /* Steps:
   * 1. Remove trailing and leading spaces.
   * 2. Check format of date.
   * 3. Convert to epoch.
   */

  //remove trailing and leading spaces.
  /*
  unsigned int leading = 0, trailing = 0;
  string whitespace = " \t";

  leading = time.find_first_not_of(whitespace);
  trailing = time.find_last_not_of(whitespace);

  string newTime = time.substr(leading, (trailing - leading + 1));
  cout << "Old time = " << time << " new time = " << newTime << endl;
  */
  string newTime = time;

  struct tm t;

  if(newTime.find("/") != string::npos) {
    //format of date is mm/dd/yyyy. followed by clock in hh:mm (24 hour clock).
    cout << "Time format contains slashes." << endl;
    if(strptime(newTime.c_str(), "%m/%e/%Y %H:%M", &t) == NULL) {
      cout << "Error. Check string for formatting." << endl;
    }
  } else if(newTime.find("-") != string::npos) {
    //format of date is yyyy-mm-dd hh:mm:ss (hh in 24 hour clock format).
    if(strptime(newTime.c_str(), "%Y-%m-%e %H:%M:%S", &t) == NULL) {
     cout << "Error. Check string for formatting of new date." << endl;
    }
  }

  if(t.tm_isdst) {
    t.tm_isdst = 0;
  }

  timeSinceEpoch = mktime(&t);
  cout << "Time since epoch = " << timeSinceEpoch << endl;

  return timeSinceEpoch;
}

如果您注意到,
t
中的
tm_sec
为1628993312,而
timesincepoch
为-1
tm_sec
也在
long
的范围内,这是
timesincepoch
的数据类型。欢迎提供任何关于为什么以及如何解决此问题的想法。

关键是在代码中,tm_sec是一个未初始化的值。它应该是一个介于0和59之间的值。因此,将这行代码添加到您的第一个if分支

if(strptime(newTime.c_str(), "%m/%e/%Y %H:%M", &t) == NULL) {
  cout << "Error. Check string for formatting." << endl;
}
t.tm_sec = 0; // initialise seconds field
if(strtime(newTime.c_str(),%m/%e/%Y%H:%m,&t)==NULL){

关键是在代码中,tm_sec是一个未初始化的值。它应该是一个介于0和59之间的值。因此,将这行代码添加到第一个if分支中

if(strptime(newTime.c_str(), "%m/%e/%Y %H:%M", &t) == NULL) {
  cout << "Error. Check string for formatting." << endl;
}
t.tm_sec = 0; // initialise seconds field
if(strtime(newTime.c_str(),%m/%e/%Y%H:%m,&t)==NULL){
约翰是对的。
strtime
不初始化
tm
,只触摸明确指定的字段(), 因此,
t.tm_sec
未初始化

为了防止这种错误,可以通过初始化声明变量

struct tm t = { 0 }; // zero filled
约翰是对的。
strtime
不初始化
tm
,只触摸明确指定的字段(), 因此,
t.tm_sec
未初始化

为了防止这种错误,可以通过初始化声明变量

struct tm t = { 0 }; // zero filled

tm_-sec
在使用
strtime
初始化后设置为0?这是如何工作的?此外,调试器输出显示
tm_-sec
(从纪元开始的时间)在调用mktime后设置为-1之前,初始化为1628993312。我的观点是,它不是由strtime初始化的,您的格式字符串中没有%s。我不确定我是否理解了这一点。
%s
用于秒,输入到函数的时间字符串没有秒,只有小时:分钟。这很好,但因为这些事实表明,当您在日期中使用斜杠时,tm结构中的tm_sec字段没有设置。但是mktime需要设置tm结构中的所有字段。因此,在这种情况下,您必须手动设置它。
tm_sec
在使用
strptime
初始化后设置为0?这将如何工作?调试器输出显示t
tm_-sec
(从纪元开始的时间)在调用mktime后设置为-1之前,初始化为1628993312。我的观点是,它不是由strtime初始化的,您的格式字符串中没有%s。我不确定我是否理解了这一点。
%s
用于秒,输入到函数的时间字符串没有秒,只有小时:分钟。这很好,但因为这些事实表明,在日期中有斜杠时,tm结构中的tm_sec字段没有设置。但是mktime需要设置tm结构中的所有字段。因此,在这种情况下,您必须手动设置它。