-1

I would like to store/save time in a file. That's no issue, BUT: What if a time zone changes? Or daylight time may differ.

How can I compare the saved time in a file with the current time? What if the time zone changes or daylight shifts.

Current time has to be always greater than saved time in order to continue with the program.

Thanks for any advice.

  • 2
    Save the time in some manner that doesn't change, like GMT, or epoch time. Standard routines exist to convert between different systems of time. – john Sep 26 '22 at 12:58
  • 2
    Save the time as UTC time then convert as necessary. If internally you always use UTC and only display (or input) in the local time the code becomes much simpler. – Richard Critten Sep 26 '22 at 12:58
  • 1
    without code this question is not related to C++. Requesting code to be written is offtopic – 463035818_is_not_an_ai Sep 26 '22 at 12:59

1 Answers1

1

Unix timestamps do not change accross timezones

To avoid timezone changes & other problems with time you can probably use timestamps. In C++ they are available through chrono library. Example and +/- related question

#include <iostream>
#include <chrono>
 
int main()
{
    const auto p1 = std::chrono::system_clock::now();
 
    std::cout << "seconds since epoch: "
              << std::chrono::duration_cast<std::chrono::seconds>(
                   p1.time_since_epoch()).count() << '\n';
}

Then if this is enough for you just save timestamp to file and compare with a new timestamp. Probably you would like to have it in a human readable format. Some useful ways to do this in C++ can be found here

Faekr
  • 128
  • 7