forked from jwt-dotnet/jwt
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
62 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
namespace JwtDnx | ||
{ | ||
public static class JwtDnxExtentions | ||
{ | ||
private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); | ||
|
||
/// <summary> | ||
/// Convert from .NET DateTime to UnixTimeStamp. | ||
/// (FYI in NET4.6 -> https://msdn.microsoft.com/en-us/library/system.datetimeoffset.tounixtimeseconds.aspx) | ||
/// </summary> | ||
/// <param name="dateTimeUtc">DateTimeUtc</param> | ||
/// <returns>UnixTimeStamp</returns> | ||
public static long ToUnixTimeSeconds(this DateTime dateTimeUtc) | ||
{ | ||
return (long)Math.Round((dateTimeUtc.ToUniversalTime() - UnixEpoch).TotalSeconds); | ||
} | ||
|
||
/// <summary> | ||
/// Convert from UnixTimeStamp to .NET DateTime. | ||
/// (FYI in NET4.6 -> https://msdn.microsoft.com/en-us/library/system.datetimeoffset.tounixtimeseconds.aspx) | ||
/// </summary> | ||
/// <param name="unixTimeStamp">UnixTimeStamp</param> | ||
/// <returns>DateTime by Utc</returns> | ||
public static DateTime ToDateTiemUtc(this long unixTimeStamp) | ||
{ | ||
return UnixEpoch.AddSeconds(unixTimeStamp); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using JwtDnx; | ||
using Xunit; | ||
|
||
namespace JwtDnxTests | ||
{ | ||
public class ExtentionsTests | ||
{ | ||
|
||
private static DateTime _netDateTimeUtc = new DateTime(2038, 01, 19, 03, 14, 07, DateTimeKind.Utc); | ||
private static long _unixTimestamp = 2147483647L; | ||
|
||
[Fact] | ||
public void Should_Convert_from_DateTime_to_UnixTime() | ||
{ | ||
long result = _netDateTimeUtc.ToUnixTimeSeconds(); | ||
Assert.Equal(result, _unixTimestamp); | ||
} | ||
|
||
[Fact] | ||
public void Should_Convert_from_UnixTime_to_DateTime() | ||
{ | ||
DateTime result = _unixTimestamp.ToDateTiemUtc(); | ||
Assert.Equal(result, _netDateTimeUtc); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters