public void UnixTimestamp() { DateTime now = DateTime.Now.ToUniversalTime(); DateTime now2 = DateTimeUtility.FromUnixTimestamp(DateTimeUtility.ToUnixTimestamp(now)); Assert.Equal(now.ToString("yyyyMMdd-HHmmss"), now2.ToString("yyyyMMdd-HHmmss")); }
public void TokenSignAndVerify() { var token = new TestToken() { Header = new DefaultJwtHeader() { Algorithm = SecurityAlgorithms.HmacSha256 }, Payload = new CommonPayload() { Actor = "TestUser", Audience = "TestAudience", Issuer = "TestIssuer", Subject = "TestTokens", IssuedAt = DateTimeUtility.FromUnixTimestamp(DateTimeUtility.ToUnixTimestamp(DateTime.Now)), Expires = DateTimeUtility.FromUnixTimestamp(DateTimeUtility.ToUnixTimestamp(DateTime.Now.AddDays(1))) } }; var testKey = new SymmetricSecurityKey("TestKey".ToHash <MD5>()); var tokenString = token.Sign(testKey); var isVaild = JwtTokenConvert.Verify <CommonPayload>( tokenString, testKey, out var verifyToken); Assert.True(isVaild); Assert.Equal(JObject.FromObject(token), JObject.FromObject(verifyToken)); }
public void RoundTripUnixTimestamp() { DateTime dtNow = ClearMilliseconds(DateTime.UtcNow); long unixTimestamp = DateTimeUtility.ToUnixTimestamp(dtNow); DateTime dtFromUnixTimestamp = DateTimeUtility.FromUnixTimestamp(unixTimestamp); Assert.AreEqual(dtNow, dtFromUnixTimestamp); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { try { return(DateTimeUtility.FromUnixTimestamp((long)reader.Value)); } catch { return(null); } }
/// <summary> /// Reads the JSON representation of the object. /// </summary> protected override DateTime ReadCore(JsonReader reader, JsonSerializer serializer) { if (reader.TokenType != JsonToken.Integer) { throw new JsonSerializationException("Expected integer for DateTime; got " + reader.TokenType); } long ticks = (long)reader.Value; return(DateTimeUtility.FromUnixTimestamp(ticks)); }
public void TestDateTime() { //字符串 To DateTime var now = DateTime.Now.Date.AddHours(8).AddMinutes(7).AddSeconds(6); var date1 = DateTimeUtility.ToDateTimeFormString(now.ToString("yyyyMMdd")); Assert.AreEqual(date1, now.Date); var date2 = DateTimeUtility.ToDateTimeFormString(now.ToString("yyyyMMddHHmmss")); Assert.AreEqual(date2, now); var date3 = DateTimeUtility.ToDateTimeFormString("2020-02-02 20:20:20", "yyyy-MM-dd HH:mm:ss"); Assert.AreEqual(date3, new DateTime(2020, 2, 2, 20, 20, 20)); //DateTime To 字符串 var dateStr1 = now.ToStandardStr(); Assert.AreEqual(dateStr1, now.ToString("yyyy-MM-dd HH:mm:ss")); var dateStr2 = now.ToDateStr(); Assert.AreEqual(dateStr2, now.ToString("yyyy-MM-dd")); var dateStr3 = now.ToTimeStr("*"); Assert.AreEqual(dateStr3, now.ToString("HH*mm*ss")); //时间戳转换 var unixStamp = 1580646020L; var jsStamp = 1580646020000L; var date = new DateTime(2020, 2, 2, 20, 20, 20); var reDate1 = DateTimeUtility.FromUnixTimestamp(unixStamp); Assert.AreEqual(reDate1, date); var reDate2 = DateTimeUtility.FromJSTimestamp(jsStamp); Assert.AreEqual(reDate2, date); var reUnixStamp = DateTimeUtility.ToUnixTimestamp(date); Assert.AreEqual(reUnixStamp, unixStamp); var reJsStamp = DateTimeUtility.ToJSTimestamp(date); Assert.AreEqual(reJsStamp, jsStamp); //日期计算 var week = date.WeekOfMonth(); Assert.AreEqual(week, 1); }
public void FromUnixTimestampTest(DateTime datetime, long unixTimestamp) { Assert.Equal(datetime, DateTimeUtility.FromUnixTimestamp(unixTimestamp)); }
public DateTime GetUtcNow() => DateTimeUtility.FromUnixTimestamp(m_timestamp);