Skip to content

Commit d608605

Browse files
authored
Merge pull request #249 from 0xced/DateOnlyTimeOnly
Add support for DateOnly and TimeOnly through implicit operator conversion
2 parents b2249c0 + 3867640 commit d608605

6 files changed

Lines changed: 130 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [1.9.5] - 2024-06-03
11+
12+
### Added
13+
14+
- Kiota's `Date` and `Time` types are interchangeable with the system `DateOnly` and `TimeOnly` types through implicit conversion operators.
15+
1016
## [1.9.4] - 2024-05-31
1117

1218
### Changed

Microsoft.Kiota.Abstractions.Tests/RequestInformationTests.cs

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,52 @@ public void SetsPathParametersOfTimeType()
260260
// Assert
261261
Assert.Contains($"%24time=06%3A00%3A00", requestInfo.URI.OriginalString);
262262
}
263+
#if NET6_0_OR_GREATER
264+
[Fact]
265+
public void SetsPathParametersOfDateOnlyType()
266+
{
267+
// Arrange as the request builders would
268+
var requestInfo = new RequestInformation
269+
{
270+
HttpMethod = Method.GET,
271+
UrlTemplate = "http://localhost/users{?%24date}"
272+
};
273+
274+
// Act
275+
var date = new DateOnly(2023, 10, 26);
276+
var pathParameters = new Dictionary<string, object>
277+
{
278+
{ "%24date", date }
279+
};
280+
281+
requestInfo.PathParameters = pathParameters;
282+
283+
// Assert
284+
Assert.Contains($"%24date=2023-10-26", requestInfo.URI.OriginalString);
285+
}
286+
[Fact]
287+
public void SetsPathParametersOfTimeOnlyType()
288+
{
289+
// Arrange as the request builders would
290+
var requestInfo = new RequestInformation
291+
{
292+
HttpMethod = Method.GET,
293+
UrlTemplate = "http://localhost/users{?%24time}"
294+
};
295+
296+
// Act
297+
var time = new TimeOnly(6, 0, 0);
298+
var pathParameters = new Dictionary<string, object>
299+
{
300+
{ "%24time", time }
301+
};
302+
303+
requestInfo.PathParameters = pathParameters;
304+
305+
// Assert
306+
Assert.Contains($"%24time=06%3A00%3A00", requestInfo.URI.OriginalString);
307+
}
308+
#endif
263309
[Fact]
264310
public void CurrentCultureDoesNotAffectTimeSerialization()
265311
{
@@ -663,14 +709,54 @@ public void SetsTimeValuesInQueryParameters()
663709
};
664710

665711
// Act
666-
var date1 = new Time(10, 0, 0);
667-
var date2 = new Time(11, 1, 1);
712+
var time1 = new Time(10, 0, 0);
713+
var time2 = new Time(11, 1, 1);
714+
715+
requestInfo.AddQueryParameters(new GetQueryParameters { Items = new object[] { time1, time2 } });
716+
717+
// Assert
718+
Assert.Equal("http://localhost/me?items=10%3A00%3A00,11%3A01%3A01", requestInfo.URI.OriginalString);
719+
}
720+
721+
#if NET6_0_OR_GREATER
722+
[Fact]
723+
public void SetsDateOnlyValuesInQueryParameters()
724+
{
725+
var requestInfo = new RequestInformation
726+
{
727+
HttpMethod = Method.GET,
728+
UrlTemplate = "http://localhost/me{?items}"
729+
};
730+
731+
// Act
732+
Date date1 = new DateOnly(2022, 8, 1);
733+
DateOnly date2 = new Date(2022, 8, 2);
668734

669735
requestInfo.AddQueryParameters(new GetQueryParameters { Items = new object[] { date1, date2 } });
670736

737+
// Assert
738+
Assert.Equal("http://localhost/me?items=2022-08-01,2022-08-02", requestInfo.URI.OriginalString);
739+
}
740+
741+
[Fact]
742+
public void SetsTimeOnlyValuesInQueryParameters()
743+
{
744+
var requestInfo = new RequestInformation
745+
{
746+
HttpMethod = Method.GET,
747+
UrlTemplate = "http://localhost/me{?items}"
748+
};
749+
750+
// Act
751+
Time time1 = new TimeOnly(10, 0, 0);
752+
TimeOnly time2 = new Time(11, 1, 1);
753+
754+
requestInfo.AddQueryParameters(new GetQueryParameters { Items = new object[] { time1, time2 } });
755+
671756
// Assert
672757
Assert.Equal("http://localhost/me?items=10%3A00%3A00,11%3A01%3A01", requestInfo.URI.OriginalString);
673758
}
759+
#endif
674760

