-2

Is there a current implementation to get the localtime from the internet and not from the machine since on the machine the time can be tampered by the user? When I run the below code it shows me:

#include <iostream>
#include <iomanip>
#include <ctime>

int main()
{
    std::time_t t = std::time(nullptr);
    std::cout << "UTC:   " << std::put_time(std::gmtime(&t), "%c %Z") << '\n';
    std::cout << "local: " << std::put_time(std::localtime(&t), "%c %Z") << '\n';
}

Output:

UTC:   Mon Sep 16 09:56:10 2019 GMT
local: Mon Sep 16 11:56:10 2019 CEST

But when I change the time on my Ubuntu 18.04 machine the time output of both functions changes, too. The usecase would be to use it for licensing since repos like licensepp use just plain gmtime which has the easy workaround of just changing the time of the machine to surpass the expiration date.

Is there a function which uses the internet to get a similar output like above without using the machine time? Any links, code snippets are appreciated. Thanks.

Blind0ne
  • 1,015
  • 12
  • 28
  • 3
    No C++ doesn't even have any notion of "Internet". But your operating system might have, and could have its time synched from an NTP sever or similar. – Some programmer dude Sep 16 '19 at 10:02
  • 1
    And asking for code to fetch time from any external time-source will make your question off-topic. Please refresh [the help pages](http://stackoverflow.com/help), especially ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask) and [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Sep 16 '19 at 10:03
  • 2
    Possible duplicate of [sntp client in c++ for time/date](https://stackoverflow.com/questions/13010593/sntp-client-in-c-for-time-date) – Thomas Sablik Sep 16 '19 at 10:05
  • On a related note, most Linux systems usually keep their hardware clock in UTC, and calculate the local time by adding an offset. If you change the time on your local system, it will change the hardware clock so it's no longer in UTC. – Some programmer dude Sep 16 '19 at 10:09
  • 1
    `ntpdate -q` might be what you want. See its manual page for details. – hyde Sep 16 '19 at 10:09
  • On a related note, changing the system time, especially backwards, can result in some issues in programs which use the time for whatever (such as comparing timestamps of files). This is why there is generally a background daemon which keeps the system time continuously in sync with an NTP server without making it jump. – hyde Sep 16 '19 at 10:11
  • @hyde I want to use it for licensing since [licensepp](https://github.com/zuhd-org/licensepp) uses `std::gmtime(&t)` which can be easily change on a local machine. – Blind0ne Sep 16 '19 at 10:14
  • @Blind0ne In that case, it'd be trivial to run local NTP server to forge the time. If you enforced a specific server, it'd still be trivial to MITM the network connection (and probably preferable, since that wouldn't mess up the system time). If you want to check license relying on something from the internet, you need a secure connection to a server controlled by you. Then you could and should get rid of the whole time-based thing and just verify the license on the server. – hyde Sep 16 '19 at 10:18
  • 1
    I suggest you just trust the system time. Having PC time be wrong is very inconvenient, so I suggest you just trust the user to have their PC in correct time. You might have extra check, which requires application restart, if system time (compared to uptime) changes while the application is running / after license is checked. – hyde Sep 16 '19 at 10:20
  • You can easily detect large jumps in time by keeping an original time-stamp, and then keeping your own track of time from that original time-stamp. If the local time deviates to much from the calculated time you handle that in an appropriate way. It's still possible to "crack" but then everything is. – Some programmer dude Sep 16 '19 at 10:27
  • There's a decent version of an NTP client class for Windows/MFC on CodeProject, here: https://www.codeproject.com/Articles/461/CSNTPClient-An-SNTP-Implementation You may be able to adapt it to your own OS/compiler. – Adrian Mole Sep 16 '19 at 11:48
  • @hyde thank you for your input. I used your idea with extra checks and restart since it's more stable than establishing a connection to a ntp server. – Blind0ne Sep 18 '19 at 11:00

1 Answers1

7

It is certainly possible to write a server that responds to a request with the current time. It is also possible to write a client that sends such request across a network.

However, C++ does not have a standard API for network communication. So, first step figure out what system you are targeting, and then consult the documentation of the system about network communication.

One caveat is that network communication introduces a delay which will cause the response to be slightly outdated. Other considerations that apply to all communication are possibility of server downtime and other connectivity issues.

Note that there exists an internet protocol for clock synchronisation: NTP. And there are publicly available servers that can be queried. This may be a decent shortcut for your problem. Make sure to read and follow their terms of service especially if you use them for a commercial purpose.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • I think this is a good answer to my initial question and so I will accept it :). Thank you for your inputs. – Blind0ne Sep 18 '19 at 11:01