首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >时间tm,mktime更改结构tm

时间tm,mktime更改结构tm
EN

Stack Overflow用户
提问于 2020-03-24 17:14:58
回答 1查看 97关注 0票数 0

我试着从文件中读取一些格式的日期( dates )。日期成对,如果第二个日期等于'-‘,则将该日期注册为nullptr。以下是文件:

代码语言:javascript
复制
30-05-2009 20-02-2020
12-06-2012 04-01-2017
22-06-2012 26-04-2017
15-03-2006 15-01-2012
25-09-2002 02-02-2006
15-07-2005 -
20-03-2000 23-01-2004
12-01-2012 19-03-2017
14-08-2008 15-08-2008
27-11-2006 18-06-2007
03-10-2006 24-01-2007
20-12-2010 23-04-2018
27-01-2007 02-03-2018
16-10-2013 11-10-2015
09-07-2003 09-10-2014
31-03-2007 28-12-2013
11-01-2012 06-09-2017
15-08-2007 18-03-2011
24-12-2004 19-08-2008
23-11-2009 -
10-05-2000 11-02-2012
26-12-2005 07-04-2007
12-01-2003 04-10-2008
24-03-2004 04-01-2016
31-05-2001 21-01-2002
27-11-2001 21-03-2013
29-09-2007 21-02-2020
13-10-2004 06-04-2015
12-06-2005 07-05-2008
14-03-2001 21-07-2005
23-11-2003 16-01-2017
13-05-2003 15-07-2016
02-12-2006 17-04-2013
20-03-2004 06-08-2014
18-12-2016 10-03-2017
22-01-2000 13-02-2004
02-03-2000 18-12-2015
01-12-2004 31-03-2019
29-04-2006 19-03-2012
14-04-2007 11-03-2015
02-03-2002 13-12-2015
03-12-2001 16-01-2013
10-12-2000 16-05-2015
08-04-2000 04-04-2018
01-02-2008 30-11-2009
30-03-2006 -
08-09-2010 21-02-2017
19-02-2002 15-03-2003
17-09-2007 18-09-2010
23-01-2007 -

下面也是我的代码:

代码语言:javascript
复制
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main(int argc, char const *argv[]) {
  //variables for reading file
  string tempDate;
  struct tm *entryDate, *exitDate;

  ifstream file("dates.txt");
  string input;

  // read file
  if (file.is_open()) {
    cout << '\n';
    entryDate = new struct tm;

    while (getline(file, input)) {
      istringstream inputStream(input);

      inputStream >> get_time(entryDate, "%d-%m-%Y") >> tempDate;

      if (!tempDate.compare("-")) {
        exitDate = nullptr;
      } else {
        istringstream dateStream(tempDate);
        exitDate = new struct tm;
        dateStream >> get_time(exitDate, "%d-%m-%Y");

        // timegm changes strut tm????
        cout << "Before: " << exitDate->tm_mday << ' ' << exitDate->tm_mon << ' ' << exitDate->tm_year << "\n";
        cout << timegm(entryDate) << ' ' << timegm(exitDate) << boolalpha << ' ' << (timegm(entryDate) > timegm(exitDate)) << "\n";
        cout << "After: " << exitDate->tm_mday << ' ' << exitDate->tm_mon << ' ' << exitDate->tm_year << "\n";

        if (timegm(entryDate) > timegm(exitDate)) { // if entryDate is after exitDate
          cerr << "Error: entryDate is later than exitDate\n";
          cout << put_time(entryDate, "%d-%m-%Y") << ' ' << put_time(exitDate, "%d-%m-%Y") << '\n';
          continue;
        }
      }

      entryDate = new struct tm;
    }
    file.close();
  } else {
    cerr << "Error: Cannot open file\n";
    return 1;
  }
  return 0;
}

在某种程度上,我试图检查第一个日期是否大于第二个日期,因为他第一次约会是医院进入日期,第二次是出院日期。但是,当我试图将struct转换为time_t以便比较日期时,我意识到在调用timegm函数( mktime也会发生这种情况)之后,对某些输入结构进行了更改。我看到人们对这些功能有问题--通常是因为DST,但我看不出这对我的程序有什么影响。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-26 16:12:21

在我遇到linux手册中使用struct变量并与timegm无关的示例后,找到了解决方案。如果有同样的问题,只需将struct初始化为所有的零字节,如下所示:

代码语言:javascript
复制
memset(date, 0, sizeof(struct tm));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60836006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档