|
-
- -- 是否是瑞年
- function isLeapYear(year)
- if year % 100 == 0 then
- return year % 400 == 0;
- else
- return year % 4 == 0;
- end
- end
-
- -- UTC时间转换成北京时间
- function UTCToBeijingTime(second)
- return second + 8 * 60 * 60
- end
-
- -- 格式:yyyyMMdd返回年月日
- function timeToDate(time)
- local year;
- local month;
- local day;
-
- year = math.floor(time / 10000);
- local monthDay = time % 10000;
- month = math.floor(monthDay / 100);
- day = monthDay % 100;
-
- return year, month, day
- end
-
- -- 支持ServerTest的公用函数
- BeijingTime =
- {
- convert2Zone = 8;
- oldDateFunc = os.date;
- oldTimeFunc = os.time;
- dateFunc = nil;
- timeFunc = nil;
-
- monthTable1 =
- {
- 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
- };
-
- monthTable2 =
- {
- 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335
- };
-
- yearTable = nil;
- };
-
- function BeijingTime:initYearTable()
- local yearTable = {};
-
- yearTable[1970] = 0;
- local dayCount = 0;
- for i = 1971, 2100 do
- local tmpDay;
- if isLeapYear(i - 1) then
- tmpDay = 366;
- else
- tmpDay = 365;
- end
- dayCount = dayCount + tmpDay;
- yearTable[i] = dayCount;
- end;
-
- self.yearTable = yearTable;
- end;
-
- function BeijingTime:reload()
-
- if not self.yearTable then
- self:initYearTable();
- end;
-
- BeijingTime.dateFunc = function(time)
- time = time or self.oldTimeFunc();
- time = time + self.convert2Zone * 3600;
- local year, month, day, hour, min, sec, weekDay = self:second2Date(time);
- return {year = year, month = month, day = day, hour = hour, min = min, sec = sec, week = weekDay};
- end
-
- BeijingTime.timeFunc = function(t)
- if not t then
- return self.oldTimeFunc();
- else
- local time = self:date2Second(t.year, t.month, t.day, t.hour, t.min, t.sec);
- return time - self.convert2Zone * 3600;
- end
- end
- end;
-
-
- function BeijingTime:date(formatStr, time)
- if not self.dateFunc then
- self:reload();
- end
-
- return self.dateFunc(formatStr, time);
- end
-
- function BeijingTime:second2Date(sec)
- local yearTable = self.yearTable;
- local monthTable1 = self.monthTable1;
- local monthTable2 = self.monthTable2;
-
-
- local weekDay = 0;
- local dayCount = 0;
- local year = 0;
- for i = 1971, 2100 do
- dayCount = yearTable[i];
- if dayCount * 86400 > sec then
- year = i - 1;
- dayCount = yearTable[year];
- break;
- end
- end
-
- sec = sec - dayCount * 86400;
- weekDay = dayCount;
-
- local month = 0;
- if isLeapYear(year) then
- for i = 1, 12 do
- month = i;
- dayCount = monthTable2[i];
- if sec >= monthTable2[i] * 86400
- and monthTable2[i + 1]
- and sec < monthTable2[i + 1] * 86400 then
- break;
- end
- end
- else
- for i = 1, 12 do
- month = i;
- dayCount = monthTable1[i];
- if sec >= monthTable1[i] * 86400
- and monthTable1[i + 1]
- and sec < monthTable1[i + 1] * 86400 then
- break;
- end
- end
- end
-
- weekDay = dayCount + weekDay;
-
- sec = sec - dayCount * 86400;
-
- local day = math.floor(sec / 86400) + 1;
- sec = sec % 86400;
- local hour = math.floor(sec / 3600);
- sec = sec % 3600;
- local min = math.floor(sec / 60);
- sec = sec % 60;
-
- -- 1970年1月1日是星期4
- weekDay = weekDay + day - 1 + 4;
- weekDay = weekDay % 7;
- -- 如果是0, 则改为7
- if weekDay == 0 then
- weekDay = 7;
- end
-
- return year, month, day, hour, min, sec, weekDay;
- end;
-
-
- function BeijingTime:date2Second(year, month, day, hour, min, sec)
- local yearTable = self.yearTable;
- local monthTable1 = self.monthTable1;
- local monthTable2 = self.monthTable2;
-
- local dayCount = 0;
- if isLeapYear(year) then
- dayCount = yearTable[year] + monthTable2[month] + day - 1;
- else
- dayCount = yearTable[year] + monthTable1[month] + day - 1;
- end
-
- local ret = dayCount * 86400;
- hour = hour or 12;
- min = min or 0;
- sec = sec or 0;
- ret = ret + hour * 3600 + min * 60 + sec;
-
- return ret;
- end;
-
- BeijingTime:reload();
|