From 9c0101e013ef0ee630ab83bedb1f28178cfd000c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Luthi?= Date: Tue, 3 Mar 2020 14:03:39 +0100 Subject: [PATCH] Obsolete UnixTimestampExtensions methods * The equivalent of `public static DateTimeOffset FromUnixTime(this long unixTime)` exists in the framework since .NET Framework 4.6: https://docs.microsoft.com/en-us/dotnet/api/system.datetimeoffset.fromunixtimeseconds * The equivalent of `public static long ToUnixTime(this DateTimeOffset date)` exists in the framework since .NET Framework 4.6: https://docs.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tounixtimeseconds --- .../Clients/StatisticsClientTests.cs | 17 ++++--- .../Helpers/UnixTimestampExtensionsTests.cs | 46 ------------------- Octokit/Helpers/UnixTimeStampExtensions.cs | 8 ++-- Octokit/Http/RateLimit.cs | 3 +- .../Models/Response/AdditionsAndDeletions.cs | 3 +- .../Models/Response/WeeklyCommitActivity.cs | 6 +-- Octokit/Models/Response/WeeklyHash.cs | 3 +- Octokit/SimpleJson.cs | 5 +- 8 files changed, 18 insertions(+), 73 deletions(-) delete mode 100644 Octokit.Tests/Helpers/UnixTimestampExtensionsTests.cs diff --git a/Octokit.Tests/Clients/StatisticsClientTests.cs b/Octokit.Tests/Clients/StatisticsClientTests.cs index 53e7f34c0a..df9549f864 100644 --- a/Octokit.Tests/Clients/StatisticsClientTests.cs +++ b/Octokit.Tests/Clients/StatisticsClientTests.cs @@ -4,7 +4,6 @@ using System.Threading; using System.Threading.Tasks; using NSubstitute; -using Octokit.Helpers; using Xunit; namespace Octokit.Tests.Clients @@ -232,10 +231,10 @@ public async Task RequestsCorrectUrl() var codeFrequency = await statisticsClient.GetCodeFrequency("owner", "name"); Assert.Equal(2, codeFrequency.AdditionsAndDeletionsByWeek.Count); - Assert.Equal(firstTimestamp.FromUnixTime(), codeFrequency.AdditionsAndDeletionsByWeek[0].Timestamp); + Assert.Equal(DateTimeOffset.FromUnixTimeSeconds(firstTimestamp), codeFrequency.AdditionsAndDeletionsByWeek[0].Timestamp); Assert.Equal(10, codeFrequency.AdditionsAndDeletionsByWeek[0].Additions); Assert.Equal(52, codeFrequency.AdditionsAndDeletionsByWeek[0].Deletions); - Assert.Equal(secondTimestamp.FromUnixTime(), codeFrequency.AdditionsAndDeletionsByWeek[1].Timestamp); + Assert.Equal(DateTimeOffset.FromUnixTimeSeconds(secondTimestamp), codeFrequency.AdditionsAndDeletionsByWeek[1].Timestamp); Assert.Equal(0, codeFrequency.AdditionsAndDeletionsByWeek[1].Additions); Assert.Equal(9, codeFrequency.AdditionsAndDeletionsByWeek[1].Deletions); } @@ -260,10 +259,10 @@ public async Task RequestsCorrectUrlWithRepositoryId() var codeFrequency = await statisticsClient.GetCodeFrequency(1); Assert.Equal(2, codeFrequency.AdditionsAndDeletionsByWeek.Count); - Assert.Equal(firstTimestamp.FromUnixTime(), codeFrequency.AdditionsAndDeletionsByWeek[0].Timestamp); + Assert.Equal(DateTimeOffset.FromUnixTimeSeconds(firstTimestamp), codeFrequency.AdditionsAndDeletionsByWeek[0].Timestamp); Assert.Equal(10, codeFrequency.AdditionsAndDeletionsByWeek[0].Additions); Assert.Equal(52, codeFrequency.AdditionsAndDeletionsByWeek[0].Deletions); - Assert.Equal(secondTimestamp.FromUnixTime(), codeFrequency.AdditionsAndDeletionsByWeek[1].Timestamp); + Assert.Equal(DateTimeOffset.FromUnixTimeSeconds(secondTimestamp), codeFrequency.AdditionsAndDeletionsByWeek[1].Timestamp); Assert.Equal(0, codeFrequency.AdditionsAndDeletionsByWeek[1].Additions); Assert.Equal(9, codeFrequency.AdditionsAndDeletionsByWeek[1].Deletions); } @@ -290,10 +289,10 @@ public async Task RequestsCorrectUrlWithCancellationToken() var codeFrequency = await client.GetCodeFrequency("owner", "name", cancellationToken); Assert.Equal(2, codeFrequency.AdditionsAndDeletionsByWeek.Count); - Assert.Equal(firstTimestamp.FromUnixTime(), codeFrequency.AdditionsAndDeletionsByWeek[0].Timestamp); + Assert.Equal(DateTimeOffset.FromUnixTimeSeconds(firstTimestamp), codeFrequency.AdditionsAndDeletionsByWeek[0].Timestamp); Assert.Equal(10, codeFrequency.AdditionsAndDeletionsByWeek[0].Additions); Assert.Equal(52, codeFrequency.AdditionsAndDeletionsByWeek[0].Deletions); - Assert.Equal(secondTimestamp.FromUnixTime(), codeFrequency.AdditionsAndDeletionsByWeek[1].Timestamp); + Assert.Equal(DateTimeOffset.FromUnixTimeSeconds(secondTimestamp), codeFrequency.AdditionsAndDeletionsByWeek[1].Timestamp); Assert.Equal(0, codeFrequency.AdditionsAndDeletionsByWeek[1].Additions); Assert.Equal(9, codeFrequency.AdditionsAndDeletionsByWeek[1].Deletions); } @@ -320,10 +319,10 @@ public async Task RequestsCorrectUrlWithRepositoryIdWithCancellationToken() var codeFrequency = await client.GetCodeFrequency(1, cancellationToken); Assert.Equal(2, codeFrequency.AdditionsAndDeletionsByWeek.Count); - Assert.Equal(firstTimestamp.FromUnixTime(), codeFrequency.AdditionsAndDeletionsByWeek[0].Timestamp); + Assert.Equal(DateTimeOffset.FromUnixTimeSeconds(firstTimestamp), codeFrequency.AdditionsAndDeletionsByWeek[0].Timestamp); Assert.Equal(10, codeFrequency.AdditionsAndDeletionsByWeek[0].Additions); Assert.Equal(52, codeFrequency.AdditionsAndDeletionsByWeek[0].Deletions); - Assert.Equal(secondTimestamp.FromUnixTime(), codeFrequency.AdditionsAndDeletionsByWeek[1].Timestamp); + Assert.Equal(DateTimeOffset.FromUnixTimeSeconds(secondTimestamp), codeFrequency.AdditionsAndDeletionsByWeek[1].Timestamp); Assert.Equal(0, codeFrequency.AdditionsAndDeletionsByWeek[1].Additions); Assert.Equal(9, codeFrequency.AdditionsAndDeletionsByWeek[1].Deletions); } diff --git a/Octokit.Tests/Helpers/UnixTimestampExtensionsTests.cs b/Octokit.Tests/Helpers/UnixTimestampExtensionsTests.cs deleted file mode 100644 index 09c542c3c1..0000000000 --- a/Octokit.Tests/Helpers/UnixTimestampExtensionsTests.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using Octokit.Helpers; -using Xunit; - -public class UnixTimestampExtensionsTests -{ - public class TheToUnixTimeMethod - { - [Fact] - public void ReturnsUnixEpochCorrectly() - { - var epoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero); - Assert.Equal(0, epoch.ToUnixTime()); - } - - [Fact] - public void ReturnsRandomDateCorrectly() - { - var epoch = new DateTimeOffset(1975, 1, 23, 1, 1, 1, TimeSpan.Zero); - Assert.Equal(159670861, epoch.ToUnixTime()); - } - } - - public class TheFromUnixTimeMethod - { - [Fact] - public void ReturnsDateFromUnixEpochCorrectly() - { - var epoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero); - - var result = ((long)0).FromUnixTime(); - - Assert.Equal(epoch, result); - } - - [Fact] - public void ReturnsDateFromRandomTimeCorrectly() - { - var expected = new DateTimeOffset(1975, 1, 23, 1, 1, 2, TimeSpan.Zero); - - var result = ((long)159670862).FromUnixTime(); - - Assert.Equal(expected, result); - } - } -} diff --git a/Octokit/Helpers/UnixTimeStampExtensions.cs b/Octokit/Helpers/UnixTimeStampExtensions.cs index 7888a91f12..071b74b282 100644 --- a/Octokit/Helpers/UnixTimeStampExtensions.cs +++ b/Octokit/Helpers/UnixTimeStampExtensions.cs @@ -7,24 +7,24 @@ namespace Octokit.Helpers /// public static class UnixTimestampExtensions { - static readonly DateTimeOffset epoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero); - /// /// Convert a Unix tick to a with UTC offset /// /// UTC tick + [Obsolete("Use DateTimeOffset.FromUnixTimeSeconds(long seconds) instead.")] public static DateTimeOffset FromUnixTime(this long unixTime) { - return epoch.AddSeconds(unixTime); + return DateTimeOffset.FromUnixTimeSeconds(unixTime); } /// /// Convert with UTC offset to a Unix tick /// /// Date Time with UTC offset + [Obsolete("Use DateTimeOffset.ToUnixTimeSeconds() instead.")] public static long ToUnixTime(this DateTimeOffset date) { - return Convert.ToInt64((date.ToUniversalTime() - epoch).TotalSeconds); + return date.ToUnixTimeSeconds(); } } } diff --git a/Octokit/Http/RateLimit.cs b/Octokit/Http/RateLimit.cs index 9a24896b46..a5c2f28800 100644 --- a/Octokit/Http/RateLimit.cs +++ b/Octokit/Http/RateLimit.cs @@ -7,7 +7,6 @@ using System.Runtime.Serialization; #endif using System.Security; -using Octokit.Helpers; using Octokit.Internal; namespace Octokit @@ -57,7 +56,7 @@ public RateLimit(int limit, int remaining, long resetAsUtcEpochSeconds) /// The date and time at which the current rate limit window resets /// [Parameter(Key = "ignoreThisField")] - public DateTimeOffset Reset { get { return ResetAsUtcEpochSeconds.FromUnixTime(); } } + public DateTimeOffset Reset => DateTimeOffset.FromUnixTimeSeconds(ResetAsUtcEpochSeconds); /// /// The date and time at which the current rate limit window resets - in UTC epoch seconds diff --git a/Octokit/Models/Response/AdditionsAndDeletions.cs b/Octokit/Models/Response/AdditionsAndDeletions.cs index f6aa16af99..362fa4ffff 100644 --- a/Octokit/Models/Response/AdditionsAndDeletions.cs +++ b/Octokit/Models/Response/AdditionsAndDeletions.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Diagnostics; using System.Globalization; -using Octokit.Helpers; namespace Octokit { @@ -33,7 +32,7 @@ public AdditionsAndDeletions(IList additionsAndDeletions) { throw new ArgumentException("Addition and deletion aggregate must only contain three data points."); } - Timestamp = additionsAndDeletions[0].FromUnixTime(); + Timestamp = DateTimeOffset.FromUnixTimeSeconds(additionsAndDeletions[0]); Additions = Convert.ToInt32(additionsAndDeletions[1]); Deletions = Convert.ToInt32(additionsAndDeletions[2]); } diff --git a/Octokit/Models/Response/WeeklyCommitActivity.cs b/Octokit/Models/Response/WeeklyCommitActivity.cs index 713fa04b03..1b7112206d 100644 --- a/Octokit/Models/Response/WeeklyCommitActivity.cs +++ b/Octokit/Models/Response/WeeklyCommitActivity.cs @@ -4,7 +4,6 @@ using System.Diagnostics; using System.Globalization; using System.Linq; -using Octokit.Helpers; namespace Octokit { @@ -37,10 +36,7 @@ public WeeklyCommitActivity(IEnumerable days, int total, long week) /// public long Week { get; protected set; } - public DateTimeOffset WeekTimestamp - { - get { return Week.FromUnixTime(); } - } + public DateTimeOffset WeekTimestamp => DateTimeOffset.FromUnixTimeSeconds(Week); /// /// Get the number of commits made on any diff --git a/Octokit/Models/Response/WeeklyHash.cs b/Octokit/Models/Response/WeeklyHash.cs index b6ee1f3489..8e06d0b320 100644 --- a/Octokit/Models/Response/WeeklyHash.cs +++ b/Octokit/Models/Response/WeeklyHash.cs @@ -2,7 +2,6 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Globalization; -using Octokit.Helpers; namespace Octokit { @@ -37,7 +36,7 @@ public WeeklyHash(long w, int a, int d, int c) public DateTimeOffset Week { - get { return W.FromUnixTime(); } + get { return DateTimeOffset.FromUnixTimeSeconds(W); } } public int Additions diff --git a/Octokit/SimpleJson.cs b/Octokit/SimpleJson.cs index 4b48db24c7..8ee637b06a 100644 --- a/Octokit/SimpleJson.cs +++ b/Octokit/SimpleJson.cs @@ -66,7 +66,6 @@ using Octokit.Reflection; #if !SIMPLE_JSON_NO_LINQ_EXPRESSION using System.Linq.Expressions; -using Octokit.Helpers; #endif #if SIMPLE_JSON_DYNAMIC using System.Dynamic; @@ -1433,11 +1432,11 @@ public virtual object DeserializeObject(object value, Type type) { if (valueIsLong && (type == typeof(DateTimeOffset) || type == typeof(DateTimeOffset?))) { - return ((long)value).FromUnixTime(); + return DateTimeOffset.FromUnixTimeSeconds((long)value); } else if (valueIsLong && (type == typeof(DateTime) || type == typeof(DateTime?))) { - return ((long)value).FromUnixTime().DateTime; + return DateTimeOffset.FromUnixTimeSeconds((long)value).DateTime; } obj = type == typeof(int) || type == typeof(long) || type == typeof(double) || type == typeof(float) || type == typeof(bool) || type == typeof(decimal) || type == typeof(byte) || type == typeof(short) ? Convert.ChangeType(value, type, CultureInfo.InvariantCulture)