Skip to content

Commit bf26c5c

Browse files
authored
Add date, time, and keyValue Deconstruct
1 parent d4ae5bd commit bf26c5c

File tree

8 files changed

+148
-14
lines changed

8 files changed

+148
-14
lines changed

apiCount.include.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
**API count: 513**
1+
**API count: 520**

api_list.include.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
#### DateTime
4848

4949
* `DateTime AddMicroseconds(DateTime, double)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.addmicroseconds)
50+
* `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@))
51+
* `void Deconstruct(DateTime, DateOnly, TimeOnly)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.deconstruct#system-datetime-deconstruct(system-dateonly@-system-timeonly@))
5052
* `int Microsecond(DateTime)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.microsecond)
5153
* `int Nanosecond(DateTime)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.nanosecond)
5254
* `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))
@@ -56,6 +58,7 @@
5658
#### DateTimeOffset
5759

5860
* `DateTimeOffset AddMicroseconds(DateTimeOffset, double)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.addmicroseconds)
61+
* `void Deconstruct(DateTimeOffset, DateOnly, TimeOnly, TimeSpan)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.deconstruct?view=net-9.0#definition)
5962
* `int Microsecond(DateTimeOffset)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.microsecond)
6063
* `int Nanosecond(DateTimeOffset)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.nanosecond)
6164
* `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))
@@ -415,6 +418,10 @@
415418

416419
#### TimeOnly
417420

421+
* `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@))
422+
* `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@))
423+
* `void Deconstruct(TimeOnly, int, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.deconstruct#system-timeonly-deconstruct(system-int32@-system-int32@))
424+
* `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@))
418425
* `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))
419426
* `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))
420427

readme.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The package targets `netstandard2.0` and is designed to support the following ru
1212
* `net5.0`, `net6.0`, `net7.0`, `net8.0`, `net9.0`, `net10.0`
1313

1414

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

1717

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

523523
* `DateTime AddMicroseconds(DateTime, double)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.addmicroseconds)
524+
* `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@))
525+
* `void Deconstruct(DateTime, DateOnly, TimeOnly)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.deconstruct#system-datetime-deconstruct(system-dateonly@-system-timeonly@))
524526
* `int Microsecond(DateTime)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.microsecond)
525527
* `int Nanosecond(DateTime)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.nanosecond)
526528
* `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))
@@ -530,6 +532,7 @@ The class `Polyfill` includes the following extension methods:
530532
#### DateTimeOffset
531533

532534
* `DateTimeOffset AddMicroseconds(DateTimeOffset, double)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.addmicroseconds)
535+
* `void Deconstruct(DateTimeOffset, DateOnly, TimeOnly, TimeSpan)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.deconstruct?view=net-9.0#definition)
533536
* `int Microsecond(DateTimeOffset)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.microsecond)
534537
* `int Nanosecond(DateTimeOffset)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.nanosecond)
535538
* `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))
@@ -889,6 +892,10 @@ The class `Polyfill` includes the following extension methods:
889892

890893
#### TimeOnly
891894

895+
* `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@))
896+
* `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@))
897+
* `void Deconstruct(TimeOnly, int, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.deconstruct#system-timeonly-deconstruct(system-int32@-system-int32@))
898+
* `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@))
892899
* `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))
893900
* `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))
894901

src/Consume/Consume.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,21 @@ class Consume
9191
type = typeof(UnreachableException);
9292
type = typeof(DebuggerDisableUserUnhandledExceptionsAttribute);
9393

94-
KeyValuePair.Create("a", "b");
94+
var (key, value) = KeyValuePair.Create("a", "b");
95+
96+
#if NET6_0_OR_GREATER
97+
var (date, time, offset) = DateTimeOffset.Now;
98+
var (dateOnly, timeOnly) = DateTime.Now;
99+
var (hour, minute) = new TimeOnly();
100+
#endif
101+
102+
var (year, month, day) = DateTime.Now;
103+
95104
// Test to make sure there are no clashes in the Polyfill code with classes that
96105
// might be defined in user code. See comments in Debug.cs for more details.
97106
Debug.Log("Test log to make sure this is working");
98107
}
99108

100-
#region Compiler Features
101-
void DeconstructTupleInForeach(IEnumerable<KeyValuePair<string, string>> variables)
102-
{
103-
foreach (var (name, value) in variables)
104-
{
105-
}
106-
}
107-
108109
#if FeatureValueTuple
109110

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

185186
#endif
186187

