是"UTC"吗?RFC 1123规范的有效时区名称? [英] Is "UTC" a valid timezone name for RFC 1123 specification?

查看:86
本文介绍了是"UTC"吗?RFC 1123规范的有效时区名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用API​​,它要求RFC 1123日期.如果我发送的日期为 Thu,04 Sep 2014 06:42:22 UTC 的请求,则由于"UTC" 部分而被拒绝.如果我操作该字符串并将时区部分设置为"GMT" ,则它可以正常工作.

I am working against an API and it requests RFC 1123 dates. If I send requests with a date like Thu, 04 Sep 2014 06:42:22 UTC it gets rejected because of the "UTC" part. If I manipulate the string and make the timezone part "GMT" it works.

我还注意到,许多语言(Java,C#,Python)正在使用时区字符串"GMT" 以RFC 1123格式格式化UTC日期,但是我使用的语言Go作为"UTC" .

I also noticed that many languages (Java, C#, Python) are formatting the UTC dates in RFC 1123 format with timezone string "GMT" however the language I'm using, Go, is keeping it as "UTC".

我试图了解它是否是语言错误,或者每个RFC都不应该使用"UTC" :

I'm trying to understand if it's a language bug or "UTC" should not be used at all per RFC: http://www.ietf.org/rfc/rfc1123.txt

推荐答案

简短回答

使用 UT GMT ,但不使用 UTC .

我对 RFC 1123 的阅读是,它采用RFC 822作为日期时间格式,但调整.

My reading of RFC 1123 is that it adopts RFC 822 for date-time formats except for tweaks.

RFC 822 不包含 UTC 一词.相反,第25页提到 UT GMT 的同义词.因此,您也许可以使用 UT ,但不能使用 UTC .

RFC 822 does not contain the word UTC. Instead page 25 refers to UT being a synonym for GMT. So you may be able to use UT but not UTC.

此外,RFC 822被 RFC 2822 取代(是,可爱的编号).该规范在定义时区偏移时仅提及一次UTC.但是此规范并未以格式将 UTC 添加为标识符.同样的规则,请在字符串中仅使用 GMT UT .

In addition, RFC 822 was superseded by RFC 2822 (yes, cute numbering). This spec only mentions UTC once, in defining time zone offsets. But this spec does not add UTC as an identifier in the format. So same rule, use only GMT or UT in your strings.

RFC 822和1123定义的这种格式很糟糕.难以阅读,难以解析,假设英语语言和文化,错误地沿错误的方向定义了军事时区,错误地将UT与GMT等同,指的是世界时,因为存在多种类型的UTC与UT,因此模棱两可一种,并鼓励在既非标准化也不独特的时区使用3或4个字母代码的错误做法.

This format defined by RFC 822 and 1123 is terrible. It is difficult to read, difficult to parse, assumes English language and culture, incorrectly defines the military time zones in the wrong direction, incorrectly equates UT with GMT, refers to Universal Time which is ambiguous as there are multiple kinds of UT with UTC being one, and encourages the bad practice of using the 3 or 4 letter codes for time zones which are neither standardized nor unique.

尽可能使用 ISO 8601 代替,如

If at all possible, use ISO 8601 instead, as discussed here by Jukka "Yucca" Korpela. This standard was adopted by ISO a few years after RFC 822 was published. ISO 8601 is far more sensible, and is being adopted in modern protocols and in business/industry.

ISO 8601格式的示例:

Examples of ISO 8601 format:

2014-09-06T13:35:58-02:00
2014-09-06T15:35:58Z

编程

如果与Go捆绑在一起的库针对该格式错误地生成了 UTC ,建议您提交错误报告.

Programming

If a library bundled with Go is generating UTC incorrectly for that format, I suggest you file a bug report.

FYI,Java包括行业领先的日期时间框架 java.time 类.这些类内置于Java 8及更高版本中,反向移植到Java 6&7 到Android .在 OpenJDK 项目中以开源形式提供.

FYI, Java includes an industry-leading date-time framework, the java.time classes. These classes are built into Java 8 and later, back-ported to Java 6 & 7 and to Android. Available as open-source in the OpenJDK project.

在解析/生成字符串表示时,默认情况下,java.time类使用 ISO 8601 格式它们的日期时间值.

The java.time classes use ISO 8601 formats by default when parsing/generating strings to represent their date-time values.

此外, DateTimeFormatter 类提供了专门用于 http://tools的格式化程序.ietf.org/html/rfc1123 请参见此示例在IdeOne.com中直播的代码.

Instant instant = Instant.now() ; // Current moment in UTC.
OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ; // A wrapper around Instant, more flexible such as formatting.
DateTimeFormatter f = DateTimeFormatter.RFC_1123_DATE_TIME ;
String output = odt.format( f ) ;

instant.toString():2016-11-15T21:12:49.261Z

instant.toString(): 2016-11-15T21:12:49.261Z

odt.toString():2016-11-15T21:12:49.261Z

odt.toString(): 2016-11-15T21:12:49.261Z

输出:星期二,2016年11月15日格林尼治标准时间

output: Tue, 15 Nov 2016 21:12:49 GMT


关于 java.time

java.time 框架内置于Java 8及更高版本中.这些类取代了麻烦的旧旧版日期时间类,例如 Calendar ,& SimpleDateFormat .


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

Joda-Time 项目,现在位于维护模式,建议迁移到要了解更多信息,请参见 Oracle教程 .并在Stack Overflow中搜索许多示例和说明.规范为 JSR 310 .

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

您可以直接与数据库交换 java.time 对象.使用与

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

在哪里获取java.time类?

Where to obtain the java.time classes?

  • Java SE 8 Java SE 11 ,以及更高版本-带有捆绑实现的标准Java API的一部分.
    • Java 9添加了一些次要功能和修复.
    • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

    > ThreeTen-Extra 项目通过以下方式扩展了java.time其他课程.该项目为将来可能在java.time中添加内容提供了一个试验场.您可能会在这里找到一些有用的类,例如 Interval YearWeek YearQuarter 更多.

    这篇关于是"UTC"吗?RFC 1123规范的有效时区名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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