帮助如何在c#中获取服务器日期 [英] Help How to get server date in c#

查看:133
本文介绍了帮助如何在c#中获取服务器日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮助如何在c#中获取服务器日期

Help How to get server date in c#

推荐答案

你不会相信: System.DateTime.Now ,或者更好, System.DateTime.UtcNow 。 :-)



无论你做什么: http://msdn.microsoft.com/en-us/library/system.datetime.aspx [ ^ ]。



-SA
You not gonna believe that: System.DateTime.Now, or, better, System.DateTime.UtcNow. :-)

Whatever you do: http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].

—SA


如果服务器是数据库服务器,并且您的客户端都访问该数据库,那么您应该能够从数据库中获取时间,例如SQL Server中的 GETDATE()。否则你会想要UTC。我认为其他任何东西都不会保证是您想要的。



在我目前的工作中,我访问了美国各地的许多服务器,可能是全世界的。为保持一致性,它们都设置为美国中部时间(UTC-6:00)。但我的设置指定亚利桑那州(UTC-7:00)所以我看到了。另一个用户可能会看到另一个时间。



因此,您需要使用UTC或让所有客户端询问一个公共进程(例如数据库服务)。
If the server is a database server, and your clients all access that database, then you should be able to get the time from the database, e.g. GETDATE() in SQL Server. Otherwise you'll want UTC. I don't think anything else will be guaranteed to be what you want.

At my current job I access many servers across the U.S., possibly around the world. For consistency, they are all set to "U.S. Central time" (UTC-6:00). But my settings specify Arizona (UTC-7:00) so I see that. Another user might see yet another time.

So, you need to use UTC or have all your clients ask a common process (the database service for instance).


这些答案是部分且不清楚的。

一个建议是建立一个允许服务器发送UTC时间的Web服务。这是最好的答案,但您应该注意,如果您有两台服务器,则需要保持同步。所有服务器都从时间服务器(通常是Microsoft或其他NTP服务器)更新/维护时间。应始终在服务器上更新时间以保持同步,您不能指望您的服务器成为世界的时间标准。所以这需要来自网络或从原子钟管理它的NTP设备。



当您编写记录时,您的应用程序可以从您的服务器获得所需的时间。从页面代码中获取服务器端时间并不容易。这就是建议服务的原因。 Web服务将从服务器中抽出时间,并在调用Web服务时将其发送回客户端。然后你可以使用那些服务器来更新时间,但请注意返回的时间不会继续滴答作响。如果您的进程在将记录写入数据库之前运行很长时间,则需要保留内部时钟。



例如,您在Web服务中查询UTC时间,它返回12:01:44:025,12小时,1分44秒25毫秒。如果您的处理需要200毫秒,那么在将记录写入数据库之前,您可能希望将毫秒增加到:225。从您的Web服务中获取的时间戳值在处理时不会持续200秒。



因此,如果这种准确度很重要,您可以管理它。通过从服务器接收时间戳从本地PC获取另一个时间戳,然后在存储处理数据之前从当前时间减去该时间戳,但首先将该差异添加到从服务返回的服务器时间戳。



听起来像很多人。但是除了Web服务之外,如果你不熟悉它并不难实现。
These answers are partial and unclear.
One suggestion was to set up a web service that allows the server to send out UTC time. This is the best answer, but you should note that if you have two servers they need to stay in sync. All servers get their time updated/maintained from time servers, typically Microsoft or other NTP servers. Time should always be updated on server to stay in sync, you can not expect your server to be the time standard for the world. So that needs to come from the network or NTP devices that manage that from an atomic clock.

You app could then get the needed time from your server when you write records. There is no easy technique for getting server side time from a code behind page. That is why the service was suggested. The web service would pull the time from the server and send it back to the client in the call to the web service. Then you could use that servers time to update, but note that the time returned is not going to keep ticking. You will need to keep an internal clock if your process runs long before you write your record to your database.

Example, you query your web service for the UTC time and it returns 12:01:44:025, twelve hours, 1 minute, 44 seconds, 25 milliseconds. If your processing takes say :200 milliseconds, you would want to increase your milliseconds to :225 before writing the record to your database. The timestamp value you get back from your web service will not keep ticking up 200 seconds while you are processing.

So if that level of accuracy is important you can manage it. By getting another timestamp from the local PC when you receive the timestamp from the server, then subtracting that from the current time just prior to storing your processed data, but first adding that difference to the server timestamp that was returned from the service.

Sounds like a lot of over head. But its not that difficult to implement, other than the web service if you are not familiar with it.


这篇关于帮助如何在c#中获取服务器日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