187-
#endregion
188-
189188
void Byte_Methods()
190189
{
191190
BytePolyfill.TryParse("1", null, out _);

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project>
33
<PropertyGroup>
4-
<Version>7.19.0</Version>
4+
<Version>7.20.0</Version>
55
<AssemblyVersion>1.0.0</AssemblyVersion>
66
<PackageTags>Polyfill</PackageTags>
77
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>

src/Polyfill/Polyfill_DateTime.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// <auto-generated />
2+
#pragma warning disable
3+
4+
namespace Polyfills;
5+
6+
using System;
7+
using System.Collections.Generic;
8+
9+
static partial class Polyfill
10+
{
11+
#if NET6_0 || NET7_0
12+
/// <summary>
13+
/// Deconstructs this <see cref="DateTime"/> instance by <see cref="DateOnly"/> and <see cref="TimeOnly"/>.
14+
/// </summary>
15+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.deconstruct#system-datetime-deconstruct(system-dateonly@-system-timeonly@)
16+
public static void Deconstruct(this DateTime target, out DateOnly date, out TimeOnly time)
17+
{
18+
date = DateOnly.FromDateTime(target);
19+
time = TimeOnly.FromDateTime(target);
20+
}
21+
#endif
22+
23+
#if !NET8_0_OR_GREATER
24+
/// <summary>
25+
/// Deconstructs <see cref="DateOnly"/> by <see cref="Year"/>, <see cref="Month"/> and <see cref="Day"/>.
26+
/// </summary>
27+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.deconstruct#system-datetime-deconstruct(system-int32@-system-int32@-system-int32@)
28+
public static void Deconstruct(this DateTime target, out int year, out int month, out int day)
29+
{
30+
year = target.Year;
31+
month = target.Month;
32+
day = target.Day;
33+
}
34+
#endif
35+
36+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// <auto-generated />
2+
#pragma warning disable
3+
4+
namespace Polyfills;
5+
6+
using System;
7+
using System.Collections.Generic;
8+
9+
static partial class Polyfill
10+
{
11+
#if NET6_0 || NET7_0
12+
/// <summary>
13+
/// Deconstructs this <see cref="DateTimeOffset"/> instance by <see cref="DateOnly"/>, <see cref="TimeOnly"/>, and <see cref="TimeSpan"/>.
14+
/// </summary>
15+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.deconstruct?view=net-9.0#definition
16+
public static void Deconstruct(this DateTimeOffset target, out DateOnly date, out TimeOnly time, out TimeSpan offset)
17+
{
18+
date = DateOnly.FromDateTime(target.DateTime);
19+
time = TimeOnly.FromDateTime(target.DateTime);
20+
21+
offset = target.Offset;
22+
}
23+
#endif
24+
}

src/Polyfill/Polyfill_TimeOnly.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// <auto-generated />
2+
#pragma warning disable
3+
4+
namespace Polyfills;
5+
6+
using System;
7+
using System.Collections.Generic;
8+
9+
static partial class Polyfill
10+
{
11+
#if NET7_0
12+
13+
/// <summary>
14+
/// Deconstructs this TimeOnly instance.
15+
/// </summary>
16+
//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@)
17+
public static void Deconstruct(this TimeOnly target, out int hour, out int minute, out int second, out int millisecond, out int microsecond)
18+
{
19+
hour = target.Hour;
20+
minute = target.Minute;
21+
second = target.Second;
22+
millisecond = target.Millisecond;
23+
microsecond = target.Microsecond;
24+
}
25+
#endif
26+
27+
#if NET6_0 || NET7_0
28+
/// <summary>
29+
/// Deconstructs this TimeOnly instance.
30+
/// </summary>
31+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.deconstruct#system-timeonly-deconstruct(system-int32@-system-int32@-system-int32@-system-int32@)
32+
public static void Deconstruct(this TimeOnly target, out int hour, out int minute, out int second, out int millisecond)
33+
{
34+
hour = target.Hour;
35+
minute = target.Minute;
36+
second = target.Second;
37+
millisecond = target.Millisecond;
38+
}
39+
40+
/// <summary>
41+
/// Deconstructs this TimeOnly instance.
42+
/// </summary>
43+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.deconstruct#system-timeonly-deconstruct(system-int32@-system-int32@-system-int32@)
44+
public static void Deconstruct(this TimeOnly target, out int hour, out int minute, out int second)
45+
{
46+
hour = target.Hour;
47+
minute = target.Minute;
48+
second = target.Second;
49+
}
50+
51+
/// <summary>
52+
/// Deconstructs this TimeOnly instance.
53+
/// </summary>
54+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.deconstruct#system-timeonly-deconstruct(system-int32@-system-int32@)
55+
public static void Deconstruct(this TimeOnly target, out int hour, out int minute)
56+
{
57+
hour = target.Hour;
58+
minute = target.Minute;
59+
}
60+
#endif
61+
}

0 commit comments

Comments
 (0)