mardi 24 mars 2020

timegm, mktime changing struct tm

I try to read some dates in format (d-m-Y) from a file. The dates are in pairs and if the second date is equal to '-' then I register that date as a nullptr. Here is the file:

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 -

Here is my code as well:

#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;
}

At some point I try to check if the first date is bigger than the second one because he first date is a hospital entry date and the second is an exit date. But when i tried to convert the struct tm to time_t so I can compare the dates, I realized that for some inputs the struct had changed after I called the timegm function (it happens with mktime as well). I've seen people having problems with these functions usually because of DST, but I can't see how that affects my program.

Aucun commentaire:

Enregistrer un commentaire