From d76228e80a3ce2e815a26f554ee1da1d64917963 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Wed, 2 Oct 2024 22:05:44 +1000 Subject: [PATCH] add DateTimePolyfill and DateTimeOffsetPolyfill (#231) --- apiCount.include.md | 2 +- api_list.include.md | 16 +++++++ readme.md | 24 +++++++++- src/Polyfill/DateTimeOffsetPolyfill.cs | 66 ++++++++++++++++++++++++++ src/Polyfill/DateTimePolyfill.cs | 66 ++++++++++++++++++++++++++ src/Tests/BuildApiTest.cs | 2 + 6 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 src/Polyfill/DateTimeOffsetPolyfill.cs create mode 100644 src/Polyfill/DateTimePolyfill.cs diff --git a/apiCount.include.md b/apiCount.include.md index acd1420a..484a50a7 100644 --- a/apiCount.include.md +++ b/apiCount.include.md @@ -1 +1 @@ -**API count: 352** \ No newline at end of file +**API count: 360** \ No newline at end of file diff --git a/api_list.include.md b/api_list.include.md index 8119c033..4fb0767d 100644 --- a/api_list.include.md +++ b/api_list.include.md @@ -452,6 +452,22 @@ * `Boolean TryParse(ReadOnlySpan, Globalization.NumberStyles, IFormatProvider, Byte&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.byte.tryparse#system-byte-tryparse(system-readonlyspan((system-char))-system-globalization-numberstyles-system-iformatprovider-system-byte@)) +#### DateTimePolyfill + + * `Boolean TryParse(String, IFormatProvider, DateTime&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tryparse#system-datetime-tryparse(system-string-system-iformatprovider-system-datetime@)) + * `Boolean TryParse(ReadOnlySpan, IFormatProvider, DateTime&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tryparse#system-datetime-tryparse(system-readonlyspan((system-char))-system-iformatprovider-system-datetime@)) + * `Boolean TryParse(ReadOnlySpan, DateTime&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tryparse#system-datetime-tryparse(system-readonlyspan((system-char))-system-datetime@)) + * `Boolean TryParse(ReadOnlySpan, IFormatProvider, Globalization.DateTimeStyles, DateTime&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tryparse#system-datetime-tryparse(system-readonlyspan((system-char))-system-iformatprovider-system-globalization-datetimestyles-system-datetime@)) + + +#### DateTimeOffsetPolyfill + + * `Boolean TryParse(String, IFormatProvider, DateTimeOffset&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tryparse#system-datetimeoffset-tryparse(system-string-system-iformatprovider-system-datetimeoffset@)) + * `Boolean TryParse(ReadOnlySpan, IFormatProvider, DateTimeOffset&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tryparse#system-datetimeoffset-tryparse(system-readonlyspan((system-char))-system-iformatprovider-system-datetimeoffset@)) + * `Boolean TryParse(ReadOnlySpan, DateTimeOffset&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tryparse#system-datetimeoffset-tryparse(system-readonlyspan((system-char))-system-datetimeoffset@)) + * `Boolean TryParse(ReadOnlySpan, IFormatProvider, Globalization.DateTimeStyles, DateTimeOffset&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tryparse#system-datetimeoffset-tryparse(system-readonlyspan((system-char))-system-iformatprovider-system-globalization-datetimestyles-system-datetimeoffset@)) + + #### DoublePolyfill * `Boolean TryParse(String, IFormatProvider, Double&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.double.tryparse#system-double-tryparse(system-string-system-iformatprovider-system-double@)) diff --git a/readme.md b/readme.md index af2c2e67..3570ce7d 100644 --- a/readme.md +++ b/readme.md @@ -12,7 +12,7 @@ The package targets `netstandard2.0` and is designed to support the following ru * `net5.0`, `net6.0`, `net7.0`, `net8.0`, `net9.0` -**API count: 352** +**API count: 362** **See [Milestones](../../milestones?state=closed) for release notes.** @@ -905,6 +905,28 @@ The class `Polyfill` includes the following extension methods: * `Boolean TryParse(ReadOnlySpan, Globalization.NumberStyles, IFormatProvider, Byte&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.byte.tryparse#system-byte-tryparse(system-readonlyspan((system-char))-system-globalization-numberstyles-system-iformatprovider-system-byte@)) +#### GuidPolyfill + + * `Boolean TryParse(String, IFormatProvider, Guid&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.guid.tryparse#system-guid-tryparse(system-string-system-iformatprovider-system-guid@)) + * `Boolean TryParse(ReadOnlySpan, IFormatProvider, Byte&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.byte.tryparse#system-byte-tryparse(system-readonlyspan((system-byte))-system-iformatprovider-system-byte@)) + + +#### DateTimePolyfill + + * `Boolean TryParse(String, IFormatProvider, DateTime&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tryparse#system-datetime-tryparse(system-string-system-iformatprovider-system-datetime@)) + * `Boolean TryParse(ReadOnlySpan, IFormatProvider, DateTime&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tryparse#system-datetime-tryparse(system-readonlyspan((system-char))-system-iformatprovider-system-datetime@)) + * `Boolean TryParse(ReadOnlySpan, DateTime&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tryparse#system-datetime-tryparse(system-readonlyspan((system-char))-system-datetime@)) + * `Boolean TryParse(ReadOnlySpan, IFormatProvider, Globalization.DateTimeStyles, DateTime&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tryparse#system-datetime-tryparse(system-readonlyspan((system-char))-system-iformatprovider-system-globalization-datetimestyles-system-datetime@)) + + +#### DateTimeOffsetPolyfill + + * `Boolean TryParse(String, IFormatProvider, DateTimeOffset&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tryparse#system-datetimeoffset-tryparse(system-string-system-iformatprovider-system-datetimeoffset@)) + * `Boolean TryParse(ReadOnlySpan, IFormatProvider, DateTimeOffset&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tryparse#system-datetimeoffset-tryparse(system-readonlyspan((system-char))-system-iformatprovider-system-datetimeoffset@)) + * `Boolean TryParse(ReadOnlySpan, DateTimeOffset&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tryparse#system-datetimeoffset-tryparse(system-readonlyspan((system-char))-system-datetimeoffset@)) + * `Boolean TryParse(ReadOnlySpan, IFormatProvider, Globalization.DateTimeStyles, DateTimeOffset&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tryparse#system-datetimeoffset-tryparse(system-readonlyspan((system-char))-system-iformatprovider-system-globalization-datetimestyles-system-datetimeoffset@)) + + #### DoublePolyfill * `Boolean TryParse(String, IFormatProvider, Double&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.double.tryparse#system-double-tryparse(system-string-system-iformatprovider-system-double@)) diff --git a/src/Polyfill/DateTimeOffsetPolyfill.cs b/src/Polyfill/DateTimeOffsetPolyfill.cs new file mode 100644 index 00000000..f68d49e5 --- /dev/null +++ b/src/Polyfill/DateTimeOffsetPolyfill.cs @@ -0,0 +1,66 @@ +// +#pragma warning disable + +namespace Polyfills; +using System; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using System.Globalization; +using System.Text; +using Link = System.ComponentModel.DescriptionAttribute; + +[ExcludeFromCodeCoverage] +[DebuggerNonUserCode] +#if PolyPublic +public +#endif +static partial class DateTimeOffsetPolyfill +{ + /// + /// Tries to parse a string into a value. + /// + [Link("https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tryparse#system-datetimeoffset-tryparse(system-string-system-iformatprovider-system-datetimeoffset@)")] + public static bool TryParse(string? target, IFormatProvider? provider, out DateTimeOffset result) => +#if NET7_0_OR_GREATER + DateTimeOffset.TryParse(target, provider, out result); +#else + DateTimeOffset.TryParse(target, provider, DateTimeStyles.None, out result); +#endif + +#if FeatureMemory + + /// + /// Tries to parse a span of characters into a value. + /// + [Link("https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tryparse#system-datetimeoffset-tryparse(system-readonlyspan((system-char))-system-iformatprovider-system-datetimeoffset@)")] + public static bool TryParse(ReadOnlySpan target, IFormatProvider? provider, out DateTimeOffset result) => +#if NET8_0_OR_GREATER + DateTimeOffset.TryParse(target, provider, out result); +#else + DateTimeOffset.TryParse(target.ToString(), provider, DateTimeStyles.None, out result); +#endif + + /// + /// Tries to parse a span of characters into a value. + /// + [Link("https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tryparse#system-datetimeoffset-tryparse(system-readonlyspan((system-char))-system-datetimeoffset@)")] + public static bool TryParse(ReadOnlySpan target, out DateTimeOffset result) => +#if NETSTANDARD2_1 || NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER + DateTimeOffset.TryParse(target, out result); +#else + DateTimeOffset.TryParse(target.ToString(), null, DateTimeStyles.None, out result); +#endif + + /// + /// Tries to parse a span of characters into a value. + /// + [Link("https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tryparse#system-datetimeoffset-tryparse(system-readonlyspan((system-char))-system-iformatprovider-system-globalization-datetimestyles-system-datetimeoffset@)")] + public static bool TryParse(ReadOnlySpan target, IFormatProvider? provider, DateTimeStyles styles, out DateTimeOffset result) => +#if NETSTANDARD2_1 || NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER + DateTimeOffset.TryParse(target, provider, styles, out result); +#else + DateTimeOffset.TryParse(target.ToString(), provider, styles, out result); +#endif + +#endif +} \ No newline at end of file diff --git a/src/Polyfill/DateTimePolyfill.cs b/src/Polyfill/DateTimePolyfill.cs new file mode 100644 index 00000000..1601324d --- /dev/null +++ b/src/Polyfill/DateTimePolyfill.cs @@ -0,0 +1,66 @@ +// +#pragma warning disable + +namespace Polyfills; +using System; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using System.Globalization; +using System.Text; +using Link = System.ComponentModel.DescriptionAttribute; + +[ExcludeFromCodeCoverage] +[DebuggerNonUserCode] +#if PolyPublic +public +#endif +static partial class DateTimePolyfill +{ + /// + /// Tries to parse a string into a value. + /// + [Link("https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tryparse#system-datetime-tryparse(system-string-system-iformatprovider-system-datetime@)")] + public static bool TryParse(string? target, IFormatProvider? provider, out DateTime result) => +#if NET7_0_OR_GREATER + DateTime.TryParse(target, provider, out result); +#else + DateTime.TryParse(target, provider, DateTimeStyles.None, out result); +#endif + +#if FeatureMemory + + /// + /// Tries to parse a span of characters into a value. + /// + [Link("https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tryparse#system-datetime-tryparse(system-readonlyspan((system-char))-system-iformatprovider-system-datetime@)")] + public static bool TryParse(ReadOnlySpan target, IFormatProvider? provider, out DateTime result) => +#if NET8_0_OR_GREATER + DateTime.TryParse(target, provider, out result); +#else + DateTime.TryParse(target.ToString(), provider, DateTimeStyles.None, out result); +#endif + + /// + /// Tries to parse a span of characters into a value. + /// + [Link("https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tryparse#system-datetime-tryparse(system-readonlyspan((system-char))-system-datetime@)")] + public static bool TryParse(ReadOnlySpan target, out DateTime result) => +#if NETSTANDARD2_1 || NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER + DateTime.TryParse(target, out result); +#else + DateTime.TryParse(target.ToString(), null, DateTimeStyles.None, out result); +#endif + + /// + /// Tries to parse a span of characters into a value. + /// + [Link("https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tryparse#system-datetime-tryparse(system-readonlyspan((system-char))-system-iformatprovider-system-globalization-datetimestyles-system-datetime@)")] + public static bool TryParse(ReadOnlySpan target, IFormatProvider? provider, DateTimeStyles styles, out DateTime result) => +#if NETSTANDARD2_1 || NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER + DateTime.TryParse(target, provider, styles, out result); +#else + DateTime.TryParse(target.ToString(), provider, styles, out result); +#endif + +#endif +} \ No newline at end of file diff --git a/src/Tests/BuildApiTest.cs b/src/Tests/BuildApiTest.cs index e81076c5..b0cb0cfb 100644 --- a/src/Tests/BuildApiTest.cs +++ b/src/Tests/BuildApiTest.cs @@ -56,6 +56,8 @@ public void Run() WriteHelper(types, "RegexPolyfill", writer, ref count); WriteHelper(types, "StringPolyfill", writer, ref count); WriteHelper(types, "BytePolyfill", writer, ref count); + WriteHelper(types, "DateTimePolyfill", writer, ref count); + WriteHelper(types, "DateTimeOffsetPolyfill", writer, ref count); WriteHelper(types, "DoublePolyfill", writer, ref count); WriteHelper(types, "IntPolyfill", writer, ref count); WriteHelper(types, "LongPolyfill", writer, ref count);