同步时间,但忽略时区

时间:2022-10-28 20:45:12

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.

我有一个应用程序,我想有一个功能,它同步Windows系统时钟,但只设置分钟,除非例如系统时间是13:58,时间服务器是14:02,在这种情况下小时应该也改变了。问题是,我不想使用时间服务器小时/天等因为我希望用户能够将时钟设置为任何时间(无论时区如何),但仍然要同步。

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.

例如,即使windows时区是PST并且用户在未改变时区的情况下将时钟设置为5小时,我希望系统正确同步,将来保持5小时。

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!

是否有算法可以计算是否应该根据分钟设置小时前进或后退一小时但忽略任何时区因素?如果系统是15:59并且服务器是16:02,则系统应该设置为16:02而不是15:02(如果它只改变了分钟,则会设置为16:02)。如果系统是15:28且服务器是16:02,则系统应设置为15:02。我已经考虑了一段时间了,但似乎无法理解我将如何去做!

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.

我已经有了获取时间服务器日期和时间的代码,以及系统时间(我使用的是delphi),所以这只是计算如何处理它的问题。

Thanks for your time.

谢谢你的时间。

4 个解决方案

#1


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.

听起来您真正想要的只是将客户端计算机的UTC时间与服务器的UTC时间同步,而不管服务器或客户端的时区。

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

您需要找到基于UTC获取和设置时间的API。

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...

这可能不是你想要的,因为我没有完全理解你的用户在未来5小时内设置机器的问题...

#2


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.

不要忘记有一些时区不在小时边界...如果用户的时钟是13:30而服务器在13:00你怎么知道用户不是简单的不在纽芬兰而不是(比如说) )蒙特利尔。

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

我认为Jon对UTC的建议是要走的路。

#3


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

获取服务器时间,减去N小时,设置当地时间。没有问题。

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).

如果您必须在计算中包含时区,请使用我的GpTimezone(说明)。

#4


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.

您可以只比较分钟,如果您> 55且系统<05,则增加您的小时。对其他边缘条件也做类似的事情。

#1


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.

听起来您真正想要的只是将客户端计算机的UTC时间与服务器的UTC时间同步,而不管服务器或客户端的时区。

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

您需要找到基于UTC获取和设置时间的API。

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...

这可能不是你想要的,因为我没有完全理解你的用户在未来5小时内设置机器的问题...

#2


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.

不要忘记有一些时区不在小时边界...如果用户的时钟是13:30而服务器在13:00你怎么知道用户不是简单的不在纽芬兰而不是(比如说) )蒙特利尔。

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

我认为Jon对UTC的建议是要走的路。

#3


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

获取服务器时间,减去N小时,设置当地时间。没有问题。

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).

如果您必须在计算中包含时区,请使用我的GpTimezone(说明)。

#4


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.

您可以只比较分钟,如果您> 55且系统<05,则增加您的小时。对其他边缘条件也做类似的事情。