lua os.time os.date 使用总结

os.time 

lua官方手册描述如下

os.time ([table])

Returns the current time when called without arguments,or a time representing the date and time specified by the given table.This table must have fieldsyear,month, andday,and may have fields hour,min,sec, andisdst(for a description of these fields, see theos.datefunction).

The returned value is a number, whose meaning depends on your system.In POSIX, Windows, and some other systems, this number counts the numberof seconds since some given start time (the "epoch").In other systems, the meaning is not specified,and the number returned bytimecan be used only as an argument todateanddifftime

函数有两种调用方式

 local t = os.time()  得到当前系统的时间戳(UTC +0) ;

 local t = os.time({year = 2020,month = 2,day = 11,hour = 11,min = 24,sec =0 }) 得到2020-2-11 11:24:00 的时间戳(UTC+0),这里需要注意的是如果以table 方式传入当前的日历时间(年月日时分秒),函数会假定你传入的时间是本地时间(带时区),会在转化的时候减去系统的时区来进行转换!

源码如下

static int os_time (lua_State *L) {

  time_t t;

  if (lua_isnoneornil(L, 1))  /* called without args? */

    t = time(NULL);  /* get current time */

  else {

    struct tm ts;

    luaL_checktype(L, 1, LUA_TTABLE);

    lua_settop(L, 1);  /* make sure table is at the top */

    ts.tm_sec = getfield(L, "sec", 0);

    ts.tm_min = getfield(L, "min", 0);

    ts.tm_hour = getfield(L, "hour", 12);

    ts.tm_mday = getfield(L, "day", -1);

    ts.tm_mon = getfield(L, "month", -1) - 1;

    ts.tm_year = getfield(L, "year", -1) - 1900;

    ts.tm_isdst = getboolfield(L, "isdst");

    t = mktime(&ts);

  }

  if (t == (time_t)(-1))

    lua_pushnil(L);

  else

    lua_pushnumber(L, (lua_Number)t);

  return 1;

}

从上面的源码可以看出 不传入参数的时候使用的是C函数 time(NULL) 返回,带参数的时候通过mktime 来转化返回,mktime 就是带了时区转化!

os.date

 lua的官方描述

os.date ([format [, time]])

Returns a string or a table containing date and time,formatted according to the given stringformat.

If thetimeargument is present,this is the time to be formatted(see theos.timefunction for a description of this value).Otherwise,dateformats the current time.

Ifformatstarts with '!',then the date is formatted in Coordinated Universal Time.After this optional character,ifformatis the string "*t",then date returns a table with the following fields:year(four digits),month(1--12),day(1--31),hour(0--23),min(0--59),sec(0--61),wday(weekday, Sunday is 1),yday(day of the year),andisdst(daylight saving flag, a boolean).

Ifformatis not "*t",thendatereturns the date as a string,formatted according to the same rules as the C functionstrftime.

When called without arguments,datereturns a reasonable date and time representation that depends onthe host system and on the current locale(that is,os.date()is equivalent toos.date("%c")).

这里支持了很多种格式化的方式来输出

这里主要记录两种方式的使用

local t = os.date("*t",time) 和 local t = os.date("!*t",time)

即是否带 "!" 的转化方式:如果没有带 "!" 则代表转化的时候需要进行时区的转换;带了则不需要进行时区的转换

源码如下

static int os_date (lua_State *L) {

  const char *s = luaL_optstring(L, 1, "%c");

  time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));

  struct tm *stm;

  if (*s == '!') {  /* UTC? */

    stm = gmtime(&t);

    s++;  /* skip `!' */

  }

  else

    stm = localtime(&t);

  if (stm == NULL)  /* invalid date? */

    lua_pushnil(L);

  else if (strcmp(s, "*t") == 0) {

    lua_createtable(L, 0, 9);  /* 9 = number of fields */

    setfield(L, "sec", stm->tm_sec);

    setfield(L, "min", stm->tm_min);

    setfield(L, "hour", stm->tm_hour);

    setfield(L, "day", stm->tm_mday);

    setfield(L, "month", stm->tm_mon+1);

    setfield(L, "year", stm->tm_year+1900);

    setfield(L, "wday", stm->tm_wday+1);

    setfield(L, "yday", stm->tm_yday+1);

    setboolfield(L, "isdst", stm->tm_isdst);

  }

  else {

    char cc[3];

    luaL_Buffer b;

    cc[0] = '%'; cc[2] = '\0';

    luaL_buffinit(L, &b);

    for (; *s; s++) {

      if (*s != '%' || *(s + 1) == '\0')  /* no conversion specifier? */

        luaL_addchar(&b, *s);

      else {

        size_t reslen;

        char buff[200];  /* should be big enough for any conversion result */

        cc[1] = *(++s);

        reslen = strftime(buff, sizeof(buff), cc, stm);

        luaL_addlstring(&b, buff, reslen);

      }

    }

    luaL_pushresult(&b);

  }

  return 1;

}

   带了 "!"  的时候则 调用的是C函数 gmtime 简单的把time 时间戳转化成 日历时间,不带的时候就先调用了localtime 来进行转换,这里就把时区带进来进行转换的!

你可能感兴趣的:(lua os.time os.date 使用总结)