14

Suppose I have an ISO 8601 duration, expressed as "P1M". Phrased colloquially, this means "one month." Is there a standard rule for converting this into a number of seconds, assuming the start date is not known?

  • For 30-day months, it might be 2,592,000.
  • For 31-day months, it might be 2,678,400.
  • In February, it might be 2,419,200 or it might be 2,505,600.

My gut says there's no way to resolve "one month" to an exact number of seconds without knowing context, and where those seconds are laid out on the calendar. But are there standard rules/conventions to calculate these durations in an abstract way?

smitelli
  • 6,835
  • 3
  • 31
  • 53

2 Answers2

12

From ISO 8601 documentation that I found (page 6 - http://xml.coverpages.org/ISO-FDIS-8601.pdf), it seems you are correct in that the number of seconds in a month cannot definitively be determined. However it does note that "In certain applications a month is regarded as a unit of time of 30 days", so depending on your application this may be a valid approach.

The distinction between "Calendar Time" (Years, Months, etc) and "Absolute Time" (Hours, Minutes, Seconds, etc) is sometimes an important one. As an example, some people might complain about having 13 mortgage payments some years if they paid every 30 days as opposed to every month.

Jake Griffin
  • 2,014
  • 12
  • 15
6

You are right, an ISO 8601 duration is dependent of the context. A duration is a period/an interval of time between two dates.

Example :

2020-01-01/2020-02-01 = P1M = P31D
2020-02-01/2020-03-01 = P1M = P29D
2019-02-01/2019-03-01 = P1M = P28D

If you want a fixed duration indepedent of the context, use the day notation P30D, P60D, P90D... instead.

The same applies for years :

2019-01-01/2020-01-01 = P1Y = P12M = P365D
2020-01-01/2021-01-01 = P1Y = P12M = P366D

If you can't have context information about a duration, for example P1M retrieved from database or given by user input, use by default today's context.

//What is a duration of one month in seconds ?
P1M = ? (no context)
//Use default context
Today = 2020-03-31
2020-03-31/P1M = 2020-03-31/2020-04-30
=> P1M = P30D
//A month contains 2 592 000 seconds
Baptistou
  • 1,749
  • 1
  • 13
  • 24
  • Wikipedia has an example where even `D` depends on the context. Quote: "PT36H" is not the same as "P1DT12H" when switching from or to Daylight saving time. – Socowi Sep 21 '21 at 14:00
  • Even the number of seconds in a minute depends on the start date, as there are leap seconds resulting in minutes with 61 seconds. – Socowi Sep 21 '21 at 21:00