Skip to content

Commit

Permalink
Add ToUtcDateTime string extension method.
Browse files Browse the repository at this point in the history
  • Loading branch information
i2van committed May 11, 2022
1 parent fccf3d4 commit d746c13
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Deploy/buildlpx.cmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@echo off

set version=7.3.4
set version=7.3.5
set fileName=CsvLINQPadDriver.%version%
set ext=lpx
set ext6=%ext%6
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ DateTime? ToDateTime(
DateTimeStyles dateTimeStyles = DateTimeStyles.None,
CultureInfo? cultureInfo = null);

DateTime? ToUtcDateTime(CultureInfo? cultureInfo = null);

// DateTimeOffset.
DateTimeOffset? ToDateTimeOffset(
DateTimeStyles dateTimeStyles = DateTimeStyles.None,
Expand Down
4 changes: 2 additions & 2 deletions Src/CsvLINQPadDriver/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<Version>7.3.4</Version>
<PackageReleaseNotes>Added DateTimeOffset, DateOnly/TimeOnly (.NET6+) types conversion support.</PackageReleaseNotes>
<Version>7.3.5</Version>
<PackageReleaseNotes>Added ToUtcDateTime string extension method.</PackageReleaseNotes>
</PropertyGroup>

<PropertyGroup>
Expand Down
10 changes: 10 additions & 0 deletions Src/CsvLINQPadDriver/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ public static class StringExtensions
GetValueOrNull(DateTime.TryParseExact(str, formats, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue);
#endif

private const DateTimeStyles UtcDateTimeStyles = DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal;

public static DateTime? ToUtcDateTime(this string? str, CultureInfo? cultureInfo = null) =>
GetValueOrNull(DateTime.TryParse(str, SelectCulture(cultureInfo), UtcDateTimeStyles, out var parsedValue), parsedValue);

#if NETCOREAPP
public static DateTime? ToUtcDateTime(this ReadOnlySpan<char> str, CultureInfo? cultureInfo = null) =>
GetValueOrNull(DateTime.TryParse(str, SelectCulture(cultureInfo), UtcDateTimeStyles, out var parsedValue), parsedValue);
#endif

public static DateTimeOffset? ToDateTimeOffset(this string? str, DateTimeStyles dateTimeStyles = DateTimeStyles.None, CultureInfo? cultureInfo = null) =>
GetValueOrNull(DateTimeOffset.TryParse(str, SelectCulture(cultureInfo), dateTimeStyles, out var parsedValue), parsedValue);

Expand Down
2 changes: 1 addition & 1 deletion Src/CsvLINQPadDriver/app.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<assemblyIdentity
name="CsvLINQPadDriver"
version="7.3.4.0"
version="7.3.5.0"
type="win32"
/>

Expand Down
2 changes: 1 addition & 1 deletion Tests/CsvLINQPadDriverTest/CsvLINQPadDriverTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.6.0" />
<PackageReference Include="LPRun" Version="7.4.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="Moq" Version="4.17.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1">
Expand Down
19 changes: 19 additions & 0 deletions Tests/CsvLINQPadDriverTest/LPRun/Templates/Extensions.linq
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,25 @@ nullString.ToDateTime().Should().BeNull(Reason());
nullString.ToDateTime(dateTimeFormat).Should().BeNull(Reason());
nullString.ToDateTime(dateTimeFormats).Should().BeNull(Reason());

var utcNow = DateTime.UtcNow;
var expectedUtcNow = new DateTime(utcNow.Year, utcNow.Month, utcNow.Day, utcNow.Hour, utcNow.Minute, utcNow.Second).AddHours(-1).AddMinutes(-30);
utcNow.ToString("yyyy-MM-ddTHH:mm:ss+01:30").ToUtcDateTime().Should().Be(expectedUtcNow, Reason()).And.Subject?.Kind.Should().Be(DateTimeKind.Utc, Reason());

var isoTimes = new []
{
"2022-05-11T10:13:19",
"2022-05-11T10:13:19Z",
"2022-05-11T10:13:19+01:00"
};

foreach(var isoTime in isoTimes)
{
isoTime.ToUtcDateTime().Should().NotBeNull(Reason()).And.Subject?.Kind.Should().Be(DateTimeKind.Utc, Reason());
((ReadOnlySpan<char>)isoTime).ToUtcDateTime().Should().NotBeNull(Reason()).And.Subject?.Kind.Should().Be(DateTimeKind.Utc, Reason());
}

nullString.ToUtcDateTime().Should().BeNull(Reason());

// DateTimeOffset.
((ReadOnlySpan<char>)expectedDateTime.ToString()).ToDateTimeOffset().Should().Be(expectedDateTime, Reason());
expectedDateTime.ToString().ToDateTimeOffset().Should().Be(expectedDateTime, Reason());
Expand Down

0 comments on commit d746c13

Please sign in to comment.