Esempio n. 1
0
static int os_date (lv_State *L) {
  const char *s = lvL_optstring(L, 1, "%c");
  time_t t = lvL_opt(L, (time_t)lvL_checknumber, 2, time(NULL));
  struct tm *stm;
  if (*s == '!') {  /* UTC? */
    stm = gmtime(&t);
    s++;  /* skip `!' */
  }
  else
    stm = localtime(&t);
  if (stm == NULL)  /* invalid date? */
    lv_pushnil(L);
  else if (strcmp(s, "*t") == 0) {
    lv_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];
    lvL_Buffer b;
    cc[0] = '%'; cc[2] = '\0';
    lvL_buffinit(L, &b);
    for (; *s; s++) {
      if (*s != '%' || *(s + 1) == '\0')  /* no conversion specifier? */
        lvL_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);
        lvL_addlstring(&b, buff, reslen);
      }
    }
    lvL_pushresult(&b);
  }
  return 1;
}
Esempio n. 2
0
static int tconcat (lv_State *L) {
    lv_clearFirstTableValue(L);
    lvL_Buffer b;
    size_t lsep;
    int i, last;
    const char *sep = lvL_optlstring(L, 2, "", &lsep);
    lvL_checktype(L, 1, LV_TTABLE);
    i = lvL_optint(L, 3, 1);
    last = lvL_opt(L, lvL_checkint, 4, lvL_getn(L, 1));
    lvL_buffinit(L, &b);
    for (; i < last; i++) {
        addfield(L, &b, i);
        lvL_addlstring(&b, sep, lsep);
    }
    if (i == last)  /* add last value (if interval was not empty) */
        addfield(L, &b, i);
    lvL_pushresult(&b);
    return 1;
}