日期时间为RFC-1123提供了不准确的时区时间为、不准确、时区、日期

2023-09-05 02:29:20 作者:寂寞太慷慨°

如果我得到一个DateTime对象的RFC-1123格式的日期,它给当前本地时间,但给人的时区为GMT(这是不准确的)。 DateTime.Now.ToString(R); 回报 周五,2010年2月12号16时23分03秒GMT

If I get the RFC-1123 formatted date of a DateTime object, it gives the current local time, but gives the timezone as GMT (which is inaccurate). DateTime.Now.ToString("r"); returns Fri, 12 Feb 2010 16:23:03 GMT

在下午4:23,但我的时区为UTC + 10(加,我们目前正在观察夏令时)。

At 4:23 in the afternoon, but my timezone is UTC+10 (plus, we're currently observing daylight saving time).

现在,我可以得到一个返回值,这是正确通过转换为UTC第一: DateTime.UtcNow.ToString(R); 回报 周五,2010年2月12号五点23分03秒GMT

Now, I can get a return value that's "correct" by converting to UTC first: DateTime.UtcNow.ToString("r"); returns Fri, 12 Feb 2010 05:23:03 GMT

不过,理想情况下,我希望得到正确的时区,这是我的猜测是 周五,2010年2月12号16时23分03秒+1100

However, ideally, I'd like to get the right timezone, which I guess would be Fri, 12 Feb 2010 16:23:03 +1100

在传递当前CultureInfo不会改变任何东西。我能得到一个UTC与TimeZoneInfo.Local.GetUtcOffset(...)的偏移量和格式化从一个时区字符串,但剥出GMT位和更换似乎gratutiously凌乱。

Passing in the current CultureInfo doesn't change anything. I could get a UTC offset with TimeZoneInfo.Local.GetUtcOffset(...) and format a timezone string from that, but stripping out the GMT bit and replacing it seems gratutiously messy.

有没有办法来迫使它包括正确的时区?

Is there a way to force it to include the correct timezone?

推荐答案

在.NET实现总是EX presses时的实际日期偏移的结果就好像它是格林尼治标准​​时间,而不论。

The .NET implementation always expresses the result as if it were GMT, irrespective of the time offset of the actual date.

通过使用 DateTime.Now.ToString(R); 你基本上说的String.Format(DDD,DD MMM YYYY HH:毫米:'SS'GMT',DateTime.Now); ,这是.NET RFC1123格式字符串,如MSDN上指示 - 的的RFC1123(R,R)格式说明。

By using DateTime.Now.ToString("r"); you're essentially saying String.Format("ddd, dd MMM yyyy HH':'mm':'ss 'GMT'", DateTime.Now);, which is the .NET RFC1123 format string, as indicated on MSDN - The RFC1123 ("R", "r") Format Specifier.

要得到你所需要的行为,你应该使用的String.Format ,并更换时间偏移说明的说明的固定GMT部分:

To get the behaviour you require, you should probably use String.Format, and replace the fixed 'GMT' section of the specifier with a time offset specifier:

的Z自定义格式说明 的ZZ自定义格式说明 的ZZZ自定义格式说明 The "z" Custom Format Specifier The "zz" Custom Format Specifier The "zzz" Custom Format Specifier