Skip to content

Instantly share code, notes, and snippets.

@cristobal
Last active December 15, 2015 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cristobal/5257220 to your computer and use it in GitHub Desktop.
Save cristobal/5257220 to your computer and use it in GitHub Desktop.
Port of php.js date to lua http://phpjs.org/functions/date/
function date (format, timestamp)
-- @credits https://raw.github.com/kvz/phpjs/master/functions/datetime/date.js
local c = {}
local f = nil
local formatPattern = "%a"
local pad = function (v, s)
v = tostring(v)
if string.len(v) < s then
return c.pad("0" .. v, s)
end
return v
end
c.pad = pad
local unpad = function(v)
v = string.gsub(v, "^0", "")
if string.find(v, "^0") then
return c.unpad(v)
end
return v
end
c.unpad = unpad
local parseInt = function (v)
return math.floor(tonumber(v, 10))
end
local round = function (num)
if num >= 0 then
return math.floor(num + .5)
else
return math.ceil(num - .5)
end
end
local txt_ordinals = {
'st', 'nd', 'rd', 'th'
}
local formatChrCb = function (chr)
if f[chr] ~= nil then
return f[chr]()
end
return chr
end
f = {
-- [Day]--
-- Day of month w/leading 0; 01..31
d = function ()
return os.date("%d", c.time)
end,
-- shorthand day name; Mon..Sun
D = function ()
return os.date("%a", c.time)
end,
-- Day of month; 1..31
j = function ()
return unpad(f.d())
end,
-- Full day name; Monday...Sunday
l = function ()
return os.date("%A", c.time)
end,
-- ISO-8601 day of week; 1[Mon]..7[Sun]
N = function ()
local d = f.w()
return (parseInt(d) > 0 and d or "7")
end,
-- Ordinal suffix for day of month; st, nd, rd, th
S = function ()
local j = parseInt(f.j())
local i = j % 10
if (i <= 3) and (parseInt((j % 100) / 10) == 1) then
i = 1
end
if i > 4 then
i = 4
end
return txt_ordinals[i]
end,
-- Day of week; 0[Sun]..6[Sat]
w = function ()
return os.date("%w", c.time)
end,
-- Day of year; 0..365
z = function ()
local t = os.date("*t", c.time)
return tostring(t.yday - 1)
end,
-- [Week] --
W = function ()
local m = parseInt(f.n()) -- - 1
local d = parseInt(f.j()) - parseInt(f.N()) + 3
local t = os.time({year = f.Y(), month = m, day = d})
local t2 = os.time({year = os.date("%Y", t), month = 1, day = 4})
local v = 1 + round((t - t2) / 864e2 / 7);
return pad(v, 2)
end,
-- [Month] --
-- Full month name; January...December
F = function ()
return os.date("%B", c.time)
end,
-- Month w/leading 0; 01...12
m = function ()
return os.date("%m", c.time)
end,
-- Shorthand month name; Jan...Dec
M = function ()
return os.date("%b", c.time)
end,
-- Month; 1...12
n = function ()
return unpad(f.m())
end,
-- Days in month; 28..31
t = function ()
local y = parseInt(f.Y())
local m = parseInt(f.n())
if m == 12 then
t = os.time({year = y + 1, month = 1, day = 0})
else
t = os.time({year = f.Y(), month = f.n() + 1, day = 0})
end
return unpad(os.date("%d", t))
end,
-- [Year] --
-- Is leap year?; 0 or 1
L = function ()
local j = parseInt(f.Y())
if ((j % 4) == 0) and ((j % 100) ~= 0) or ((j % 400) == 0) then
return "1"
end
return "0"
end,
-- ISO-8601 year
o = function ()
local n = parseInt(f.n())
local W = parseInt(f.W())
local Y = parseInt(f.Y())
local o = 0
if (n == 12) and (W < 9) then
o = 1
elseif (n == 1) and (W > 9) then
o = -1
end
return tostring(Y + o)
end,
-- Full year; e.g. 1980...2010
Y = function ()
return os.date("%Y", c.time)
end,
--
y = function ()
return os.date("%y", c.time)
end,
-- am or pm
a = function ()
return string.lower(f.A())
end,
-- AM or PM
A = function ()
return os.date("%p", c.time)
end,
-- Swatch Internet time; 000..999
-- TODO: check this function more precisely
B = function ()
local H = parseInt(f.H()) * 36e2 -- hours
local i = parseInt(f.i()) * 60 -- minutes
local s = parseInt(f.s()) -- seconds
local v = ((H + i + s + 36e2) / 86.4) % 1e3
return tostring(v)
end,
-- 12-Hours; 1..12
g = function ()
return unpad(f.h())
end,
-- 24-Hours; 0..23
G = function ()
return unpad(f.H())
end,
-- 12-Hours w/leading 0; 01..12
h = function ()
return os.date("%I", c.time)
end,
-- 24-Hours w/leading 0; 00..23
H = function ()
return os.date("%H", c.time)
end,
-- Minutes w/leading 0; 00..59
i = function ()
return os.date("%M", c.time)
end,
-- Seconds w/leading 0; 00..59
s = function ()
return os.date("%S", c.time)
end,
-- Microseconds; 000000-999000; will always return 000000
u = function ()
return "000000"
end,
-- Timezone
e = function ()
error("Not supported")
end,
-- DST observed?; 0 or 1
I = function ()
error("Not supported")
-- -- Compares Jan 1 minus Jan 1 UTC to Jul 1 minus Jul 1 UTC.
-- -- If they are not equal, then DST is observed.
-- local a = os.time({year = f.Y(), month = 1, day = 1}), -- Jan 1
-- local c = os.time({year = f.Y(), month = 1, day = 1}), -- Jan 1 UTC
-- local b = os.time({year = f.Y(), month = 6, day = 1}), -- Jul 1
-- local d = os.time({year = f.Y(), month = 6, day = 1}), -- Jul 1 UTC
--
-- if ((a - c) != (b - d) then
-- return "1"
-- end
--
-- return "0"
end,
-- Difference to GMT in hour format; e.g. +0200
O = function ()
error("Not supported")
-- local tzo = getTimezoneOffset()
-- local a = math.abs(tzo);
-- return (tzo > 0 ? "-" : "+") + pad(math.floor(a / 60) * 100 + a % 60, 4);
end,
-- Difference to GMT w/colon; e.g. +02:00
P = function ()
error("Not supported")
-- local O = f.O();
-- return (O.substr(0, 3) + ":" + O.substr(3, 2));
end,
-- Timezone abbreviation; e.g. EST, MDT, ...
T = function ()
error("Not supported")
-- return 'UTC'
end,
-- Timezone offset in seconds (-43200...50400)
Z = function ()
error("Not supported")
-- return -getTimezoneOffset() * 60;
end,
--[ Full Date/Time]--
-- ISO-8601 date.
c = function ()
return date('Y-m-d\\TH:i:sP', c.time)
end,
-- RFC 2822
r = function ()
return date('D, d M Y H:i:s O', c.time)
end,
-- Seconds since UNIX epoch
U = function ()
return c.time
end
}
c.date = function (format, timestamp)
c.time = timestamp or os.time()
return string.gsub(format, formatPattern, formatChrCb)
end
return c.date(format, timestamp)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment