使用C#将时间转换到指定的时区? [英] Convert a time to specified time zone using C#?

查看:301
本文介绍了使用C#将时间转换到指定的时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.Net 3.5在C#中处理一个应用程序。我有时区的值存储在DB中,格式为( - 05:00,1),其中 -5.00 表示EST时区值,而 1 表示此时区属于夏令时(如果0不是夏令时区域)。



现在,我想将任何日期时间值转换为这个时区值,考虑到夏令时的值。



任何帮助都不胜感激。谢谢

解决方案

只有偏移量以及夏令时是否不足以指示时区:可以有几个时区具有相同的标准偏移量和DST偏移量,但是在不同时间具有DST转换。



这些数据是否已在数据库中,或者是还在设计吗理想情况下,您应该存储时区ID - TimeZoneInfo 适用于Windows ID,这不是理想的IMO(我喜欢其他业界倾向于使用的IANA / Olson ID),但至少它代表一个实时时区。



一旦你有一个 TimeZoneInfo 和你要转换的瞬间, a href =http://msdn.microsoft.com/en-us/library/system.timezoneinfo.converttimefromutc(v=VS.90).aspx =nofollow noreferrer> ConvertTimeFromUtc ConvertTimeToUtc 可能是你以后的 - 但要小心你的意思是任何日期时间值;你需要确保你总是知道你所代表的是什么。



BCL在所有这一切上都有些毛病,这是我开始的原因之一href =http://nodatime.org =nofollow noreferrer> Noda Time ,允许使用IANA时区数据以及BCL。


I'm working on an application in C# with .Net 3.5. I have time zone value of the User is stored in DB with this format (-05:00,1), where -5.00 represents EST time zone value and the 1 indicates that this time zone follows daylight saving (if 0 not a daylight saving zone).

Now I want to convert any date time value into this timezone value considering the daylight saving value.

Any help is appreciated. Thanks

解决方案

Having just an offset and whether or not it's in daylight saving time isn't really enough to indicate a time zone: there can be several time zones with the same standard offset and DST offset, but which have DST transitions at different times.

Is this data already in the database, or are you still designing it? Ideally you should store a time zone ID - the ones TimeZoneInfo works with are Windows IDs, which isn't ideal IMO (I prefer the IANA/Olson IDs that the rest of the industry tends to work with) but at least it does represent a real time zone.

Once you've got a TimeZoneInfo and an instant that you want to convert, ConvertTimeFromUtc and ConvertTimeToUtc are probably what you're after - but be careful about what you mean by "any date time value"; you need to make sure you always know exactly what you're representing.

The BCL is somewhat woolly on all of this, which is one of the reasons I started Noda Time, which allows use of the IANA time zone data as well as the BCL.

这篇关于使用C#将时间转换到指定的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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