Home > Net >  C - Saving time in a file, then comparing it with current time
C - Saving time in a file, then comparing it with current time

Time:09-27

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.

CodePudding user response:

This may not be an answer to your question but this video explains the problem with time zones: https://www.youtube.com/watch?v=-5wpm-gesOY

As others have suggested, try to use an independent time format otherwise madness will follow.

CodePudding user response:

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

  • Related