views:

67

answers:

3

Is there a built-in way in .NET 3.5 to convert an ISO 8601 period into a .NET TimeSpan / ticks / miliseconds?

+1  A: 

There is nothing directly built into .NET unfortunately.

And there is no TimeSpan.ParseExact before .NET 4 which could be used if the components of the string are known.

Richard
flagged answered for being the first to answer.
yas4891
+1  A: 

Even if there is nothing directly in the .Net framework i found that Joda.org has implemented such a thing and i remembered that Jon did a port of it to .Net called noda-time.

A first view into this source code shows that he implemented something here for this purpose. Maybe you should do some further investigations in this project.

Oliver
Thanks for that link. quite impressive work done there
yas4891
+1  A: 

There is no built-in way. To further complicate things ISO 8601 durations is in fact totally incompatible with TimeSpan or any other exact way to measure time. The reason is that ISO 8601 durations can contain years and months. The problem with that is that a year can have 365 or 366 days and a month 28 to 31 days. Joda-Time solves this problem by relating such inexact periods to an instant in time.

Unless you are lucky and the Periods happen to use the P[YYYY]-[MM]-[DD]T[hh]:[mm]:[ss] format because then you just:

string period = "P0003-06-04T12:30:05";
TimeSpan span = new TimeSpan(DateTime.Parse(period.Remove(0,1)).Ticks);
Jonas Elfström
For my specific task this should normally be something like "PT1S" or "PT0.05999999999S", so parsing isn't a real problem. Just wanted to get a good solution for it. Thanks for your answer!
yas4891