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.

192 lines
3.7 KiB

  1. -- 是否是瑞年
  2. function isLeapYear(year)
  3. if year % 100 == 0 then
  4. return year % 400 == 0;
  5. else
  6. return year % 4 == 0;
  7. end
  8. end
  9. -- UTC时间转换成北京时间
  10. function UTCToBeijingTime(second)
  11. return second + 8 * 60 * 60
  12. end
  13. -- 格式:yyyyMMdd返回年月日
  14. function timeToDate(time)
  15. local year;
  16. local month;
  17. local day;
  18. year = math.floor(time / 10000);
  19. local monthDay = time % 10000;
  20. month = math.floor(monthDay / 100);
  21. day = monthDay % 100;
  22. return year, month, day
  23. end
  24. -- 支持ServerTest的公用函数
  25. BeijingTime =
  26. {
  27. convert2Zone = 8;
  28. oldDateFunc = os.date;
  29. oldTimeFunc = os.time;
  30. dateFunc = nil;
  31. timeFunc = nil;
  32. monthTable1 =
  33. {
  34. 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
  35. };
  36. monthTable2 =
  37. {
  38. 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335
  39. };
  40. yearTable = nil;
  41. };
  42. function BeijingTime:initYearTable()
  43. local yearTable = {};
  44. yearTable[1970] = 0;
  45. local dayCount = 0;
  46. for i = 1971, 2100 do
  47. local tmpDay;
  48. if isLeapYear(i - 1) then
  49. tmpDay = 366;
  50. else
  51. tmpDay = 365;
  52. end
  53. dayCount = dayCount + tmpDay;
  54. yearTable[i] = dayCount;
  55. end;
  56. self.yearTable = yearTable;
  57. end;
  58. function BeijingTime:reload()
  59. if not self.yearTable then
  60. self:initYearTable();
  61. end;
  62. BeijingTime.dateFunc = function(time)
  63. time = time or self.oldTimeFunc();
  64. time = time + self.convert2Zone * 3600;
  65. local year, month, day, hour, min, sec, weekDay = self:second2Date(time);
  66. return {year = year, month = month, day = day, hour = hour, min = min, sec = sec, week = weekDay};
  67. end
  68. BeijingTime.timeFunc = function(t)
  69. if not t then
  70. return self.oldTimeFunc();
  71. else
  72. local time = self:date2Second(t.year, t.month, t.day, t.hour, t.min, t.sec);
  73. return time - self.convert2Zone * 3600;
  74. end
  75. end
  76. end;
  77. function BeijingTime:date(formatStr, time)
  78. if not self.dateFunc then
  79. self:reload();
  80. end
  81. return self.dateFunc(formatStr, time);
  82. end
  83. function BeijingTime:second2Date(sec)
  84. local yearTable = self.yearTable;
  85. local monthTable1 = self.monthTable1;
  86. local monthTable2 = self.monthTable2;
  87. local weekDay = 0;
  88. local dayCount = 0;
  89. local year = 0;
  90. for i = 1971, 2100 do
  91. dayCount = yearTable[i];
  92. if dayCount * 86400 > sec then
  93. year = i - 1;
  94. dayCount = yearTable[year];
  95. break;
  96. end
  97. end
  98. sec = sec - dayCount * 86400;
  99. weekDay = dayCount;
  100. local month = 0;
  101. if isLeapYear(year) then
  102. for i = 1, 12 do
  103. month = i;
  104. dayCount = monthTable2[i];
  105. if sec >= monthTable2[i] * 86400
  106. and monthTable2[i + 1]
  107. and sec < monthTable2[i + 1] * 86400 then
  108. break;
  109. end
  110. end
  111. else
  112. for i = 1, 12 do
  113. month = i;
  114. dayCount = monthTable1[i];
  115. if sec >= monthTable1[i] * 86400
  116. and monthTable1[i + 1]
  117. and sec < monthTable1[i + 1] * 86400 then
  118. break;
  119. end
  120. end
  121. end
  122. weekDay = dayCount + weekDay;
  123. sec = sec - dayCount * 86400;
  124. local day = math.floor(sec / 86400) + 1;
  125. sec = sec % 86400;
  126. local hour = math.floor(sec / 3600);
  127. sec = sec % 3600;
  128. local min = math.floor(sec / 60);
  129. sec = sec % 60;
  130. -- 1970年1月1日是星期4
  131. weekDay = weekDay + day - 1 + 4;
  132. weekDay = weekDay % 7;
  133. -- 如果是0, 则改为7
  134. if weekDay == 0 then
  135. weekDay = 7;
  136. end
  137. return year, month, day, hour, min, sec, weekDay;
  138. end;
  139. function BeijingTime:date2Second(year, month, day, hour, min, sec)
  140. local yearTable = self.yearTable;
  141. local monthTable1 = self.monthTable1;
  142. local monthTable2 = self.monthTable2;
  143. local dayCount = 0;
  144. if isLeapYear(year) then
  145. dayCount = yearTable[year] + monthTable2[month] + day - 1;
  146. else
  147. dayCount = yearTable[year] + monthTable1[month] + day - 1;
  148. end
  149. local ret = dayCount * 86400;
  150. hour = hour or 12;
  151. min = min or 0;
  152. sec = sec or 0;
  153. ret = ret + hour * 3600 + min * 60 + sec;
  154. return ret;
  155. end;
  156. BeijingTime:reload();