Skip to content

Commit

Permalink
Add date, time, and keyValue Deconstruct
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Feb 26, 2025
1 parent d4ae5bd commit bf26c5c
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 14 deletions.
2 changes: 1 addition & 1 deletion apiCount.include.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
**API count: 513**
**API count: 520**
7 changes: 7 additions & 0 deletions api_list.include.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
#### DateTime

* `DateTime AddMicroseconds(DateTime, double)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.addmicroseconds)
* `void Deconstruct(DateTime, int, int, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.deconstruct#system-datetime-deconstruct(system-int32@-system-int32@-system-int32@))
* `void Deconstruct(DateTime, DateOnly, TimeOnly)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.deconstruct#system-datetime-deconstruct(system-dateonly@-system-timeonly@))
* `int Microsecond(DateTime)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.microsecond)
* `int Nanosecond(DateTime)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.nanosecond)
* `bool TryFormat(DateTime, Span<byte>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tryformat#system-datetime-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
Expand All @@ -56,6 +58,7 @@
#### DateTimeOffset

* `DateTimeOffset AddMicroseconds(DateTimeOffset, double)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.addmicroseconds)
* `void Deconstruct(DateTimeOffset, DateOnly, TimeOnly, TimeSpan)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.deconstruct?view=net-9.0#definition)
* `int Microsecond(DateTimeOffset)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.microsecond)
* `int Nanosecond(DateTimeOffset)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.nanosecond)
* `bool TryFormat(DateTimeOffset, Span<byte>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tryformat#system-datetimeoffset-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
Expand Down Expand Up @@ -415,6 +418,10 @@

#### TimeOnly

* `void Deconstruct(TimeOnly, int, int, int, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.deconstruct#system-timeonly-deconstruct(system-int32@-system-int32@-system-int32@-system-int32@))
* `void Deconstruct(TimeOnly, int, int, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.deconstruct#system-timeonly-deconstruct(system-int32@-system-int32@-system-int32@))
* `void Deconstruct(TimeOnly, int, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.deconstruct#system-timeonly-deconstruct(system-int32@-system-int32@))
* `void Deconstruct(TimeOnly, int, int, int, int, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.deconstruct#system-timeonly-deconstruct(system-int32@-system-int32@-system-int32@-system-int32@-system-int32@))
* `bool TryFormat(TimeOnly, Span<byte>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.tryformat#system-timeonly-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
* `bool TryFormat(TimeOnly, Span<char>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.tryformat#system-timeonly-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))

Expand Down
9 changes: 8 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`, `net10.0`


**API count: 513**<!-- singleLineInclude: apiCount. path: /apiCount.include.md -->
**API count: 520**<!-- singleLineInclude: apiCount. path: /apiCount.include.md -->


**See [Milestones](../../milestones?state=closed) for release notes.**
Expand Down Expand Up @@ -521,6 +521,8 @@ The class `Polyfill` includes the following extension methods:
#### DateTime

* `DateTime AddMicroseconds(DateTime, double)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.addmicroseconds)
* `void Deconstruct(DateTime, int, int, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.deconstruct#system-datetime-deconstruct(system-int32@-system-int32@-system-int32@))
* `void Deconstruct(DateTime, DateOnly, TimeOnly)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.deconstruct#system-datetime-deconstruct(system-dateonly@-system-timeonly@))
* `int Microsecond(DateTime)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.microsecond)
* `int Nanosecond(DateTime)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.nanosecond)
* `bool TryFormat(DateTime, Span<byte>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tryformat#system-datetime-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
Expand All @@ -530,6 +532,7 @@ The class `Polyfill` includes the following extension methods:
#### DateTimeOffset

* `DateTimeOffset AddMicroseconds(DateTimeOffset, double)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.addmicroseconds)
* `void Deconstruct(DateTimeOffset, DateOnly, TimeOnly, TimeSpan)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.deconstruct?view=net-9.0#definition)
* `int Microsecond(DateTimeOffset)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.microsecond)
* `int Nanosecond(DateTimeOffset)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.nanosecond)
* `bool TryFormat(DateTimeOffset, Span<byte>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tryformat#system-datetimeoffset-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
Expand Down Expand Up @@ -889,6 +892,10 @@ The class `Polyfill` includes the following extension methods:

#### TimeOnly

* `void Deconstruct(TimeOnly, int, int, int, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.deconstruct#system-timeonly-deconstruct(system-int32@-system-int32@-system-int32@-system-int32@))
* `void Deconstruct(TimeOnly, int, int, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.deconstruct#system-timeonly-deconstruct(system-int32@-system-int32@-system-int32@))
* `void Deconstruct(TimeOnly, int, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.deconstruct#system-timeonly-deconstruct(system-int32@-system-int32@))
* `void Deconstruct(TimeOnly, int, int, int, int, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.deconstruct#system-timeonly-deconstruct(system-int32@-system-int32@-system-int32@-system-int32@-system-int32@))
* `bool TryFormat(TimeOnly, Span<byte>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.tryformat#system-timeonly-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
* `bool TryFormat(TimeOnly, Span<char>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.tryformat#system-timeonly-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))

Expand Down
21 changes: 10 additions & 11 deletions src/Consume/Consume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,21 @@ class Consume
type = typeof(UnreachableException);
type = typeof(DebuggerDisableUserUnhandledExceptionsAttribute);

KeyValuePair.Create("a", "b");
var (key, value) = KeyValuePair.Create("a", "b");

#if NET6_0_OR_GREATER
var (date, time, offset) = DateTimeOffset.Now;
var (dateOnly, timeOnly) = DateTime.Now;
var (hour, minute) = new TimeOnly();
#endif

var (year, month, day) = DateTime.Now;

// Test to make sure there are no clashes in the Polyfill code with classes that
// might be defined in user code. See comments in Debug.cs for more details.
Debug.Log("Test log to make sure this is working");
}

#region Compiler Features
void DeconstructTupleInForeach(IEnumerable<KeyValuePair<string, string>> variables)
{
foreach (var (name, value) in variables)
{
}
}

#if FeatureValueTuple

static (string value1, bool value2) NamedTupleMethod() =>
Expand Down Expand Up @@ -184,8 +185,6 @@ void Ranges()

#endif

#endregion

void Byte_Methods()
{
BytePolyfill.TryParse("1", null, out _);
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<Version>7.19.0</Version>
<Version>7.20.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageTags>Polyfill</PackageTags>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
Expand Down
36 changes: 36 additions & 0 deletions src/Polyfill/Polyfill_DateTime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// <auto-generated />
#pragma warning disable

namespace Polyfills;

using System;
using System.Collections.Generic;

static partial class Polyfill
{
#if NET6_0 || NET7_0
/// <summary>
/// Deconstructs this <see cref="DateTime"/> instance by <see cref="DateOnly"/> and <see cref="TimeOnly"/>.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.deconstruct#system-datetime-deconstruct(system-dateonly@-system-timeonly@)
public static void Deconstruct(this DateTime target, out DateOnly date, out TimeOnly time)
{
date = DateOnly.FromDateTime(target);
time = TimeOnly.FromDateTime(target);
}
#endif

#if !NET8_0_OR_GREATER
/// <summary>
/// Deconstructs <see cref="DateOnly"/> by <see cref="Year"/>, <see cref="Month"/> and <see cref="Day"/>.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.deconstruct#system-datetime-deconstruct(system-int32@-system-int32@-system-int32@)
public static void Deconstruct(this DateTime target, out int year, out int month, out int day)
{
year = target.Year;
month = target.Month;
day = target.Day;
}
#endif

}
24 changes: 24 additions & 0 deletions src/Polyfill/Polyfill_DateTimeOffset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// <auto-generated />
#pragma warning disable

namespace Polyfills;

using System;
using System.Collections.Generic;

static partial class Polyfill
{
#if NET6_0 || NET7_0
/// <summary>
/// Deconstructs this <see cref="DateTimeOffset"/> instance by <see cref="DateOnly"/>, <see cref="TimeOnly"/>, and <see cref="TimeSpan"/>.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.deconstruct?view=net-9.0#definition
public static void Deconstruct(this DateTimeOffset target, out DateOnly date, out TimeOnly time, out TimeSpan offset)
{
date = DateOnly.FromDateTime(target.DateTime);
time = TimeOnly.FromDateTime(target.DateTime);

offset = target.Offset;
}
#endif
}
61 changes: 61 additions & 0 deletions src/Polyfill/Polyfill_TimeOnly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// <auto-generated />
#pragma warning disable

namespace Polyfills;

using System;
using System.Collections.Generic;

static partial class Polyfill
{
#if NET7_0

/// <summary>
/// Deconstructs this TimeOnly instance.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.deconstruct#system-timeonly-deconstruct(system-int32@-system-int32@-system-int32@-system-int32@-system-int32@)
public static void Deconstruct(this TimeOnly target, out int hour, out int minute, out int second, out int millisecond, out int microsecond)
{
hour = target.Hour;
minute = target.Minute;
second = target.Second;
millisecond = target.Millisecond;
microsecond = target.Microsecond;
}
#endif

#if NET6_0 || NET7_0
/// <summary>
/// Deconstructs this TimeOnly instance.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.deconstruct#system-timeonly-deconstruct(system-int32@-system-int32@-system-int32@-system-int32@)
public static void Deconstruct(this TimeOnly target, out int hour, out int minute, out int second, out int millisecond)
{
hour = target.Hour;
minute = target.Minute;
second = target.Second;
millisecond = target.Millisecond;
}

/// <summary>
/// Deconstructs this TimeOnly instance.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.deconstruct#system-timeonly-deconstruct(system-int32@-system-int32@-system-int32@)
public static void Deconstruct(this TimeOnly target, out int hour, out int minute, out int second)
{
hour = target.Hour;
minute = target.Minute;
second = target.Second;
}

/// <summary>
/// Deconstructs this TimeOnly instance.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.deconstruct#system-timeonly-deconstruct(system-int32@-system-int32@)
public static void Deconstruct(this TimeOnly target, out int hour, out int minute)
{
hour = target.Hour;
minute = target.Minute;
}
#endif
}

0 comments on commit bf26c5c

Please sign in to comment.