DateTimeExtensions.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. using System;
  2. namespace Grpc.Extension.Common
  3. {
  4. /// <summary>
  5. /// DateTimeExtensions
  6. /// </summary>
  7. public static class DateTimeExtensions
  8. {
  9. /// <summary>
  10. /// unixtime to datetime
  11. /// </summary>
  12. /// <param name="unixtime"></param>
  13. /// <returns></returns>
  14. public static DateTime FromUnixTimestamp(this long unixtime)
  15. {
  16. //DateTime sTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  17. DateTime sTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Utc, TimeZoneInfo.Local);
  18. return sTime.AddMilliseconds(unixtime);
  19. }
  20. /// <summary>
  21. /// datetime to unixtime
  22. /// </summary>
  23. /// <param name="datetime"></param>
  24. /// <returns></returns>
  25. public static long ToUnixTimestamp(this DateTime datetime)
  26. {
  27. //DateTime sTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  28. DateTime sTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Utc, TimeZoneInfo.Local);
  29. return (long)(datetime - sTime).TotalMilliseconds;
  30. }
  31. }
  32. }