Skip to content

feature/datetime-extensions #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<NeutralLanguage>en</NeutralLanguage>
<Copyright>Copyright © ONIXLabs 2020</Copyright>
<RepositoryUrl>https://github.com/onix-labs/onixlabs-dotnet</RepositoryUrl>
<Version>10.0.0</Version>
<PackageVersion>10.0.0</PackageVersion>
<AssemblyVersion>10.0.0</AssemblyVersion>
<Version>10.1.0</Version>
<PackageVersion>10.1.0</PackageVersion>
<AssemblyVersion>10.1.0</AssemblyVersion>
</PropertyGroup>
</Project>
49 changes: 49 additions & 0 deletions OnixLabs.Core.UnitTests/DateTimeExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2020 ONIXLabs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using Xunit;

namespace OnixLabs.Core.UnitTests;

public sealed class DateTimeExtensionTests
{
[Theory(DisplayName = "ToDateOnly should produce the expected result")]
[InlineData(1, 1, 1, 0, 0, 0)]
[InlineData(1999, 12, 12, 12, 30, 31)]
public void ToDateOnlyShouldProduceExpectedResult(int year, int month, int day, int hour, int minute, int second)
{
// When
DateTime value = new(year, month, day, hour, minute, second);
DateOnly expected = DateOnly.FromDateTime(value);
DateOnly actual = value.ToDateOnly();

// Then
Assert.Equal(expected, actual);
}

[Theory(DisplayName = "ToTimeOnly should produce the expected result")]
[InlineData(1, 1, 1, 0, 0, 0)]
[InlineData(1999, 12, 12, 12, 30, 31)]
public void ToTimeOnlyShouldProduceExpectedResult(int year, int month, int day, int hour, int minute, int second)
{
// When
DateTime value = new(year, month, day, hour, minute, second);
TimeOnly expected = TimeOnly.FromDateTime(value);
TimeOnly actual = value.ToTimeOnly();

// Then
Assert.Equal(expected, actual);
}
}
39 changes: 39 additions & 0 deletions OnixLabs.Core/Extensions.DateTime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2020-2024 ONIXLabs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.ComponentModel;

namespace OnixLabs.Core;

/// <summary>
/// Provides extension methods for <see cref="DateTime"/> instances.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class DateTimeExtensions
{
/// <summary>
/// Obtains a <see cref="DateOnly"/> instance from the current <see cref="DateTime"/> instance.
/// </summary>
/// <param name="value">The <see cref="DateTime"/> instance from which to obtain a <see cref="DateOnly"/> instance.</param>
/// <returns>Returns the <see cref="DateOnly"/> instance obtained from the current <see cref="DateTime"/> instance.</returns>
public static DateOnly ToDateOnly(this DateTime value) => DateOnly.FromDateTime(value);

/// <summary>
/// Obtains a <see cref="TimeOnly"/> instance from the current <see cref="TimeOnly"/> instance.
/// </summary>
/// <param name="value">The <see cref="DateTime"/> instance from which to obtain a <see cref="TimeOnly"/> instance.</param>
/// <returns>Returns the <see cref="TimeOnly"/> instance obtained from the current <see cref="DateTime"/> instance.</returns>
public static TimeOnly ToTimeOnly(this DateTime value) => TimeOnly.FromDateTime(value);
}
2 changes: 1 addition & 1 deletion OnixLabs.Core/Extensions.Object.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
namespace OnixLabs.Core;

/// <summary>
/// Provides extension methods for objects.
/// Provides extension methods for <see cref="object"/> instances.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class ObjectExtensions
Expand Down
2 changes: 1 addition & 1 deletion OnixLabs.Core/Extensions.Random.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
namespace OnixLabs.Core;

/// <summary>
/// Provides extension methods for objects.
/// Provides extension methods for <see cref="Random"/> instances.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class RandomExtensions
Expand Down
2 changes: 1 addition & 1 deletion OnixLabs.Core/Extensions.ReadOnlySequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
namespace OnixLabs.Core;

/// <summary>
/// Provides extension methods for read-only sequences.
/// Provides extension methods for <see cref="ReadOnlySequence{T}"/> instances.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class ReadOnlySequenceExtensions
Expand Down
2 changes: 1 addition & 1 deletion OnixLabs.Core/Extensions.String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
namespace OnixLabs.Core;

/// <summary>
/// Provides extension methods for strings.
/// Provides extension methods for <see cref="string"/> instances.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class StringExtensions
Expand Down
Loading