Skip to content

Commit b516a00

Browse files
authored
Added DateTimeExtensions. (#192)
1 parent 7e7a221 commit b516a00

File tree

4 files changed

+95
-5
lines changed

4 files changed

+95
-5
lines changed

CHANGELOG.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
Nothing yet.
1111

12+
## [1.18.0] - 2024-07-30
13+
14+
### Added
15+
16+
- `DateTimeExtensions`.
17+
18+
### Fixed
19+
20+
- Upgraded NuGet package.
21+
1222
## [1.17.0] - 2024-06-06
1323

1424
### Changed
@@ -211,7 +221,8 @@ Nothing yet.
211221

212222
- Implemented StringExtensions.
213223

214-
[unreleased]: https://github.com/Logitar/Logitar.NET/compare/v1.17.0...HEAD
224+
[unreleased]: https://github.com/Logitar/Logitar.NET/compare/v1.18.0...HEAD
225+
[1.18.0]: https://github.com/Logitar/Logitar.NET/compare/v1.17.0...v1.18.0
215226
[1.17.0]: https://github.com/Logitar/Logitar.NET/compare/v1.16.0...v1.17.0
216227
[1.16.0]: https://github.com/Logitar/Logitar.NET/compare/v1.15.0...v1.16.0
217228
[1.15.0]: https://github.com/Logitar/Logitar.NET/compare/v1.14.0...v1.15.0

src/Logitar/DateTimeExtensions.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace Logitar;
2+
3+
/// <summary>
4+
/// Provides extension methods for <see cref="DateTime"/> instances.
5+
/// </summary>
6+
public static class DateTimeExtensions
7+
{
8+
/// <summary>
9+
/// Converts the specified date and time as universal (UTC).
10+
/// </summary>
11+
/// <param name="value">The date and time to convert.</param>
12+
/// <returns>The date and time as universal (UTC).</returns>
13+
/// <exception cref="ArgumentException">The date time kind is no supported.</exception>
14+
public static DateTime AsUniversalTime(this DateTime value) => value.Kind switch
15+
{
16+
DateTimeKind.Local => value.ToUniversalTime(),
17+
DateTimeKind.Unspecified => DateTime.SpecifyKind(value, DateTimeKind.Utc),
18+
DateTimeKind.Utc => value,
19+
_ => throw new ArgumentException($"The date time kind '{value.Kind}' is not supported.", nameof(value)),
20+
};
21+
22+
/// <summary>
23+
/// Converts the specified date and time as an ISO 8601 string.
24+
/// </summary>
25+
/// <param name="value">The date and time to convert.</param>
26+
/// <returns>The string representation.</returns>
27+
public static string ToISOString(this DateTime value) => value.AsUniversalTime().ToString("O", CultureInfo.InvariantCulture);
28+
}

src/Logitar/Logitar.csproj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
<PackageReadmeFile>README.md</PackageReadmeFile>
1616
<RepositoryType>git</RepositoryType>
1717
<RepositoryUrl>https://github.com/Logitar/Logitar.NET</RepositoryUrl>
18-
<AssemblyVersion>6.2.0.0</AssemblyVersion>
18+
<AssemblyVersion>6.3.0.0</AssemblyVersion>
1919
<FileVersion>$(AssemblyVersion)</FileVersion>
2020
<PackageLicenseFile>LICENSE</PackageLicenseFile>
2121
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
22-
<Version>6.2.0</Version>
22+
<Version>6.3.0</Version>
2323
<NeutralLanguage>en-CA</NeutralLanguage>
2424
<GenerateDocumentationFile>True</GenerateDocumentationFile>
25-
<PackageReleaseNotes>Now targeting .NET Standard 2.1.</PackageReleaseNotes>
25+
<PackageReleaseNotes>Added DateTimeExtensions and upgraded NuGet.</PackageReleaseNotes>
2626
<PackageTags>logitar net framework</PackageTags>
2727
<PackageProjectUrl>https://github.com/Logitar/Logitar.NET/tree/dev/src/Logitar</PackageProjectUrl>
2828
</PropertyGroup>
@@ -36,7 +36,7 @@
3636
</PropertyGroup>
3737

3838
<ItemGroup>
39-
<PackageReference Include="System.Text.Json" Version="8.0.3" />
39+
<PackageReference Include="System.Text.Json" Version="8.0.4" />
4040
</ItemGroup>
4141

4242
<ItemGroup>
@@ -56,6 +56,7 @@
5656

5757
<ItemGroup>
5858
<Using Include="System.Collections" />
59+
<Using Include="System.Globalization" />
5960
<Using Include="System.Text.Json" />
6061
</ItemGroup>
6162

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
namespace Logitar;
2+
3+
[Trait(Traits.Category, Categories.Unit)]
4+
public class DateTimeExtensionsTests
5+
{
6+
[Fact(DisplayName = "AsUniversalTime: it should return a local DateTime as universal (UTC).")]
7+
public void AsUniversalTime_it_should_return_a_local_DateTime_as_universal_UTC()
8+
{
9+
DateTime value = DateTime.Now;
10+
Assert.Equal(value.ToUniversalTime(), value.AsUniversalTime());
11+
}
12+
13+
[Fact(DisplayName = "AsUniversalTime: it should return an unspecified DateTime as universal (UTC) with the same date and time.")]
14+
public void AsUniversalTime_it_should_return_an_unspecified_DateTime_as_universal_UTC_with_the_same_date_and_time()
15+
{
16+
DateTime value = new(2024, 5, 21, 20, 7, 53);
17+
DateTime expected = new(2024, 5, 21, 20, 7, 53, DateTimeKind.Utc);
18+
Assert.Equal(expected, value.AsUniversalTime());
19+
}
20+
21+
[Fact(DisplayName = "AsUniversalTime: it should return the original universal (UTC) DateTime.")]
22+
public void AsUniversalTime_it_should_return_the_original_universal_UTC_DateTime()
23+
{
24+
DateTime value = DateTime.UtcNow;
25+
Assert.Equal(value, value.AsUniversalTime());
26+
}
27+
28+
[Fact(DisplayName = "ToISOString: it should return the correct string representation (local).")]
29+
public void ToISOString_it_should_return_the_correct_string_representation_local()
30+
{
31+
DateTime value = new(2024, 5, 21, 16, 7, 53, DateTimeKind.Local);
32+
string expected = value.ToUniversalTime().ToISOString();
33+
Assert.Equal(expected, value.ToISOString());
34+
}
35+
36+
[Fact(DisplayName = "ToISOString: it should return the correct string representation (unspecified).")]
37+
public void ToISOString_it_should_return_the_correct_string_representation_unspecified()
38+
{
39+
DateTime value = new(2024, 5, 21, 16, 7, 53);
40+
string expected = DateTime.SpecifyKind(value, DateTimeKind.Utc).ToISOString();
41+
Assert.Equal(expected, value.ToISOString());
42+
}
43+
44+
[Fact(DisplayName = "ToISOString: it should return the correct string representation (UTC).")]
45+
public void ToISOString_it_should_return_the_correct_string_representation_UTC()
46+
{
47+
DateTime value = new(2024, 5, 21, 16, 7, 53, DateTimeKind.Utc);
48+
Assert.Equal("2024-05-21T16:07:53.0000000Z", value.ToISOString());
49+
}
50+
}

0 commit comments

Comments
 (0)