使用 TimeZoneInfo 在 UTC/指定时区之间进行转换不起作用
我们数据库中的所有日期/时间数据均以 UTC 时间存储。我正在尝试编写一个函数来将该 UTC 时间转换为用户的首选时区(他们可以在他们的个人资料中选择他们的时区,因此它与他们计算机上的本地设置无关,而与他们选择的时区有关)来自可用选项的下拉列表。
此函数位于 DevExpress AspxGridView 事件的上下文中(第三方控件与问题无关,但我想我会提到它):
DateTimeOffset utcTime = (DateTimeOffset)e.Value;
TimeZoneInfo destTimeZone = Helper.GetTimeZoneInfo();
DateTime modifiedDate = TimeZoneInfo
.ConvertTimeFromUtc(utcTime.DateTime, destTimeZone);
e.DisplayText = String.Format("{0} {1}",
modifiedDate.ToString("g"),
destTimeZone.Abbreviation());
Helper.GetTimeZoneInfo()
只是返回一个与用户选择的 TimeZoneInfo 类相对应的类,或者如果用户未选择该类,则默认为“太平洋标准时间”。
在我切换系统之前,这通常可以正常工作。时钟(该服务器运行的时间)从今天(10 月 14 日,夏令时日期)到 1 月 11 日(非夏令时)
时间似乎始终以夏令时显示(即始终有 7 小时偏移) 。 。无论我如何处理系统时钟,我都无法调整额外的时间。
例如,当我将时区设置为太平洋标准时间(UTC 时间 10-10-2011 20:00)时: 00,始终显示此时间:
10-10-2011 13:00:00(DST 时间,偏移量 -7)。
在非夏令时(标准)期间,时间应为:
2011 年 10 月 10 日 12:00:00(偏移量 -8)。
我缺少什么?
All of our date/time data in our database is stored in UTC time. I'm trying to write a function to convert that UTC time into the user's preferred time zone (they can select their timezone in their profile so it has NOTHING to do with local settings on their computer and everything to do with which timezone they have selected from a dropdown of available choices.
This function is in the context of a DevExpress AspxGridView event (the third party control is not relevant to the question but I thought I'd mention it):
DateTimeOffset utcTime = (DateTimeOffset)e.Value;
TimeZoneInfo destTimeZone = Helper.GetTimeZoneInfo();
DateTime modifiedDate = TimeZoneInfo
.ConvertTimeFromUtc(utcTime.DateTime, destTimeZone);
e.DisplayText = String.Format("{0} {1}",
modifiedDate.ToString("g"),
destTimeZone.Abbreviation());
Helper.GetTimeZoneInfo()
simply returns a TimeZoneInfo class that corresponds to the one the user selected, or defaults to "Pacific Standard Time" if they have not chosen one.
This generally works fine until I switch my system clock (which this server is running on) from today (which is Oct. 14, a DST date) to something like January 11, which is NOT DST.
The time seems to always be displayed in DST (i.e. always a 7 hour offset). No matter what I do with my system clock, I can't get the time to adjust for the extra hour.
For example, when I have my timezone set to Pacific Standard Time, for UTC time 10-10-2011 20:00:00, it is always displaying this time:
10-10-2011 13:00:00 (the DST time, offset of -7).
During non-Daylight Savings dates (standard), the time should be:
10-10-2011 12:00:00 (offset of -8).
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UTC 时间转换后的本地时间始终基于该 UTC 时间的时区信息...而不是当前时间。
也就是说,UTC 时间 2011 年 10 月 10 日的 PST 偏移量始终 -7。您在哪一天进行转换并不重要。
……或者我误解了你的要求?
The converted local time of a UTC time is always based on the time zone info for that UTC time... not for whatever the current time happens to be.
That is, the PST offset on Oct 10, 2011 UTC is always -7. It doesn't matter what date you are doing the conversion on.
...Or am I misunderstanding what you are asking?
您可以查看e.Value。
它的 DateTimeKind 是否设置为 DateTimeKind.Utc 还是 DateTimeKind.Unspecified
如果是 Unspecified,转换将无法正常工作。
无法直接设置 Kind。我想出的最好的办法就是
希望这会有所帮助,
艾伦。
You might have a look at the e.Value.
Is the DateTimeKind for it set to DateTimeKind.Utc or is it DateTimeKind.Unspecified
If it is Unspecified, the conversion will not work correctly.
One cannot set the Kind directly. The best I have come up with is something along the lines of
Hope this helps,
Alan.