是否有日期/时间间隔格式的名称,例如“ 1h10m”? [英] Is there a name for date/time interval format like "1h10m"

查看:379
本文介绍了是否有日期/时间间隔格式的名称,例如“ 1h10m”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使在软件外部,以截断的方式交流时间或日期间隔也是很平常的事。例如: 1h10m 表示为一个小时零十分钟。

It's commonplace even outside of software to communicate time or date intervals in a truncated manner. For example: 1h10m translates to "One hour and ten minutes."

这可以抽象为一组规则。例如:日期间隔表示为 _h _m (等等)的组合,其中 _ 字符代表非负整数或浮点数,它们被加到一个日期间隔对象中。

This could be abstracted to a set of rules. For instance: A date interval is represented as a combination of _h, _m, (and so on), where _ characters represent non-negative integers or floats, which are summed into one date interval object.

混合天数,小时,允许分钟。例如, 0.5d1h60m 将是 14h 的同义词。

Mixing days, hours, minutes are allowed. For example, 0.5d1h60m would be a synonym for 14h.

在任何地方都有类似的标准吗?

Is there a standard defined out there anywhere that resembles this?

推荐答案

此标准是持续时间,由 ISO 8601 定义。
请注意,时间间隔是一个不同的概念(也由相同的ISO定义),尽管两者密切相关:

The standard for this is a Duration, defined by ISO 8601. Note that an Interval is a different concept (also defined by the same ISO), although both are closely related:


  • 持续时间定义了一个时间时间(例如 1小时10分钟或 2年3个月4天)。但它不会告诉您开始或结束的时间(相对于什么时间是 1小时10分钟)。

  • 间隔(引用维基百科)是两个时间点之间的间隔时间 。它具有定义的开始日期和结束日期,但是您可以使用 Duration 进行定义,因为它可以有4种不同的格式:

  • A Duration defines an amount of time (like "1 hour and 10 minutes" or "2 years, 3 months and 4 days"). But it doesn't tell you when it starts or ends ("1 hours and 10 minutes" relative to what?). It's just the amount of time, by itself.
  • An Interval (quoting wikipedia) is "the intervening time between two time points". It has a defined start and end dates, but you can use a Duration to define it, as it can have 4 different formats:

  1. 开始和结束,例如 2007-03-01T13:00:00Z / 2008-05-11T15:30:00Z

  2. 开始和持续时间,例如 2007-03-01T13:00:00Z / P1Y2M10DT2H30M

  3. 持续时间和结束时间,例如 P1Y2M10DT2H30M / 2008-05-11T15:30:00Z

  4. 仅持续时间,例如 P1Y2M10DT2H30M 以及其他上下文信息

  1. Start and end, such as 2007-03-01T13:00:00Z/2008-05-11T15:30:00Z
  2. Start and duration, such as 2007-03-01T13:00:00Z/P1Y2M10DT2H30M
  3. Duration and end, such as P1Y2M10DT2H30M/2008-05-11T15:30:00Z
  4. Duration only, such as P1Y2M10DT2H30M, with additional context information


案例1、2和3是等效的(都具有相同的开始和结束日期)。唯一的区别是,在情况2和3中,持续时间 P1Y2M10DT2H30M 用于计算另一个日期(在情况2中,将其添加到开始日期, 3您将其从结束日期中减去)。

Cases 1, 2 and 3 are equivalent (all have the same start and end dates). The only difference is that in cases 2 and 3, the duration P1Y2M10DT2H30M is used to calculate the other date (in case 2, you add it to the start date, and in case 3 you subtract it from the end date).

如上所示, 持续时间的标准格式为 P [n] Y [n] M [n] DT [n] H [n] M [n] S ,其中:

As you can notice above, the standard format for a Duration is P[n]Y[n]M[n]DT[n]H[n]M[n]S, where:


  • P是放置在持续时间表示开始处的持续时间指示符(用于周期)。

  • Y是遵循年份值的年份指示符。

  • M是遵循月份值的月份指示符。

  • W是遵循周数的星期指示符。

  • D是遵循天数的星期指示符。 / li>
  • T是表示形式的时间成分之前的时间指示符。

  • H是表示时间的指示符跟随小时数的值。

  • M是跟随分钟数的值的分钟指示符。

  • S是第二个

  • 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.
  • H is the hour designator that follows the value for the number of hours.
  • M is the minute designator that follows the value for the number of minutes.
  • S is the second designator that follows the value for the number of seconds.

因此, 1年10个月表示为 P1Y10M 和 1小时10分钟是 PT1H10M (请注意 T 需要解决1个月( P1M )和1分钟( PT1M )之间的潜在歧义与指定人相同的字母 M )。

So, "1 year and 10 months" is represented as P1Y10M and "1 hour and 10 minutes" is PT1H10M (note that the T is required to resolve the potencial ambiguity between 1 month (P1M) and 1 minute (PT1M), as they use the same letter M as designator).

