local DateTimeWrapper = {}
DateTimeWrapper.__index = DateTimeWrapper
local function create_proxy(wrapper_methods)
local mt = {}
mt.__index = function(self, key)
if wrapper_methods[key] ~= nil then
return wrapper_methods[key]
end
local val = rawget(self, key)
if val ~= nil then
return val
end
local underlying = rawget(self, "_obj")
local v = underlying[key]
if type(v) == "function" then
return function(_, ...)
return v(underlying, ...)
end
else
return v
end
end
mt.__newindex = function(self, key, value)
if wrapper_methods[key] ~= nil or rawget(self, key) ~= nil then
rawset(self, key, value)
else
local underlying = rawget(self, "_obj")
local setter = underlying["set_" .. key]
if type(setter) == "function" then
setter(underlying, value)
else
underlying[key] = value
end
end
end
mt.__tostring = function(self)
local u = rawget(self, "_obj")
return (u.to_string and u:to_string()) or tostring(u)
end
mt.__concat = function(a, b)
return tostring(a) .. tostring(b)
end
return mt
end
local function wrap(obj, wrapper_methods)
assert(type(obj) == "userdata" or type(obj) == "table", "Can only wrap userdata or tables")
local proxy = { _obj = obj }
setmetatable(proxy, create_proxy(wrapper_methods or {}))
return proxy
end
local function new_datetime(differentiator, month, day, hour, min, sec, milli)
if type(differentiator) == "string" then
return astra_internal__datetime_new_parse(differentiator)
elseif type(differentiator) == "number" then
return astra_internal__datetime_new_from(differentiator, month, day, hour, min, sec, milli)
else
return astra_internal__datetime_new_now()
end
end
Astra.datetime = {}
function Astra.datetime.new(differentiator, month, day, hour, min, sec, milli)
local real_dt = new_datetime(differentiator, month, day, hour, min, sec, milli)
return wrap(real_dt, DateTimeWrapper)
end