0

I'm writing a C++ application on an embedded ARM device running Embedded Linux.

I am trying to convert a date and time string to seconds, subtract it from the current time in seconds, and taking action if the number of seconds elapsed is greater than a certain number.

Something that should be quite simple to implement has proved quite tricky, and i'm not sure why.

The time difference i'm calculating turns out to be a massive number, when it should really be a low number. See my below code. I'm manually hardcoding a time and date string for testing.

std::string       timestr = "2020-12-21T16:07:00";
struct tm         t = {0};

sscanf(timestr.c_str(), "%04d-%02d-%02dT%02d:%02d:%02d",
           &t.tm_year, &t.tm_mon,  &t.tm_mday,
           &t.tm_hour, &t.tm_min, &t.tm_sec);

t.tm_year -= 1900;   // This is required because my year should be the number of years since 1900

auto tp    = std::chrono::system_clock::from_time_t(std::mktime(&t));
auto now   = std::chrono::system_clock::now();

auto now_s = std::chrono::time_point_cast<std::chrono::seconds>(now);
auto tp_s = std::chrono::time_point_cast<std::chrono::seconds>(tp);

std::chrono::duration<double> diff = now-tp;  // Huge number

auto elapsed = now_s - tp_s;   // This value is massive and not as expected when printed out
Engineer999
  • 3,683
  • 6
  • 33
  • 71
  • What unit do you think `now - tp` will be expressed in? Why? You need to `std::duration_cast()` it to the right unit, and you didn't. – underscore_d Dec 21 '20 at 16:29
  • Does this answer your question? [How to calculate a time difference in C++](https://stackoverflow.com/questions/728068/how-to-calculate-a-time-difference-in-c) – underscore_d Dec 21 '20 at 16:30
  • @underscore_d I've modified my question. I am using std::chrono::time_point_cast and still getting a wrong value back. – Engineer999 Dec 21 '20 at 16:41
  • Is it still "huge", or is it just "wrong"? What are the values of `tp`, `now`, and the "huge" and/or "wrong" result in both cases? It's not very clear. – underscore_d Dec 21 '20 at 16:49
  • @NathanPierson I am getting the time difference between now and a time a few minutes before. I would expect 120 seconds if it's 2 minutes for example – Engineer999 Dec 21 '20 at 16:49
  • @underscore_d : I am getting Time difference : now - tp : 51243598 . ... and Time difference now_s - tp_s : 4292288967. I can't understand why – Engineer999 Dec 21 '20 at 16:55
  • @underscore_d I've solved it. You know what the bug / problem was?? Need to subtract 1 from the month also. Therefore January starts at 0 and not 1. – Engineer999 Dec 21 '20 at 17:43
  • How are you handling daylight savings time? – JohnFilleau Dec 21 '20 at 18:54
  • @JohnFilleau What do you mean? – Engineer999 Dec 21 '20 at 20:01
  • Are you always dealing with timezoneless times GMT/UTC etc? Or could you be dealing with times within political boundaries that utilize daylight savings time? The difference between 1:30 am and 3:30 am on the same day is usually two hours, but sometimes one hour and sometimes three, depending on where you live. – JohnFilleau Dec 21 '20 at 20:33
  • @Engineer999 Well discovered! Seems worth you posting an answer to get this closed off. – underscore_d Dec 22 '20 at 09:59

1 Answers1

0

For those who are interested, I solved this problem.

Not only should we subtract 1900 from the number of years before calling std::mktime(&t) .

t.tm_year -= 1900;

but also 1 must be subtracted from the number of months t.tm_mon -= 1 .

The months are numbered from 0 to 11 and not 1 to 12 as we would expect.

This explains why there was a big difference in seconds.

Engineer999
  • 3,683
  • 6
  • 33
  • 71