timegm, mktime 改变 struct tm
Posted
技术标签:
【中文标题】timegm, mktime 改变 struct tm【英文标题】:timegm, mktime changing struct tm 【发布时间】:2020-03-24 17:14:58 【问题描述】:我尝试从文件中读取一些格式为 (d-m-Y) 的日期。日期是成对的,如果第二个日期等于“-”,那么我将该日期注册为 nullptr。这是文件:
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 -
这也是我的代码:
#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 tm 转换为 time_t 以便我可以比较日期时,我意识到对于某些输入,在我调用 timegm 函数后结构已经改变(它也发生在 mktime 上)。我见过人们通常因为 DST 而对这些功能有问题,但我看不出这对我的程序有何影响。
【问题讨论】:
【参考方案1】:我在 linux 手册页中遇到一个使用 struct tm 变量且与 timegm 无关的示例后找到了解决方案。如果有人遇到同样的问题,只需将 struct tm 初始化为全零字节,如下所示:
memset(date, 0, sizeof(struct tm));
【讨论】:
以上是关于timegm, mktime 改变 struct tm的主要内容,如果未能解决你的问题,请参考以下文章
linux把日期date转换为毫秒时间戳(struct tm)mktime()