675761
[Fact]
676762
public void SetsGuidValuesInQueryParameters()

src/Date.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@ namespace Microsoft.Kiota.Abstractions
1111
/// </summary>
1212
public struct Date
1313
{
14+
#if NET6_0_OR_GREATER
15+
/// <summary>
16+
/// Converts the supplied <see cref="DateOnly"/> parameter to <see cref="Date"/>.
17+
/// </summary>
18+
/// <param name="date">The <see cref="DateOnly"/> to be converted.</param>
19+
/// <returns>A new <see cref="Date"/> structure whose years, months and days are equal to those of the supplied date.</returns>
20+
public static implicit operator Date(DateOnly date) => new(date.Year, date.Month, date.Day);
21+
22+
/// <summary>
23+
/// Converts the supplied <see cref="Date"/> parameter to <see cref="DateOnly"/>.
24+
/// </summary>
25+
/// <param name="date">The <see cref="Date"/> to be converted.</param>
26+
/// <returns>A new <see cref="DateOnly"/> structure whose years, months and days are equal to those of the supplied date.</returns>
27+
public static implicit operator DateOnly(Date date) => new(date.Year, date.Month, date.Day);
28+
#endif
29+
1430
/// <summary>
1531
/// Create a new Date object from a <see cref="DateTime"/> object
1632
/// </summary>

src/Microsoft.Kiota.Abstractions.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<PackageProjectUrl>https://aka.ms/kiota/docs</PackageProjectUrl>
1616
<EmbedUntrackedSources>true</EmbedUntrackedSources>
1717
<Deterministic>true</Deterministic>
18-
<VersionPrefix>1.9.4</VersionPrefix>
18+
<VersionPrefix>1.9.5</VersionPrefix>
1919
<VersionSuffix></VersionSuffix>
2020
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
2121
<SignAssembly>false</SignAssembly>

src/RequestInformation.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ public Uri URI
124124
bool boolean => boolean.ToString().ToLower(),// pass in a lowercase string as the final url will be uppercase due to the way ToString() works for booleans
125125
DateTimeOffset dateTimeOffset => dateTimeOffset.ToString("o"),// Default to ISO 8601 for datetimeoffsets in the url.
126126
DateTime dateTime => dateTime.ToString("o"),// Default to ISO 8601 for datetimes in the url.
127+
#if NET6_0_OR_GREATER
128+
DateOnly dateOnly => dateOnly.ToString("yyyy-MM-dd"),// Default to ISO 8601 for dateonlys in the url.
129+
TimeOnly timeOnly => timeOnly.ToString(@"HH\:mm\:ss"),// Default to ISO 8601 for timeonlys in the url.
130+
#endif
127131
Guid guid => guid.ToString("D"),// Default of 32 digits separated by hyphens
128132
Date date => date.ToString(), //Default to string format of the custom date object
129133
Time time => time.ToString(), //Default to string format of the custom time object

src/Time.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ namespace Microsoft.Kiota.Abstractions
1111
/// </summary>
1212
public struct Time
1313
{
14+
#if NET6_0_OR_GREATER
15+
/// <summary>
16+
/// Converts the supplied <see cref="TimeOnly"/> parameter to <see cref="Time"/>.
17+
/// </summary>
18+
/// <param name="time">The <see cref="TimeOnly"/> to be converted.</param>
19+
/// <returns>A new <see cref="Time"/> structure whose hours, minutes, seconds and milliseconds are equal to those of the supplied time.</returns>
20+
public static implicit operator Time(TimeOnly time) => new(new DateTime(1, 1, 1, time.Hour, time.Minute, time.Second, time.Millisecond));
21+
22+
/// <summary>
23+
/// Converts the supplied <see cref="Time"/> parameter to <see cref="TimeOnly"/>.
24+
/// </summary>
25+
/// <param name="time">The <see cref="Time"/> to be converted.</param>
26+
/// <returns>A new <see cref="TimeOnly"/> structure whose hours, minutes, seconds and milliseconds are equal to those of the supplied time.</returns>
27+
public static implicit operator TimeOnly(Time time) => new(time.DateTime.Hour, time.DateTime.Minute, time.DateTime.Second, time.DateTime.Millisecond);
28+
#endif
1429
/// <summary>
1530
/// Create a new Time from hours, minutes, and seconds.
1631
/// </summary>

0 commit comments

Comments
 (0)