DateUtils.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * Copyright (C) Alibaba Cloud Computing
  3. * All rights reserved.
  4. *
  5. * 版权所有 (C)阿里云计算有限公司
  6. */
  7. using System;
  8. using System.Globalization;
  9. namespace Aliyun.OSS.Util
  10. {
  11. internal static class DateUtils
  12. {
  13. private const string Rfc822DateFormat = "ddd, dd MMM yyyy HH:mm:ss \\G\\M\\T";
  14. private const string Iso8601DateFormat = "yyyy-MM-dd'T'HH:mm:ss.fff'Z'";
  15. /// <summary>
  16. /// Format an instance of <see cref="DateTime" /> to a GMT format string.
  17. /// </summary>
  18. public static string FormatRfc822Date(DateTime dtime)
  19. {
  20. return dtime.ToUniversalTime().ToString(Rfc822DateFormat,
  21. CultureInfo.InvariantCulture);
  22. }
  23. /// <summary>
  24. /// Format a GMT format string to an instance of <see cref="DateTime" />.
  25. /// </summary>
  26. /// <returns></returns>
  27. public static DateTime ParseRfc822Date(String s)
  28. {
  29. return DateTime.SpecifyKind(
  30. DateTime.ParseExact(s, Rfc822DateFormat, CultureInfo.InvariantCulture),
  31. DateTimeKind.Utc);
  32. }
  33. /// <summary>
  34. /// Format an instance of <see cref="DateTime" /> to string in iso-8601 format.
  35. /// </summary>
  36. public static string FormatIso8601Date(DateTime dtime)
  37. {
  38. return dtime.ToUniversalTime().ToString(Iso8601DateFormat,
  39. CultureInfo.CurrentCulture);
  40. }
  41. }
  42. }