Advertisement
psycholyzern

date timezone function

Jul 23rd, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.11 KB | None | 0 0
  1. -- look here for time formatting http://www.lua.org/pil/22.1.html
  2.  
  3.  
  4. local offset = "+8.00" --put your timezone value
  5.  
  6. local function format_time(timestamp, format, tzoffset, tzname)
  7.    if tzoffset == "local" then  -- calculate local time zone (for the server)
  8.       local now = os.time()
  9.       local local_t = os.date("*t", now)
  10.       local utc_t = os.date("!*t", now)
  11.       local delta = (local_t.hour - utc_t.hour)*60 + (local_t.min - utc_t.min)
  12.       local h, m = math.modf( delta / 60)
  13.       tzoffset = string.format("%+.4d", 100 * h + 60 * m)
  14.    end
  15.    tzoffset = tzoffset or "GMT"
  16.    format = format:gsub("%%z", tzname or tzoffset)
  17.    if tzoffset == "GMT" then
  18.       tzoffset = "+0000"
  19.    end
  20.    tzoffset = tzoffset:gsub(":", "")
  21.  
  22.    local sign = 1
  23.    if tzoffset:sub(1,1) == "-" then
  24.       sign = -1
  25.       tzoffset = tzoffset:sub(2)
  26.    elseif tzoffset:sub(1,1) == "+" then
  27.       tzoffset = tzoffset:sub(2)
  28.    end
  29.    tzoffset = sign * (tonumber(tzoffset:sub(1,2))*60 +
  30. tonumber(tzoffset:sub(3,4)))*60
  31.    return os.date(format, timestamp)
  32. end
  33.  
  34. local date = format_time(os.time(), "!%Y-%m-%d", offset)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement