转换使用C#一时间指定时区? [英] Convert a time to specified time zone using C#?
问题描述
我工作在C#与.net 3.5的应用程序。我已经在用户的时区值存储在数据库这种格式的( - 05:00,1),其中 -5.00 表示EST时区的价值和<强> 1 表示该时区如下夏令(如果不是0一夏令时区)。
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
推荐答案
刚刚偏移以及是否是夏令时是不是真的足以说明一个时区:可以有同一标准偏移和DST偏移几个时区,但在不同时间DST转换。
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.
这是数据已经在数据库中,或者是你还设计它?理想情况下,你应该存储时区ID - 那些 的TimeZoneInfo
与作品都是Windows的ID,这是不理想的国际海事组织(我更喜欢IANA /奥尔森的ID,该行业的其他趋向一起工作),但至少它确实代表了真正的时区。
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.
一旦你得到了一个的TimeZoneInfo
键,你要转换,<一个瞬间HREF =http://msdn.microsoft.com/en-us/library/system.timezoneinfo.converttimefromutc(v=VS.90).aspx相对=nofollow> ConvertTimeFromUtc
和的 ConvertTimeToUtc
可能是你追求的 - 但要小心你的意思是任何日期时间值是什么;你需要确保你总是知道你代表什么。
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.
首创置业是有点毛茸茸的这一切,这是我开始<一的原因之一HREF =http://nodatime.org相对=nofollow>野田佳彦时间,它允许使用IANA时区数据以及首创置业的。
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屋!