views:

192

answers:

4

Hi,

I have an application that I want to have a feature where it synchronizes the Windows system clock, but only setting the minutes, unless for example the system time is 13:58 and the time server is 14:02, in which case the hour should be changed too. The thing is, I don't want to use the time server hours/days etc because I want the user to be able to set the clock to any time (regardless of time zone) but for it to still synchronize.

For example even if the windows timezone is PST and the user sets the clock 5 hours in the future without changing the time zone, I want the system to synchronize correctly, keeping it 5 hours in the future.

Is there an algorithm that could calculate whether it should be setting the hour forward or backwards an hour depending on the minutes but disregarding any time zone factors? If the system is 15:59 and the server is 16:02 the system should set to 16:02 rather than 15:02 (which it would if it only changed the minutes). If the system is 15:28 and the server is 16:02, the system should be set to 15:02. I've been mulling this over for a while now, but can't seem to get my head around how I would go about it!

I already have the code to get the time server date and time, and the system time (I'm using delphi), so it's just a matter of calculating what to do with it.

Thanks for your time.

+3  A: 

It sounds like what you really want is just to synchronize the client machine's UTC time to be the same as the server's UTC time, regardless of the time zones of either the server or the client.

You'll want to find the APIs to get and set the time based on UTC.

That may not be what you want, because I didn't fully understand your question in terms of the user setting their machine 5 hours in the future...

Jon Skeet
Thanks to the reply,basically, the clock could be intentionally set hours in the future/past, in which case using the UTC is perhaps not the option, as I presume it would correct the time based on the Windows time zone? Which is not the desired behaviour.
It wouldn't correct it based on the Windows time zone - it would correct it based on UTC. What valid use case is there for deliberately putting the system clock several hours out? If you spot that the time is 30 minutes out, would you expect it to move the time forward 30 minutes or back?
Jon Skeet
A: 

You could just compare the minutes and if you're >55 and system is <05, then increment your hour. Do something similar for the other edge condition as well.

Brian Knoblauch
+1  A: 

Don't forget that there are some timezones that are not on hour boundaries ... if the user's clock is at 13:30 and the server is at 13:00 how do you know the user is simple not in Newfoundland rather than (say) Montreal.

I think Jon's suggestion of UTC is the way to go.

Rob Walker
Indeed. There are even some annoying ones aligned on 15-minute boundaries, which doesn't give you that much leeway for telling the difference between a clock that out by a little bit and one that's in a different timezone.
bobince
+1  A: 

Get the server time, subtract N hours, set the local time. There is no problem.

Time is a real number. Hours, minutes, seconds etc are just a presentation format.

In case you have to include time zones in your calculation, use my GpTimezone (description).

gabr