与@MattJohnson评论,日期的数学并不总是很明显,因此不同持续时间之间的等效性不是我们通常期望的。

As @MattJohnson commented, the math with dates it's not always obvious, so the equivalence between different durations can't be what we normally expect.

对于下面的示例,我正在使用Java 8(只是为了展示持续时间是多么棘手)。请注意, java.time API使用2个不同的类( Period Duration ),但两者的想法是相同的(它们都是时间量)。

For the examples below, I'm using Java 8 (just to show how durations can be tricky). Note that the java.time API uses 2 different classes (Period and Duration), but the idea for both is the same (they're both amounts of time).


  • A持续时间1个月等于多少天?这取决于:

  • A duration of 1 month is equivalent to how many days? It depends:

// one month period
Period oneMonth = Period.parse("P1M");
// January 1st
LocalDate jan = LocalDate.of(2016, 1, 1);
System.out.println(jan); // 2016-01-01
// January 1st plus 1 month period = February 1st
LocalDate feb = jan.plus(oneMonth);
System.out.println(feb); // 2016-02-01
// February 1st plus 1 month period = March 1st
LocalDate mar = feb.plus(oneMonth);
System.out.println(mar); // 2016-03-01

// difference between Jan 1st and Feb 1st = 31 days
System.out.println(ChronoUnit.DAYS.between(jan, feb)); // 31
// difference between Feb 1st and Mar 1st = 29 days (2016 is leap year)
System.out.println(ChronoUnit.DAYS.between(feb, mar)); // 29


因此,将1个月添加到 1月1日 st 会导致 2月1日 st -在这种情况下,1个月等于31天(又名添加1个月的持续时间( P1M )等同于添加31天的持续时间( P31D ),然后在2月1日 st 中加上1个月,就会导致 3月1日 st (在本例中为1月= 29天,因为2016年是a年。)

So, adding 1 month to January 1st results in February 1st - in this case, 1 month is equivalent 31 days (A.K.A. adding a 1 month duration (P1M) is equivalent to adding a 31 days duration (P31D)), and adding 1 month to February 1st results in March 1st (in this case, 1 month = 29 days, because 2016 is a leap year).


  • 1天= 24小时?不总是。如果涉及到夏令时转换,您会得到奇怪的结果:

  • 1 day = 24 hours? Not always. If there's a Daylight Saving Time shift involved, you can get strange results:

// 1 day period
Period oneDay = Period.parse("P1D");
// 24 hours period
Duration twentyFourHours = Duration.parse("PT24H");
// in Sao Paulo, summer time starts at Oct 15, at midnight
// getting a date one day before DST change, at 10:00 AM
ZonedDateTime z = ZonedDateTime.of(2017, 10, 14, 10, 0, 0, 0, ZoneId.of("America/Sao_Paulo"));
System.out.println(z); // 2017-10-14T10:00-03:00[America/Sao_Paulo]

// add 1 day - gets the same hour (10:00 AM)
System.out.println(z.plus(oneDay)); // 2017-10-15T10:00-02:00[America/Sao_Paulo]

// add 24 hours - gets 11:00 AM because of DST shift (at midnight, clocks moved forward 1 hour)
System.out.println(z.plus(twentyFourHours)); // 2017-10-15T11:00-02:00[America/Sao_Paulo]


在圣保罗,2017年10月15日 ,DST开始(时钟向前移动1小时),因此:

In São Paulo, at October 15th, 2017, DST starts (clocks are moved forward by 1 hour), so:


  • 如果您将 24小时添加到10月14日 10点,您会在 11 AM

  • 10月15日 th 1天,您将在 10 AM

  • If you add 24 hours to October 14th at 10 AM, you'll get October 15th at 11 AM
  • But if you add 1 day, you'll get October 15th at 10 AM

因此,在这种情况下,1天= 23小时-这意味着添加1天的持续时间( P1D )等同于增加23小时的持续时间( PT23H

So, in this case, 1 day = 23 hours - it means that adding a 1 day duration (P1D) is equivalent to adding a 23 hours duration (PT23H)

DST结束后,相反:时钟向后移动1个小时,而1天等于25个小时。

When DST ends, is the opposite: clocks move back 1 hour, and 1 day will be equivalent to 25 hours.

因此,该标准定义了时间概念的格式和含义,但是不同持续时间之间的等价性取决于c ontext(尽管听起来1天不一定总是24小时,但是日期/时间数学并不像我们想要的那么明显,这听起来似乎不是直觉。)

So, the standard defines the format and meaning of the amounts of time concepts, but the equivalence between different durations will depend on the context (although it might sound non-intuitive that 1 day is not always 24 hours, but date/time math is not as obvious as we'd like).

这篇关于是否有日期/时间间隔格式的名称,例如“ 1h10m”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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