解析ISO8601日期/时间与日期时间结构 [英] Parsing ISO8601 date/time with DateTime struct

查看:5273
本文介绍了解析ISO8601日期/时间与日期时间结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析ISO8601格式的日期/时间字符串与.NET的日期时间的结构。

I am trying to parse an ISO8601 formatted date/time string with .NET's DateTime structure.

为了让我给问题的全部underdstanding,我将执行使用.NET和JavaScript的测试。我目前在英国(英国夏令时,这是UTC + 01:00)。

我的ISO8601的理解是:

My understanding of ISO8601 is that:

  • 当字符串后缀Z,时间是pssed在UTC EX $ P $。
  • 当字符串后缀以+/- HH:MM,时间是pssed在EX $ P $ 当地时间,这里的+/- HH:MM。EX presses从UTC偏移
  • When the string is suffixed with "Z", the time is expressed in UTC.
  • When the string is suffixed with "+/-hh:mm", the time is expressed in Local time, where the "+/-hh:mm" expresses the offset from UTC.

考虑下面的ISO8601日期/时间格式字符串:

"1987-01-05T08:45:30.500+0100"

由于我分以上,这串前presses的8时45分30秒的本地时间,与7点45分三十秒

Given my points above, this string expresses a local time of "08:45:30", and a UTC time of "07:45:30"

当前时区的测试(.NET)

DateTime now = DateTime.Now;
Console.WriteLine(now.ToLocalTime()); // 27/08/2013 12:02:43
Console.WriteLine(now.ToUniversalTime()); // 27/08/2013 11:02:43

当前时区的测试(JavaScript的)

var now = new Date(Date.now());
now.toString(); // Tue Aug 27 2013 12:03:46 GMT+0100 (GMT Daylight Time)
now.toUTCString(); // Tue, 27 Aug 2013 11:03:46 GMT

除了未成年人(分/秒)的两个例子之间的差异,他们正在返回正是我所期望的英国夏令时间(UTC + 01:00)。 的分/秒的差异是因为我不能同时运行一个.NET测试,并在完全相同的时间一个JavaScript测试。

所以,现在让我用ISO8601的日期/时间格式的字符串:

So now lets use my ISO8601 date/time formatted string:

解析ISO8601格式字符串(.NET)

DateTime dt = DateTime.Parse("1987-01-05T08:45:30.500+0100");
Console.WriteLine(dt.ToLocalTime()); // 05/01/1987 07:45:30
Console.WriteLine(dt.ToUniversalTime()); // 05/01/1987 07:45:30

解析ISO8601格式字符串(JavaScript的)

var dt = new Date("1987-01-05T08:45:30.500+0100");
dt.toString(); // Mon Jan 05 1987 07:45:30 GMT+0000 (GMT Standard Time)
dt.toUTCString(); //Mon, 05 Jan 1987 07:45:30 GMT

这似乎并不与它显示的日期/时间现在的例子是一致的。为什么这个显示,就好像我在英国的冬季时间(UTC + 00:00),在现在的例子显示在英国夏令时间(UTC + 01:00)?

This does not seem consistent with the example which displays the date/time "now". Why is this displaying as though I am in British Winter time (UTC+00:00), when the "now" examples display in British Summer Time (UTC+01:00)?

如果我改变我的时区设置,我得到预期的结果本地/世界时间,但如果它们设置为我现在的时间/区,这似乎给inconsisteent结果。

If I change my time zone settings, I get the expected results for Local/Universal time, but if they are set to my current time/zone, this seems to give inconsisteent results.

编辑:总之,就好像.NET和JavaScript的都忽略夏令时/英国夏令时间(UTC + 01:00)当我尝试解析字符串。其结果将是正确的冬季,也是正确的,当我身体改变我的时间/区......但就是代表这是不正确的任何的机器我已经在英国测试了。

In short, it's as if both .NET and JavaScript are ignoring daylight saving/british summer time (UTC+01:00) when I try to parse the string. The result would be correct for winter, and also correct when I physically change my time/zone...but as is stands this is not correct on "any" machine I have tested on in Britain.

推荐答案

英国使用的夏令时(促进他们的时钟一小时),在夏季(8月27日)的,但不是在冬季(1月5日)。

Britain uses summer time (advancing their clocks by one hour) in the summer (27 August), but not in the winter (5 January).

其实,在冬天英国使用UTC。你的机器似乎有一个英国的CultureInfo

Actually, in the winter Britain uses UTC. Your machine seems to have a British CultureInfo.

您可以检查 dt.IsDaylightSavingTime()

增加:

我的回答是有关.NET只(但也许同样适用于JavaScript的?)。你提供工作的例子完全符合市场预期。日期和时间1987年1月将被转换成当地的文化,按理说它是英国,并于1987年1月英国在+0000因为它是冬天。你给的时间字符串被加盖+0100(好像它是从德国或其他国家的一小时,英国东),这是当字符串被分析确认。在1987年的夏天,日期将正确地转换了不同,因为在1987年,英国(和所有的欧洲经济共同体)夏天的观察夏令时间(夏令时间)。

My answer is about .NET only (but maybe the same applies to JavaScript?). The examples you provide work entirely as expected. A date and time in January 1987 will be converted into your local "culture", which is supposedly British, and in January 1987 the United Kingdom was at +0000 since it was winter. The time string you gave was stamped with +0100 (as if it was from Germany or some other country one hour to the east of Britain), and this was acknowledged when the string was parsed. A date in the summer of 1987 would correctly have converted differently because in the summer of 1987 Britain (and all of the EEC) observed summer time (daylight saving time).

综上所述:偏移指示或区域说明 +0100 的时间preting的时间考虑。这被转换成英国的时候您的计算机上。如果你想一些其他的文化,使用需要一个的IFormatProvider 并指定了一些选择的的CultureInfo 过载。如果你想转换为UTC,而不是转化为您的机器的时候,使用花费过载 DateTimeStyles 枚举,其中包括标志 DateTimeStyles.AdjustToUniversal

To sum up: The offset indicator or zone specifier +0100 is considered when interpreting the time. This is converted into British time on your computer. If you want some other culture, use the overload that takes an IFormatProvider and specify some CultureInfo of your choice. If you want conversion to UTC instead of conversion to the time of your machine, use the overload that takes a DateTimeStyles enum and include the flag DateTimeStyles.AdjustToUniversal.

如果你想有一个值,最好重新presents时间的的绝对区域,可以考虑使用结构的DateTimeOffset 而不是的DateTime 。您也可以考虑.NET类型的NODA时间吧。

If you want a value that better represents time and absolute zone, consider using the struct DateTimeOffset instead of DateTime. You can also consider NODA time instead of the .NET types.

这篇关于解析ISO8601日期/时间与日期时间结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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