c# - 为什么 ISO-8601 规范在涉及小数时似乎被普遍忽略?

标签 c# javascript php datetime iso8601

<分区>

来自 ISO-8601:2004(E) 规范:

4.2.2.4 Representations with decimal fraction

If necessary for a particular application a decimal fraction of hour, minute or second may be included. If a decimal fraction is included, lower order time elements (if any) shall be omitted and the decimal fraction shall be divided from the integer part by the decimal sign specified in ISO 31-0, i.e. the comma [,] or full stop [.]. Of these, the comma is the preferred sign.

很简单。所以根据这个规范,秒的小数部分最好用逗号分隔整数和小数部分,例如 2014-01-01T00:00:00,123。然而,似乎几乎所有地方都只接受小数点(又名“句号”)!

现在我确定有一些语言或库考虑到了这一点,而且我知道在很多情况下您可以自己提供格式的完整详细信息。但这似乎是对规范的明显疏忽,似乎很多程序员都犯了同样的错误。 除了纯粹的人为错误之外,还有其他原因吗?

下面是我测试过的列表。如果您发现任何其他问题,请随时编辑问题以扩充我的列表。谢谢。

.NET/C#

DateTime dt = DateTime.Parse("2014-01-01T00:00:00,123");

抛出一个 FormatException 消息“String was not recognized as a valid DateTime”。使用句点而不是逗号的相同内容解析成功。

JavaScript 日期对象

在最新的(截至撰写本文时)Chrome、Internet Explorer、Firefox 和 Node.js 中测试:

var dt = new Date('2014-01-01T00:00:00,123');

返回 “无效日期”。改用句点效果很好。

JavaScript moment.js

var valid = moment("2014-01-01T00:00:00,123").isValid();

返回 false。使用句点代替返回 true

PHP

echo strtotime('2014-01-01T00:00:00,123');

返回一个空字符串。改用句点效果很好。

ruby

require 'time'
puts Time.iso8601("2014-01-01T00:00:00,123")

给出一个运行时错误。虽然 Time 不保留小数秒,但它不应该出错 - 事实上,如果使用句点代替,它会起作用。

最佳答案

RFC3339 ,由 IETF 定义仅指定 . 作为分隔符。

这是 section 5.6 :

5.6. Internet Date/Time Format

   The following profile of ISO 8601 [ISO8601] dates SHOULD be used in
   new protocols on the Internet.  This is specified using the syntax
   description notation defined in [ABNF].

   date-fullyear   = 4DIGIT
   date-month      = 2DIGIT  ; 01-12
   date-mday       = 2DIGIT  ; 01-28, 01-29, 01-30, 01-31 based on
                             ; month/year
   time-hour       = 2DIGIT  ; 00-23
   time-minute     = 2DIGIT  ; 00-59
   time-second     = 2DIGIT  ; 00-58, 00-59, 00-60 based on leap second
                             ; rules
   time-secfrac    = "." 1*DIGIT
   time-numoffset  = ("+" / "-") time-hour ":" time-minute
   time-offset     = "Z" / time-numoffset

   partial-time    = time-hour ":" time-minute ":" time-second
                     [time-secfrac]
   full-date       = date-fullyear "-" date-month "-" date-mday
   full-time       = partial-time time-offset

   date-time       = full-date "T" full-time

关于c# - 为什么 ISO-8601 规范在涉及小数时似乎被普遍忽略?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20699705/

相关文章:

javascript无法使用xul的oncommand?

Php/MYSQl 输出数组的每个值查询的第一个和最后一个结果?

php - 我该如何让我的 friend 的 friend 使用 mysql 和 PHP

c# - 为什么我们要将 ClassInterface 属性应用于类?

c# - 如何在 WindowsPhone/Windows 8.1 上按下 Enter 键时隐藏软键盘?

C#/winforms : ItemCheckEventHandler after new checkstate has been applied

javascript - 如何将 DataTables 按钮定位在表格包装器之外

javascript - 为什么表格仍然提交?

c# - 系统.Data.SqlClient.SqlException : Column name or number of supplied values does not match table definition

php - MySQL 'AND' 给我错误