Skip to content

Commit

Permalink
Obsolete UnixTimestampExtensions methods (#2121)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
0xced authored Mar 3, 2020
1 parent faedc8d commit c94cd05
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 73 deletions.
17 changes: 8 additions & 9 deletions Octokit.Tests/Clients/StatisticsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Threading;
using System.Threading.Tasks;
using NSubstitute;
using Octokit.Helpers;
using Xunit;

namespace Octokit.Tests.Clients
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
46 changes: 0 additions & 46 deletions Octokit.Tests/Helpers/UnixTimestampExtensionsTests.cs

This file was deleted.

8 changes: 4 additions & 4 deletions Octokit/Helpers/UnixTimeStampExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ namespace Octokit.Helpers
/// </summary>
public static class UnixTimestampExtensions
{
static readonly DateTimeOffset epoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);

/// <summary>
/// Convert a Unix tick to a <see cref="DateTimeOffset"/> with UTC offset
/// </summary>
/// <param name="unixTime">UTC tick</param>
[Obsolete("Use DateTimeOffset.FromUnixTimeSeconds(long seconds) instead.")]
public static DateTimeOffset FromUnixTime(this long unixTime)
{
return epoch.AddSeconds(unixTime);
return DateTimeOffset.FromUnixTimeSeconds(unixTime);
}

/// <summary>
/// Convert <see cref="DateTimeOffset"/> with UTC offset to a Unix tick
/// </summary>
/// <param name="date">Date Time with UTC offset</param>
[Obsolete("Use DateTimeOffset.ToUnixTimeSeconds() instead.")]
public static long ToUnixTime(this DateTimeOffset date)
{
return Convert.ToInt64((date.ToUniversalTime() - epoch).TotalSeconds);
return date.ToUnixTimeSeconds();
}
}
}
3 changes: 1 addition & 2 deletions Octokit/Http/RateLimit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Runtime.Serialization;
#endif
using System.Security;
using Octokit.Helpers;
using Octokit.Internal;

namespace Octokit
Expand Down Expand Up @@ -57,7 +56,7 @@ public RateLimit(int limit, int remaining, long resetAsUtcEpochSeconds)
/// The date and time at which the current rate limit window resets
/// </summary>
[Parameter(Key = "ignoreThisField")]
public DateTimeOffset Reset { get { return ResetAsUtcEpochSeconds.FromUnixTime(); } }
public DateTimeOffset Reset => DateTimeOffset.FromUnixTimeSeconds(ResetAsUtcEpochSeconds);

/// <summary>
/// The date and time at which the current rate limit window resets - in UTC epoch seconds
Expand Down
3 changes: 1 addition & 2 deletions Octokit/Models/Response/AdditionsAndDeletions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using Octokit.Helpers;

namespace Octokit
{
Expand Down Expand Up @@ -33,7 +32,7 @@ public AdditionsAndDeletions(IList<long> 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]);
}
Expand Down
6 changes: 1 addition & 5 deletions Octokit/Models/Response/WeeklyCommitActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using Octokit.Helpers;

namespace Octokit
{
Expand Down Expand Up @@ -37,10 +36,7 @@ public WeeklyCommitActivity(IEnumerable<int> days, int total, long week)
/// </summary>
public long Week { get; protected set; }

public DateTimeOffset WeekTimestamp
{
get { return Week.FromUnixTime(); }
}
public DateTimeOffset WeekTimestamp => DateTimeOffset.FromUnixTimeSeconds(Week);

/// <summary>
/// Get the number of commits made on any <see cref="DayOfWeek"/>
Expand Down
3 changes: 1 addition & 2 deletions Octokit/Models/Response/WeeklyHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using Octokit.Helpers;

namespace Octokit
{
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions Octokit/SimpleJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c94cd05

Please sign in to comment.