From d746c13d3c8338faf5b9e90a79a0c09192d8f793 Mon Sep 17 00:00:00 2001 From: Ivan Ivon Date: Wed, 11 May 2022 16:34:43 +0300 Subject: [PATCH] Add ToUtcDateTime string extension method. --- Deploy/buildlpx.cmd | 2 +- README.md | 2 ++ Src/CsvLINQPadDriver/Directory.Build.props | 4 ++-- .../Extensions/StringExtensions.cs | 10 ++++++++++ Src/CsvLINQPadDriver/app.manifest | 2 +- .../CsvLINQPadDriverTest.csproj | 2 +- .../LPRun/Templates/Extensions.linq | 19 +++++++++++++++++++ 7 files changed, 36 insertions(+), 5 deletions(-) diff --git a/Deploy/buildlpx.cmd b/Deploy/buildlpx.cmd index bdf8245..2f8f912 100644 --- a/Deploy/buildlpx.cmd +++ b/Deploy/buildlpx.cmd @@ -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 diff --git a/README.md b/README.md index 62009fc..a2cfb30 100644 --- a/README.md +++ b/README.md @@ -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, diff --git a/Src/CsvLINQPadDriver/Directory.Build.props b/Src/CsvLINQPadDriver/Directory.Build.props index 793d1d3..fecdc26 100644 --- a/Src/CsvLINQPadDriver/Directory.Build.props +++ b/Src/CsvLINQPadDriver/Directory.Build.props @@ -1,7 +1,7 @@  - 7.3.4 - Added DateTimeOffset, DateOnly/TimeOnly (.NET6+) types conversion support. + 7.3.5 + Added ToUtcDateTime string extension method. diff --git a/Src/CsvLINQPadDriver/Extensions/StringExtensions.cs b/Src/CsvLINQPadDriver/Extensions/StringExtensions.cs index e3b49b6..460aa7d 100644 --- a/Src/CsvLINQPadDriver/Extensions/StringExtensions.cs +++ b/Src/CsvLINQPadDriver/Extensions/StringExtensions.cs @@ -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 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); diff --git a/Src/CsvLINQPadDriver/app.manifest b/Src/CsvLINQPadDriver/app.manifest index 94032e4..74444cf 100644 --- a/Src/CsvLINQPadDriver/app.manifest +++ b/Src/CsvLINQPadDriver/app.manifest @@ -8,7 +8,7 @@ diff --git a/Tests/CsvLINQPadDriverTest/CsvLINQPadDriverTest.csproj b/Tests/CsvLINQPadDriverTest/CsvLINQPadDriverTest.csproj index b5b3ae5..494ff43 100644 --- a/Tests/CsvLINQPadDriverTest/CsvLINQPadDriverTest.csproj +++ b/Tests/CsvLINQPadDriverTest/CsvLINQPadDriverTest.csproj @@ -60,7 +60,7 @@ - + diff --git a/Tests/CsvLINQPadDriverTest/LPRun/Templates/Extensions.linq b/Tests/CsvLINQPadDriverTest/LPRun/Templates/Extensions.linq index 022a2ad..2e26477 100644 --- a/Tests/CsvLINQPadDriverTest/LPRun/Templates/Extensions.linq +++ b/Tests/CsvLINQPadDriverTest/LPRun/Templates/Extensions.linq @@ -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)isoTime).ToUtcDateTime().Should().NotBeNull(Reason()).And.Subject?.Kind.Should().Be(DateTimeKind.Utc, Reason()); +} + +nullString.ToUtcDateTime().Should().BeNull(Reason()); + // DateTimeOffset. ((ReadOnlySpan)expectedDateTime.ToString()).ToDateTimeOffset().Should().Be(expectedDateTime, Reason()); expectedDateTime.ToString().ToDateTimeOffset().Should().Be(expectedDateTime, Reason());