Skip to content

ISO 8601 :: C# library to convert a duration pattern in a TimeSpan object, a Custom object or to a Lexical Representation

License

Notifications You must be signed in to change notification settings

J0rgeSerran0/Iso8601Duration

Repository files navigation

Iso8601Duration - C# Library

C#

ISO 8601 :: C# library that converts Duration Patterns from the ISO 8601 and resolves limitations with the convertion with System.Xml.

You can find more information about the ISO 8601 and the lexical reprentation in the next link. ISO 8601 - Lexical Representation - Duration

The format of a lexical representation is:

PnYnMnDTnHnMnS

NuGet - Iso8601Duration

(Compatible for .NET Framework v4.6.1 or greater, and .NET Core 2.0 or greater)

NuGet

Iso8601Duration Converter - v1.0.0

Structs

DurationStruct

Separator struct DurationStruct - Struct to work with dates and times easily.

public int Years;
public int Months;
public int Days;
public int Hours;
public int Minutes;
public int Seconds;

Method's

NormalizeDuration

Separator string NormalizeDuration(string pattern) - Converts a general pattern in a correct and a well formed pattern.

// "PT61S" to "PT1M1S"
var periodBuilder = new PeriodBuilder();
var pattern = "PT61S";
var wellFormedPattern = periodBuilder.NormalizeDuration(inputPattern);

ToDurationStruct

Separator DurationStruct ToDurationStruct(TimeSpan timeSpan) - Converts from TimeSpan to DurationStruct.

var periodBuilder = new PeriodBuilder();
var durationStruct = periodBuilder.ToDurationStruct(new TimeSpan(0, 0, 120));

Separator DurationStruct ToDurationStruct(string pattern) - Converts from TimeSpan to DurationStruct.

var periodBuilder = new PeriodBuilder();
var durationStruct = periodBuilder.ToDurationStruct("P1Y");

ToString

Separator string ToString(TimeSpan timeSpan) - Converts from TimeSpan to Duration Pattern.

var periodBuilder = new PeriodBuilder();
var durationStruct = periodBuilder.ToString(new TimeSpan(0, 120, 0));

Separator string ToString(DurationStruct durationStruct) - Converts from DurationStruct to Duration Pattern.

var periodBuilder = new PeriodBuilder();
var durationStruct = periodBuilder.ToString("PT2H");

ToTimeSpan

Separator TimeSpan ToTimeSpan(string pattern) - Converts from Duration Pattern to TimeSpan.

var periodBuilder = new PeriodBuilder();
var durationStruct = periodBuilder.ToTimeSpan("PT2H");

Separator TimeSpan ToTimeSpan(DurationStruct durationStruct) - Converts from DurationStruct to TimeSpan.

var periodBuilder = new PeriodBuilder();
var durationStruct = periodBuilder.ToTimeSpan(new DurationStruct() { Minutes = 1440 });

About

ISO 8601 :: C# library to convert a duration pattern in a TimeSpan object, a Custom object or to a Lexical Representation

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages