有没有办法定义在ASP.NET应用程序中的时区? [英] Is there a way to define the timezone for an application in ASP.NET?

查看:184
本文介绍了有没有办法定义在ASP.NET应用程序中的时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来定义时区在ASP.NET应用程序使得从读/相比,目前的服务器时,所有的时间被隐式转换,或者我需要把转换报表每DateTime.Now打电话?

Is there a way to define the timezone for an application in ASP.NET such that all times read from/compared to current server time are implicitly converted, or do I need to put in conversion statements as each and every DateTime.Now call?

推荐答案

我不知道关于ASP的最新变阵,但这<一个href=\"http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/772971a4-9f1b-4004-a873-c1e0e2098852/\"相对=nofollow>从2006年本文提供了一个有趣的答案:

I am not sure about the latest evolutions of ASP, but this article from 2006 provides an interesting answer:

的问题是,时区信息不直接可通过网络浏览器。你可以使用启发式技术来确定正确的时区或你将不得不存储时区设置,根据他们选择的用户。

The problem is that timezone information is not directly available through the web browser. You could use heuristics to determine the correct time zone or you will have to store the timezone settings for a user based on their selection.

接下来可能无法与.net 3.5和被需要TimeZoneInfo..ConvertTime 方法,不过,为了完整起见:

What follows may not be need with .Net 3.5 and its TimeZoneInfo..ConvertTime method, but still, for completeness sake:

一旦你有你现在需要的时间翻译时区设置。 您应该总是存储在数据库中的UTC日期/时间值。这消除了许多的转换问题。如果您存储在UTC的信息,那么你只需要从UTC到用户的本地时区转换时,显示的数据给他们,你需要,当你从他们那里得到的日期/时间值从他们的时区转换为UTC。

Once you have the timezone settings you now need to translate the times. You should always store your date/time values in the DB in UTC. This eliminates many conversion issues. If you store the information in UTC then you need only translate from UTC to the user's local timezone when you display the data to them and you need to convert from their time zone to UTC when you get date/time values from them.

是什么让这更困难的是,TimeZone类是不是所有的有用。幸运的是在2.0版的日期时间进行了更新,以支持上的时间是否在UTC与否的指标。 您可以使用DateTime.ToUniversalTime方法的日期时间转换为UTC。然而不幸的是,你不能将其转换回

What makes this more difficult is the fact that the TimeZone class is not all that useful. Fortunately in v2.0 DateTime was updated to support an indicator on whether the time is in UTC or not. You can convert a DateTime to UTC using the DateTime.ToUniversalTime method. Unfortunately however you can't convert it back.

借助 ToLocalTime 方法是使用本地时区其中,当在服务器上运行,使用服务器的时区这是不是你想要的。更糟糕的是但是,你不能简单地创建一个TimeZone对象,并用它作为这种支持不存在。

The ToLocalTime method uses the local time zone which, when run on the server, uses the server's time zone which isn't what you wanted. Even worse however is that you can't simply create a TimeZone object and use it as that support doesn't exist.

迈克尔Brumm创造了一个很好的小类,以能够轻松地创建和使用的时区。我在自己的应用程序的个人使用code的重大的修改版本,它工作得很好。下面是来自DB UTC值给本地用户的时区转换的步骤。

Michael Brumm created a nice little class for being able to create and use time zones easily. I personally use a heavily modified version of that code in my own apps and it works nicely. Here are the steps to convert from a DB UTC value to the local user's time zone.

1)获取存储区值,为用户

2)创建的SimpleTimeZone类来包装它(使用一个数据库值映射到底层的Windows注册表版本的一些映射方案)

3)使用SimpleTimeZone.ToLocalTime方法将DateTime值转换为本地时间。

1) Get the stored timezone value for the user
2) Create a SimpleTimeZone class to wrap it (using some mapping scheme that maps a DB value to the underlying Windows registry version)
3) Use the SimpleTimeZone.ToLocalTime method to convert the DateTime value to the local time.

有关性能方面的原因,你应该可能得到和初始化的SimpleTimeZone实例,并在项目属性的请求的长度缓存,所以你不必继续创造它。

For performance reasons you should probably get and initialize the SimpleTimeZone instance and cache it in the Items property for the length of the request so you don't have to keep creating it.

有关从用户的本地时区转换为UTC做相反的:

For converting from the user's local timezone to UTC do the reverse:

1)从用户
2)创建的SimpleTimeZone类来包装它

3)使用方法SimpleTimeZone.ToUniversalTime的日期时间转换为UTC。

1) Get the stored timezone value from the user
2) Create a SimpleTimeZone class to wrap it
3) Use SimpleTimeZone.ToUniversalTime method to convert the DateTime to UTC.

这篇关于有没有办法定义在ASP.NET应用程序中的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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