如何处理独立于托管服务器时区的DateTime的转换? [英] How to handle conversion of a DateTime independently of the timezone of the hosting server?

查看:132
本文介绍了如何处理独立于托管服务器时区的DateTime的转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在世界不同时区可能出现的应用程序,仅举几例,在德国和英国。

I have an application which potentially rans in different timezones in the world, to name a few it runs in Germany & UK.

现在我有一个要求,我必须解释datetime对象,因为它们是GMT日期,然后得到等效的UTC datetime。这应该发生在时区之外,例如,如果应用程序在德国运行,并且DateTime应该被解释为GMT并转换为UTC,然后保存在服务器上。我将处理DST也会移动。

Now I have a requirement wherein I have to interpret datetime objects as they are GMT dates, and then get the equivalent UTC datetime out of it. This should happen irrespective of timezones, for example if the application is running in Germany as well the DateTime should be interpreted as GMT and converted to UTC before saving on the server.I do deal will DST shifts as well.

例如:

如果我有一个这样的datetime对象:
var date = new日期时间(2011,03,28,01,00,00),应将其转换为等效的UTC。在这种情况下,应为 28/03/2011 01: 00:00 +01:00:00 ,但是当我的服务器读取保存的datetime时,它会发现它是一个 28/03/2011 01:00:00 +02:00 :00 。我的UI现在在德国运行,我怀疑这些日期被解释为本地日期(CET)。

If I have a datetime object like this: var date = new Datetime(2011,03,28,01,00,00), this should be converted to it's equivalent UTC.In this case it should be 28/03/2011 01:00:00 +01:00:00, however while my server reads the saved datetime it finds it as a 28/03/2011 01:00:00 +02:00:00. My UI was running in Germany at the moment and I suspect the dates were interpreted as local dates(CET).

你能告诉我如何执行准确的转换?

Can you please advise me how to perform the accurate conversion?

推荐答案

    $ b $通常被认为是最佳做法b
  • 将数据时间作为UTC

  • 尽可能晚地以时区感知用户友好的格式呈现此文件

  • Store datetimes as UTC
  • Render this in timezone-aware user friendly format as late as possible

因此,我建议您依赖于 DateTime.Now ,而是考虑使用 DateTime .UtcNow

Thus, I would suggest you to not rely on DateTime.Now, but instead consider using DateTime.UtcNow.

如果您允许每个用户确定(通过首选项/选项面板)选择自己的时区,则可以以适当的用户友好格式呈现UTC日期使用:

Provided you allow each user to determine (through a Preferences/Options panel) to select its own timezone, then you could render an UTC date in the appropriate user-friendly format using:


string timeZoneInfoId = "Romance Standard Time"; // Or any valid Windows timezone id ;-)
var tzi = TimeZoneInfo.FindSystemTimeZoneById(timeZoneInfoId);

//Build a DateTimeOffset which holds the correct offset considering the user's timezone
var dto = new DateTimeOffset(TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, timeZoneInfo), timeZoneInfo.GetUtcOffset(utcDateTime));

var formated = string.Format("{0} {1} ({2})", dto.ToString("d"), dto.ToString("T"), dto.ToString("zzz")); //Or whatever format that fits you

注意:您可以找到所有有效的Windows timeZoneId列表 here

Note: You can find the list of all valid Windows timeZoneId here.

如果您愿意为用户添加选择列表来选择其渲染时区,则可以使用 TimeZoneInfo.GetSystemTimeZones()检索 TimeZoneInfo 的列表,然后使用每个TimeZoneInfo的 Id 属性作为选项的值并调用每个TimeZoneInfo的 ToString()方法呈现选项的内容。

Provided you're willing to add a selecting list for the user to choose its rendering timezone, you could use TimeZoneInfo.GetSystemTimeZones() to retrieve a list of TimeZoneInfo, then use the Id property of each TimeZoneInfo as the option's value and call the ToString() method of each TimeZoneInfo to render the option's content.

您的选择列表将呈现类似的方式比本机Windows:

Your selecting list would render in a similar way than native Windows one:

这篇关于如何处理独立于托管服务器时区的DateTime的转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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