Java “PT”前缀在 Duration 中代表什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51168022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:59:06  来源:igfitidea点击:

What does 'PT' prefix stand for in Duration?

javadurationjava-timeiso8601period

提问by Daneel S. Yaitskov

I am trying to use the Durationclass instead of long. It has superior literal syntax. I like its flexibility, though it looks weird.

我正在尝试使用Duration该类而不是long. 它具有优越的文字语法。我喜欢它的灵活性,虽然它看起来很奇怪。

"PT10S" means 10 seconds, what is the problem to accept "10 seconds"?! Okay never mind.

“PT10S”表示10秒,接受“10秒”有什么问题?!好的没关系。

I am just curious why PT prefix has been chosen (not "DU" e.g.) and why any prefix is better here rather than nothing?

我只是好奇为什么选择了 PT 前缀(而不是“DU”),为什么这里的任何前缀都比什么都好?

采纳答案by RobAu

As can be found on the page Jesper linked to (ISO-8601 - Data elements and interchange formats – Information interchange – Representation of dates and times)

可以在 Jesper 链接到的页面上找到(ISO-8601 - 数据元素和交换格式 - 信息交换 - 日期和时间的表示

P is the duration designator (for period) placed at the start of the duration representation.
Y is the year designator that follows the value for the number of years.
M is the month designator that follows the value for the number of months.
W is the week designator that follows the value for the number of weeks.
D is the day designator that follows the value for the number of days.
T is the time designator that precedes the time components of the representation.

So P means 'Period' and because there are no date-components it only has a 'Time'.

所以 P 的意思是“期间”,因为没有日期组件,所以它只有一个“时间”。

You could interpret this as 'Period of Time'

您可以将其解释为“时间段”

The 'why' this was chosen, you have to ask the ISO members that wrote the standard, but my guess is that it is easier to parse. (short and unambigious)

选择“为什么”,您必须询问编写标准的 ISO 成员,但我的猜测是它更容易解析。(简短而明确)

回答by Ole V.V.

Java has taken a subset of the ISO 8601 standard format for a duration. So the “why” is why the standard was written the way it is, and it's a guessing game. My go is:

Java 在一段时间内采用了 ISO 8601 标准格式的一个子集。所以“为什么”就是为什么标准是这样编写的,这是一个猜谜游戏。我的做法是:

  • Pfor period was chosen so that you can distinguish a duration from a date and/or time. Especially since a period may also be written in the same format as a local date-time, for example P0003-06-04T12:30:05for 3 years 6 months 4 days 12 hours 30 minutes 5 seconds, the Pcan be necessary to distinguish. The Palso gives a little but quick and convenient bit of validation in case you happen to pass a completely different string in a place where a duration was expected. And yes, PT10Slooks weird, but once you get accustomed to it, you recognize it immediately as a duration, which can be practical.
  • Tfor time between the date part and the time part was chosen for two reasons:
    • For consistency with date-time strings that have Tin the same place, for example 2018-07-04T15:00for July 4, 2018 at 15:00 hours.
    • To disambiguate the otherwise ambiguous Mfor either months or minutes: P3Munambiguously means 3 months while PT3Mmeans 3 minutes.
  • P选择 for period 以便您可以将持续时间与日期和/或时间区分开来。特别是因为一个句点也可能以与本地日期时间相同的格式书写,例如P0003-06-04T12:30:053 年 6 个月 4 天 12 小时 30 分 5 秒,所以P可能需要区分。该P还提供了一点点,但如果你碰巧经过一个完全不同的字符串在持续时间预期的地方验证快速,便捷位。是的,PT10S看起来很奇怪,但是一旦您习惯了它,您就会立即将其识别为一个持续时间,这很实用。
  • T选择日期部分和时间部分之间的时间有两个原因:
    • 为了与T同一位置的日期时间字符串保持一致,例如2018-07-04T15:002018 年 7 月 4 日 15:00。
    • M几个月或几分钟消除歧义:P3M明确表示 3 个月,而PT3M表示 3 分钟。