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.

185 lines
6.6 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Apewer.Internals
  5. {
  6. internal class ClockHelper
  7. {
  8. /// <summary>为字符串前添加字符“0”。</summary>
  9. /// <param name="argNumber">原字符串,内容应为整数、小数或十六进制数,若格式不符则返回原字符串。</param>
  10. /// <param name="argLength">新字符串的长度,若大于原数字长度,则不添加额外的“0”。</param>
  11. private static string PreZero(string argNumber, int argLength = 0)
  12. {
  13. string result = string.IsNullOrEmpty(argNumber) ? "" : argNumber.Trim();
  14. if (result.StartsWith(".")) result = "0" + result;
  15. while (result.Length < argLength) result = "0" + result;
  16. return result;
  17. }
  18. /// <summary>判断是否为闰年。</summary>
  19. public static bool LeapYear(int argYear)
  20. {
  21. if (argYear % 400 == 0) return true;
  22. if (argYear % 100 == 0) return false;
  23. if (argYear % 4 == 0) return true;
  24. return false;
  25. }
  26. /// <summary>获取指定年月的天数。</summary>
  27. public static int MonthDays(int argYear, int argMonth)
  28. {
  29. switch (argMonth)
  30. {
  31. case 1: return 31;
  32. case 2: return LeapYear(argYear) ? 29 : 28;
  33. case 3: return 31;
  34. case 4: return 30;
  35. case 5: return 31;
  36. case 6: return 30;
  37. case 7: return 31;
  38. case 8: return 31;
  39. case 9: return 30;
  40. case 10: return 31;
  41. case 11: return 30;
  42. case 12: return 31;
  43. default: return 0;
  44. }
  45. }
  46. public static string ToString(ClockValue argValue, bool argLucid)
  47. {
  48. if (argLucid)
  49. {
  50. var result = new TextBuilder();
  51. result.Append(TextModifier.PreZero(argValue.Year.ToString(), 4));
  52. result.Append("-");
  53. result.Append(TextModifier.PreZero(argValue.Month.ToString(), 2));
  54. result.Append("-");
  55. result.Append(TextModifier.PreZero(argValue.Day.ToString(), 2));
  56. result.Append(" ");
  57. result.Append(TextModifier.PreZero(argValue.Hour.ToString(), 2));
  58. result.Append(":");
  59. result.Append(TextModifier.PreZero(argValue.Minute.ToString(), 2));
  60. result.Append(":");
  61. result.Append(TextModifier.PreZero(argValue.Second.ToString(), 2));
  62. result.Append(".");
  63. result.Append(TextModifier.PreZero(argValue.Millisecond.ToString(), 3));
  64. return result.Value;
  65. }
  66. else
  67. {
  68. string result = "";
  69. result += TextModifier.PreZero(argValue.Year.ToString(), 4);
  70. result += TextModifier.PreZero(argValue.Month.ToString(), 2);
  71. result += TextModifier.PreZero(argValue.Day.ToString(), 2);
  72. result += TextModifier.PreZero(argValue.Hour.ToString(), 2);
  73. result += TextModifier.PreZero(argValue.Minute.ToString(), 2);
  74. result += TextModifier.PreZero(argValue.Second.ToString(), 2);
  75. result += TextModifier.PreZero(argValue.Millisecond.ToString(), 3);
  76. return result;
  77. }
  78. }
  79. /// <summary>表示当前时钟的字节数组。</summary>
  80. public static byte[] ToBinary(ClockValue argValue)
  81. {
  82. var bs = new byte[9];
  83. bs[0] = (byte)(argValue.Year / 256);
  84. bs[1] = (byte)(argValue.Year % 256);
  85. bs[2] = (byte)(argValue.Day / 256);
  86. bs[3] = (byte)(argValue.Month / 256);
  87. bs[4] = (byte)(argValue.Hour / 256);
  88. bs[5] = (byte)(argValue.Minute / 256);
  89. bs[6] = (byte)(argValue.Second / 256);
  90. bs[7] = (byte)(argValue.Millisecond / 256);
  91. bs[8] = (byte)(argValue.Millisecond % 256);
  92. return bs;
  93. }
  94. /// <summary>由指定字节数组创建对象。</summary>
  95. public static ClockValue FromBinary(byte[] argBytes)
  96. {
  97. var bs = argBytes;
  98. if (bs != null)
  99. {
  100. var clock = new ClockValue();
  101. if (argBytes.Length >= 4)
  102. {
  103. clock.Year = bs[0] * 256 + bs[1];
  104. clock.Month = bs[2];
  105. clock.Day = bs[3];
  106. }
  107. if (argBytes.Length >= 7)
  108. {
  109. clock.Hour = bs[4];
  110. clock.Minute = bs[5];
  111. clock.Millisecond = bs[6] * 256 + bs[7];
  112. }
  113. return clock;
  114. }
  115. else
  116. {
  117. return ClockValue.Create(0, 0, 0, 0, 0, 0, 0);
  118. }
  119. }
  120. public static long Stamp(TimeSpan argSpan, bool argMilleSecond = true)
  121. {
  122. try
  123. {
  124. if (argSpan == null) return 0;
  125. var total = argMilleSecond ? argSpan.TotalMilliseconds : argSpan.TotalSeconds;
  126. var result = Convert.ToInt64(total);
  127. return result;
  128. }
  129. catch { return 0; }
  130. }
  131. public static long Stamp(DateTime argCurrent, bool argMilleSecond = true)
  132. {
  133. try
  134. {
  135. var origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
  136. var span = argCurrent - origin;
  137. return Stamp(span, argMilleSecond);
  138. }
  139. catch { return 0; }
  140. }
  141. /// <summary>解析 DateTime 对象。</summary>
  142. public static DateTime Resolve(Object argDateTime)
  143. {
  144. if (argDateTime != null)
  145. {
  146. try
  147. {
  148. if (argDateTime.GetType().Equals(typeof(DateTime)))
  149. {
  150. return (DateTime)argDateTime;
  151. }
  152. }
  153. finally { }
  154. }
  155. return new DateTime(0, 0, 0, 0, 0, 0, 0);
  156. }
  157. /// <summary>解析用 String 表示的 DateTime 对象。</summary>
  158. public static DateTime Resolve(string argDateTime)
  159. {
  160. if (!string.IsNullOrEmpty(argDateTime))
  161. {
  162. try
  163. {
  164. return Resolve(DateTime.Parse(argDateTime));
  165. }
  166. finally { }
  167. }
  168. return new DateTime(0, 0, 0, 0, 0, 0, 0);
  169. }
  170. }
  171. }