Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Representing 40,000,000 seconds as an ISO-8601 period

Tags:

iso8601

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?

like image 692
Paul Beckingham Avatar asked Dec 14 '13 05:12

Paul Beckingham


People also ask

How do I specify the duration in an ISO 8601 format?

Briefly, the ISO 8601 notation consists of a P character, followed by years, months, weeks, and days, followed by a T character, followed by hours, minutes, and seconds with a decimal part, each with a single-letter suffix that indicates the unit. Any zero components may be omitted.

What is Z in ISO 8601 date format?

Coordinated Universal Time (UTC) Z is the zone designator for the zero UTC offset. "09:30 UTC" is therefore represented as "09:30Z" or "T0930Z".

What time zone is ISO 8601?

Universal Coordinate Time is the time at the zero meridian, near Greenwich, England. UTC is a datetime value that uses the ISO 8601 basic form yyyymmddT hhmmss+|– hhmm or the ISO 8601 extended form yyyy-mm-ddT hh:mm:ss+|– hh:mm.


1 Answers

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.

like image 50
G.Y Avatar answered Oct 02 '22 14:10

G.Y