Question

40,000,000 seconds is 462.962 days. If I wanted to represent that as an ISO-8601 period, in this form:

PnYnMnDTnHnMnS

What are the rules for determining months and years? Neither are precise terms, and the standard says that it depends on the start date of the period.

This makes sense: it makes the period ambiguous without a start date. Are there any accepted means of using a period without a start date?

Was it helpful?

Solution

Ok, let's put some order:

The standard support both types that is: Dates as a specific point in time since year 0000 and until year 9999.
Or Durations which may describe a timespan between two dates or an arbiter duration/timespan which is not between two specific dates. Luckily when you specify durations the standard is much more flexible than with dates :)

So how can we tell the difference between a duration which belong between dates and an arbiter duration?
Simple: if you specified "P1Y" which is one year - that is exactly what you got.
If you want to calculate how much this 1 year has in days - that will be depended on the starting date you are applying this one year upon.
In example, if say the start date is 1/1/2010 (So February has 28 days which is normal) your 1 year value in calculation will yield 365 days. But if say the start date is 1/1/2012 (So February has 29 days due to leap year) your 1 year value will yield 366 days in it...

But in both cases you wanted 1 year - and you got 1 year. Therefore the 1st thing is not to use years in duration in your case since what you really want is to specify the equivalent of 40000000 in seconds - just shortly... so our next alternative is to specify it in days and the .962 as a part of one day:

40000000 seconds is 462 days, 23 hours, 6 minutes and 40 seconds and according to what I read in the ISO-8601 this can be specified as duration like that: "P462DT23H6M40S" or you can simply specify "PT40000000S" or even "P462.962D" - all goes in duration.

To ease the pain of calculating you can use this very simple C# Line:
var timespan = new TimeSpan(0, 0, 40000000);
Simply put a breakpoint after it and look at the internals the Timespan class calculated for you.
Putting those into ISO-8601 format is rather easy after that point.

Oh and last thing - yes you can specify fractions too - such as "P0.5Y" to specify half year.

OTHER TIPS

As I read (freely available documentation for) the standard, the answer to the first question

What are the rules for determining months and years?

...is that there are no rules, because (as you have noted) it is not possible without a start date/time or duration. Therefore, context is required.

As for your second question,

Are there any accepted means of using a period without a start date?

Absolutely: See Unix Time. In Unix time, 40,000,000 seconds would be represented as 40000000.

Pretty easy conversion, I hardly even have to use the calculator! ;)

Edit:

An (imperfect) analogy:

Using the metric system only, how far is it to Chicago from London? What does the metric system define as the best/most accurate method to represent this distance?

Well, the metric system defines units of length/distance that we can use: meters.

The problem is that the metric system simply doesn't address the variables involved. Are we taking into account the curvature of the Earth? Is this a geometrically straight line, is this "as the crow flies", is this using travel distance as determined by available public routes and known flight paths?

The answer is that there is no way to do it without more context.

As mentioned in the other answer by G.Y., not all years are equal number days. The same is true if you are converting to days - not all days are 24 hours. The place this most often occurs are during periods that cross daylight savings time boundaries making the time period off by an hour (of course if it crosses an even number of boundaries, the errors cancel).

The second place this can occur is when the period crosses the end of month boundary because you can gain or lose a leap second. Again, this seldom occurs in most business applications and is usually ignored, but it can be an issue if you're coordinating multiple systems that are synchronized to world clocks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top