You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
5.0 KiB

  1. using Apewer.Internals;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Apewer
  6. {
  7. /// <summary>时钟。</summary>
  8. public class ClockUtility
  9. {
  10. /// <summary>表示当前时钟的文本,显示为易于阅读的格式。</summary>
  11. public static string Lucid
  12. {
  13. get { return ClockValue.Current.Lucid; }
  14. }
  15. /// <summary>表示当前时钟的文本,显示为连续的数字。</summary>
  16. public static string Compact
  17. {
  18. get { return ClockValue.Current.Compact; }
  19. }
  20. /// <summary>获取当前本地时间以秒为单位的时间戳,以 1970-01-01 00:00:00.000 为原点。</summary>
  21. public static long SecondStamp()
  22. {
  23. return ClockHelper.Stamp(new DateTime(1970, 1, 1, 0, 0, 0, 0), false);
  24. }
  25. /// <summary>获取指定时间以秒为单位的时间戳,以 1970-01-01 00:00:00.000 为原点。</summary>
  26. public static long SecondStamp(DateTime datetime)
  27. {
  28. return ClockHelper.Stamp(datetime, false);
  29. }
  30. /// <summary>获取当前本地时间以毫秒为单位的时间戳,以 1970-01-01 00:00:00.000 为原点。</summary>
  31. public static long MilliSecondStamp()
  32. {
  33. return ClockHelper.Stamp(new DateTime(1970, 1, 1, 0, 0, 0, 0), true);
  34. }
  35. /// <summary>获取指定时间以毫秒为单位的时间戳,以 1970-01-01 00:00:00.000 为原点。</summary>
  36. public static long MilliSecondStamp(DateTime datetime)
  37. {
  38. return ClockHelper.Stamp(datetime, true);
  39. }
  40. /// <summary>获取 UTC 时间。</summary>
  41. public static DateTime UtcDateTime
  42. {
  43. get { return DateTime.UtcNow; }
  44. }
  45. /// <summary>获取 UTC 时间以秒为单位的时间戳,以 1970-01-01 00:00:00.000 为原点。</summary>
  46. public static long UtcSecondStamp
  47. {
  48. get { return ClockHelper.Stamp(DateTime.UtcNow, false); }
  49. }
  50. /// <summary>获取 UTC 时间以毫秒为单位的时间戳,以 1970-01-01 00:00:00.000 为原点。</summary>
  51. public static long UtcMilliSecondStamp
  52. {
  53. get { return ClockHelper.Stamp(DateTime.UtcNow, true); }
  54. }
  55. /// <summary>获取本地时间。</summary>
  56. public static DateTime LocalDateTime
  57. {
  58. get { return DateTime.Now; }
  59. }
  60. /// <summary>获取本地时间以秒为单位的时间戳,以 1970-01-01 00:00:00.000 为原点。</summary>
  61. public static long LocalSecondStamp
  62. {
  63. get { return ClockHelper.Stamp(DateTime.Now, false); }
  64. }
  65. /// <summary>获取本地时间以毫秒为单位的时间戳,以 1970-01-01 00:00:00.000 为原点。</summary>
  66. public static long LocalMilliSecondStamp
  67. {
  68. get { return ClockHelper.Stamp(DateTime.Now, true); }
  69. }
  70. /// <summary>转换毫秒时间戳到易于阅读的文本。</summary>
  71. public static string ToLucid(long stamp)
  72. {
  73. return ClockValue.Create(stamp).Lucid;
  74. }
  75. /// <summary>转换 DateTime 对象到易于阅读的文本。</summary>
  76. public static string ToLucid(DateTime datetime)
  77. {
  78. return ClockValue.Create(datetime).Lucid;
  79. }
  80. /// <summary>转换 DateTime 对象到易于阅读的文本。</summary>
  81. public static string ToLucidDate(DateTime datetime)
  82. {
  83. return ClockValue.Create(datetime).LucidDate;
  84. }
  85. /// <summary>转换 DateTime 对象到易于阅读的文本。</summary>
  86. public static string ToLucidTime(DateTime datetime, bool millisconds = false)
  87. {
  88. var time = ClockValue.Create(datetime).LucidTime;
  89. if (!millisconds) time = time.Substring(0, time.Length - 4);
  90. return time;
  91. }
  92. /// <summary>转换毫秒时间戳到紧凑文本。</summary>
  93. public static string ToCompact(long stamp)
  94. {
  95. return ClockValue.Create(stamp).Compact;
  96. }
  97. /// <summary>转换 DateTime 对象到紧凑文本。</summary>
  98. public static string ToCompact(DateTime datetime)
  99. {
  100. return ClockValue.Create(datetime).Compact;
  101. }
  102. /// <summary>判断闰年。</summary>
  103. public static bool LeapYear(int year)
  104. {
  105. return ClockHelper.LeapYear(year);
  106. }
  107. /// <summary>获取指定年月的天数。</summary>
  108. public static int MonthDays(int year, int month)
  109. {
  110. return ClockHelper.MonthDays(year, month);
  111. }
  112. /// <summary>尝试获取 DateTime 对象。</summary>
  113. public static DateTime GetDateTime(Object datetime)
  114. {
  115. return ClockHelper.Resolve(datetime);
  116. }
  117. /// <summary>尝试获取 DateTime 对象。</summary>
  118. public static DateTime GetDateTime(string datetime)
  119. {
  120. return ClockHelper.Resolve(datetime);
  121. }
  122. }
  123. }