https://github.com/j0rgeserran0/iso8601duration
ISO 8601 :: C# library to convert a duration pattern in a TimeSpan object, a Custom object or to a Lexical Representation
https://github.com/j0rgeserran0/iso8601duration
8601 csharp duration iso iso8601 netcore2 netframework
Last synced: 4 months ago
JSON representation
ISO 8601 :: C# library to convert a duration pattern in a TimeSpan object, a Custom object or to a Lexical Representation
- Host: GitHub
- URL: https://github.com/j0rgeserran0/iso8601duration
- Owner: J0rgeSerran0
- License: mit
- Created: 2017-12-19T13:34:37.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-11-22T07:25:43.000Z (almost 6 years ago)
- Last Synced: 2025-01-05T08:03:54.060Z (10 months ago)
- Topics: 8601, csharp, duration, iso, iso8601, netcore2, netframework
- Language: C#
- Homepage:
- Size: 20.5 KB
- Stars: 8
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Iso8601Duration - C# Library

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](https://www.w3.org/TR/xmlschema-2/#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)

[Iso8601Duration Converter - v1.0.0](https://www.nuget.org/packages/Iso8601Duration/)
## Structs
- [x] [`DurationStruct`](#durationstruct)
### `DurationStruct`
 **`struct DurationStruct`** - Struct to work with dates and times easily.
```csharp
public int Years;
public int Months;
public int Days;
public int Hours;
public int Minutes;
public int Seconds;
```
## Method's
- [x] [`NormalizeDuration`](#normalizeduration)
- [x] [`ToDurationStruct`](#todurationstruct)
- [x] [`ToString`](#tostring)
- [x] [`ToTimeSpan`](#totimespan)
### `NormalizeDuration`
 **`string NormalizeDuration(string pattern)`** - Converts a general pattern in a correct and a well formed pattern.
```csharp
// "PT61S" to "PT1M1S"
var periodBuilder = new PeriodBuilder();
var pattern = "PT61S";
var wellFormedPattern = periodBuilder.NormalizeDuration(inputPattern);
```
### `ToDurationStruct`
 **`DurationStruct ToDurationStruct(TimeSpan timeSpan)`** - Converts from TimeSpan to DurationStruct.
```csharp
var periodBuilder = new PeriodBuilder();
var durationStruct = periodBuilder.ToDurationStruct(new TimeSpan(0, 0, 120));
```
 **`DurationStruct ToDurationStruct(string pattern)`** - Converts from TimeSpan to DurationStruct.
```csharp
var periodBuilder = new PeriodBuilder();
var durationStruct = periodBuilder.ToDurationStruct("P1Y");
```
### `ToString`
 **`string ToString(TimeSpan timeSpan)`** - Converts from TimeSpan to Duration Pattern.
```csharp
var periodBuilder = new PeriodBuilder();
var durationStruct = periodBuilder.ToString(new TimeSpan(0, 120, 0));
```
 **`string ToString(DurationStruct durationStruct)`** - Converts from DurationStruct to Duration Pattern.
```csharp
var periodBuilder = new PeriodBuilder();
var durationStruct = periodBuilder.ToString("PT2H");
```
### `ToTimeSpan`
 **`TimeSpan ToTimeSpan(string pattern)`** - Converts from Duration Pattern to TimeSpan.
```csharp
var periodBuilder = new PeriodBuilder();
var durationStruct = periodBuilder.ToTimeSpan("PT2H");
```
 **`TimeSpan ToTimeSpan(DurationStruct durationStruct)`** - Converts from DurationStruct to TimeSpan.
```csharp
var periodBuilder = new PeriodBuilder();
var durationStruct = periodBuilder.ToTimeSpan(new DurationStruct() { Minutes = 1440 });
```