Question From Specific DateTime will give real time

Valakik

Member
Joined
Nov 8, 2020
Messages
12
Programming Experience
Beginner
Hello
I don't find any information on the google how to do the following:
- I get from server side the server time through API. I load in DateTime one time when the form load completely.
- However I don't know from that point how the app will use that DateTime as normal date and time. What I mean is that the app wouldn't use the local time which is on the pc but the server time.
- I don't want to request everytime what is the realtime on serverside every 1 second because that would be heavy after long term to the webserver so I thought if the pc will get once the server time and from that point use that time and working as normal clock.

I tried with this and put in a timer with 100 interval but didn't even move the numbers:

C#:
DateTime something = new DateTime((int)stuff.year, (int)stuff.month, (int)stuff.day, (int)stuff.hour, (int)stuff.minutes, (int)stuff.seconds);
var test = something.AddSeconds(1);

How can make the app from the specific DateTime will functional as a normal date and clock?
 
Assuming that the local PC and the server are synchronized at least once a day with a NTP server, then both the local PC and the server with have the correct UTC time with negligible time difference. All modern Windows OSes do this NTP sync by default and had to be explicitly turned off. You don't even have to ask the server what time it thinks it is. You just need to ask it what time zone it is in.

Also consider the kind of problems you have to solve if you asked the server for the time as opposed to making the reasonable assumption that the machine are synchronized: There is a lag between the time you request the time and when you get back the response. You will need to take that latency into account. To solve that problem, you would need to basically reimplement parts of the NTP protocol which has already solved this problem.
 
Last edited:
Well in my case is just a simple parental control app so the kids will not using in my house the computer between time.
The kids are smart to change the time on the pc to cheat the available time to use the laptop.
So therefore I thought to make that way where the webserver gives the server time to the C# app and the app will use that information.
For example the time is 15:00:00 and the C# app gets that information. I wanted that the app continue the clock from 15:00:00 so I don't need to request every seconds from the API.
So the C# will continue after loaded in 1 time from the API, 15:00:01 15:00:02 15:00:03 and so on.
But the problem is that I don't know how to do that.
 
You can measure time with a Stopwatch, at any time you can add the previously given server time and the Elapsed time from watch to get current time.
C#:
var timeNow = serverTime + watch.Elapsed;
 
Also, if they smart enough to change the time, then they are smart enough to just terminate your process.
 
Thank you for the answer. It works. :)
About your last comment the solution is to make for them an account which doesn't have admin permission but user permission.
With this method of course they can close the app however I will make a service which will check if the app is running. If they want to disable the service to stop checking then the windows will ask for admin permission eventually.
So if they success to close the app then the service will start the app automatically and everything works again.
 
Back
Top Bottom