智慧 + 毅力 = 无所不能

正确性、健壮性、可靠性、效率、易用性、可读性、可复用性、兼容性、可移植性...

导航

Lua游戏开发之时区问题

Posted on 2019-12-09 17:13  Bill Yuan  阅读(965)  评论(0编辑  收藏  举报

转自:https://www.cnblogs.com/meteoric_cry/p/9637040.html

目前大部分游戏都采用了Lua语言进行功能开发,在进行多语种发行的时候就会遇到时区显示的问题。以韩国版本为例,场景如下:

1、服务器处于固定的位置,比如放在首尔机房;

2、玩家所处的位置不确定,可能在韩国,或者是出差在其它国家或地区;

 

需求:

无论在哪个国家或地区,统一显示服务器的当前时间。在PC上查看,即便在国内测试的时候也显示韩国首尔的时间(比北京时间快1个小时)。

 

实现:

-- 北京时间
local serverTime = 1536722753 -- 2018/09/12 11:25

function getTimeZone()
    local now = os.time()
    return os.difftime(now, os.time(os.date("!*t", now)))
end

-- 8 hour * 3600 seconds = 28800 seconds
local timeZone = getTimeZone()/ 3600
print("timeZone : " .. timeZone)


local timeInterval = os.time(os.date("!*t", serverTime)) + timeZone * 3600 + (os.date("*t", time).isdst and -1 or 0) * 3600
local timeTable = os.date("*t", timeInterval)

--[[
for k, v in pairs(timeTable) do
    print(k .. ":" .. tostring(v))
end
]]
print(timeTable.year .. "/" .. timeTable.month .. "/" .. timeTable.day .. " " .. timeTable.hour .. ":" .. timeTable.min .. ":" .. timeTable.sec)

关注是这个方法: os.date("!*t", now),其中以!为关键。

lua 源码, loslib.c Line 283 行

static int os_date (lua_State *L) {
  size_t slen;
  const char *s = luaL_optlstring(L, 1, "%c", &slen);
  time_t t = luaL_opt(L, l_checktime, 2, time(NULL));
  const char *se = s + slen;  /* 's' end */
  struct tm tmr, *stm;
  if (*s == '!') {  /* UTC? */
    stm = l_gmtime(&t, &tmr);
    s++;  /* skip '!' */
  }
  else
    stm = l_localtime(&t, &tmr);
  if (stm == NULL)  /* invalid date? */
    luaL_error(L, "time result cannot be represented in this installation");
  if (strcmp(s, "*t") == 0) {
    lua_createtable(L, 0, 9);  /* 9 = number of fields */
    setallfields(L, stm);
  }
  else {
    char cc[4];  /* buffer for individual conversion specifiers */
    luaL_Buffer b;
    cc[0] = '%';
    luaL_buffinit(L, &b);
    while (s < se) {
      if (*s != '%')  /* not a conversion specifier? */
        luaL_addchar(&b, *s++);
      else {
        size_t reslen;
        char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT);
        s++;  /* skip '%' */
        s = checkoption(L, s, se - s, cc + 1);  /* copy specifier to 'cc' */
        reslen = strftime(buff, SIZETIMEFMT, cc, stm);
        luaL_addsize(&b, reslen);
      }
    }
    luaL_pushresult(&b);
  }
  return 1;
}

从源码可以看到 ! 调用了

#define l_gmtime(t,r)        gmtime_r(t,r)

gmtime_r 函数是标准的POSIX函数,它是线程安全的,将日历时间转换为用UTC时间表示的时间。

注:UTC —— 协调世界时,又称世界统一时间、世界标准时间

也就是说 “!*t” 得到的是一个 UTC 时间,为0度的经线(子午线),亦称本初子午线,通常将它与GMT视作等同(但是UTC更为科学和精确)。

首尔位于东9区,所以实际的时间应该是 UTC + 9,9就是时区差 —— 9个小时。北京位于东8区,即 UTC + 8。

 

如何保证游戏内全部统一为服务器的时间呢?

服务器需要返回给客户端当前的时区的差值,比如韩国就返回 9,国内就返回 8,越南返回 7,北美返回 –16,记为 serverTimeZone。

服务端返回当前服务器时间serverTime(即首尔当前时间),我们只需要将服务器时间转为 UTC 的时间,然后再加上 serverTimeZone即可。

os.time(os.date("!*t", serverTime)) + serverTimeZone * 3600

这样无论在哪个地区或国家,都将显示首尔的时候,与服务器显示的时间就同步上了。

 

为什么要一直显示服务器的时间呢?

游戏中有很多功能是有时间限制的,比如运营活动,或者功能开启。如果用本地时间就不好控制,统一用服务器时间避免了很多问题。

可是也容易遇到一个坑,运营配置的活动时间都是针对当前服务器的时间,例如某个活动的截止时间是:2018-10-08 00:00:00,游戏需要显示活动截止倒计时。

 

通常的做法: ployEndTime – serverTime,得到一个秒数,然后将秒转成:xx天xx小时xx分xx秒

serverTime 是固定的,可是ployEndTime就容易出错,为什么?

serverTime 是在东9区 —— 首尔的时间,而 os.time({year=…}) 是根据本地时间来算时间的,这中间就存在问题。有一个时差的问题,之前计算一直用的是serverTimeZone —— 一个固定值,而我当前处于地区或国家,它相对于UTC的时区不确定的,怎么办?

用 (currTimeZone – serverTimeZone) * 3600 / 秒,os.time()之后再加上这个时区差就是首尔当前的时间戳了。国内东8 - 东9  = -1,也就是要减去一个1时区,最终将得到首尔地区的时间戳,再减去 serverTime 就是剩下的秒数了,然后将它转为 xx 天 xx 小时 xx 分 xx 秒。

 

 

最后小结一下:

1)os.time({year=xx}),这个时间算出来的是针对当前所处时区的那个时间戳。

2)os.date(“!*t”, 时间戳) 得到的是UTC(时区为0)的时间戳。

3)获取当前时区的值,可以通过文章开头的 getTimeZone 方法

4)想显示固定时区的时间(例如无论在哪都显示服务器的时间),只需要将(服务器)时间戳(秒),通过第2步的方法,得到 UTC 再加上固定的时区差

5)计算倒计时的时候,需要考虑到 os.time 是取当前时区,需要再将当前时区减去目标时区,再计划时间戳

6)夏令时,本身已经拨快了一个小时,当需要显示为固定时区的时间,则需要减去一个小时