.net - 在应用程序级别定义TimeZone [英] .net - defining TimeZone at application level

查看:102
本文介绍了.net - 在应用程序级别定义TimeZone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由许多子应用程序组成的全球应用程序:

I have a global application compsed by many sub-applications :


  • 网站

  • Windows服务

我想在许多服务器上发送这些应用程序部分。
我的问题是:我希望日期仍然准确。
如果活动在太平洋时间下午6:00存储,我不希望我的欧洲用户在其董事会上看到下午6:00。

I want to dispatch those "application part" in many servers. My problem is : I want the dates to be still accurate. If an event is stored at 6:00 PM pacific time, I don't want my european users to see "6:00 PM" on their board.

我看过很多文章说你只需要保存你的日期
=>但我不能改变每个日期的保存方式,代表太多的工作(是的,它是datetime被保存,而不是dateoffset)。

I've read a lot of articles that say "you just have to save your dates that way" => but I can't change the way each single date is saved, that represent too much work (and yes, it's datetime that are saved and not dateoffset).

我已经阅读其他文章,建议更改服务器上的时区=>但我不能想要这样做,因为这些服务器可以用于其他目的。

I've read other articles that propose to change the time zone on the server => But I can't want to do that, as those server can be used for others purposes.

好消息是,日期在应用程序中始终被定义,数据库中不生成

The good news is that dates are always defined in the application and not generated by the database.

因此,我唯一要做的就是在应用程序级别定义TimeZone 。所以无论他们在哪里,我的应用程序将始终与相同的时区工作。剩下的唯一步骤是在UI级别转换当前用户的时区(非常简单,因为所有日期都通过相同的本地化日期时间转换方法)。

In consequence, the only thing I have to do is to define the TimeZone at application level. So wherever they are, my applications will always work with the same timezone. The only step left will be to convert the dates at UI level, in the timezone of the current user (very simple as all dates are passing throught the same localized datetime conversion method).

我真的不能改变应用程序开发人员的工作方式,因为它将是容易出错的。他们需要继续使用DateTime.Now,而不用考虑转换。

I really cannot afford to changes the ways the application developpers work, as it will be prone-to-error. They need to keep using DateTime.Now, without ever thinking of conversion.

我使用的确切技术与此不相关,问题是全球.Net。

The exact technologies I use are not relevant here, the question is global .Net.

感谢您的帮助

编辑:让我们说我的问题只是要使 DateTime.Now 返回 UTC日期
有没有办法这样做?

Edit : let's say my problem is just to have DateTime.Now return the UTC date. Is there a way to do that ?

推荐答案

存储所有时间在UTC。

Store all times in UTC.

DateTime.UtcNow

然后在客户端处理TZ更正。

then take care of the TZ correction on the client.

编辑:我错过了关于不更改DateTime.Now的关键部分。使程序员做到正确;这不是很容易出错。

I missed that crucial part about not changing DateTime.Now. Make the programmers do it right; it's not less prone to error.

您需要使用所有客户端代码使用DateTime.UtcNow或者让所有客户端代码都使用数据库来获取时间戳。无论哪种方式 - 你有工作要做。

祝你好运!

这篇关于.net - 在应用程序级别定义TimeZone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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