I'm most likely interested and this could help others looking for this.
Any thing helps :)
I found this module scrolling through the dev forum
local Mod = {}
local Months = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
Names = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
}
}
local Days = { -- January 2015
"Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday"
}
local DayInSeconds = 60 * 60 * 24
local DateFuncs;
DateFuncs = {
GetString = function(DateObject, DateFormat)
assert(type(DateObject)=="userdata",
"Invalid argument #1 (UTC-Date expected, got " .. type(DateObject) .. ")")
assert(DateObject.ClassName == "UTC-Date",
"Invalid argument #1 (UTC-Date expected, got " .. DateObject.ClassName .. ")")
-- FORMATS:
-- --------
-- MM: MONTH
-- DD: DAY
-- YY: YEAR (last 2)
-- YYYY: YEAR (full)
-- Mm: MONTH NAME
-- Dd: DAY NAME
-- ss: SECOND
-- mm: MINUTE
-- hh: HOUR
-- I$: USE MILITARY TIME
DateFormat = type(DateFormat) == "string" and DateFormat or "MM/DD/YYYY hh:mm:ss"
local isMilitary = DateFormat:match(" I$")
local doubleDigitFmt = "d"
local fmt = string.format
local meridiem = "AM"
local R = DateFormat
R = R:gsub("MM", DateObject.Month)
--print(R)
R = R:gsub("DD", DateObject.Day)
R = R:gsub("YYYY", DateObject.Year)
R = R:gsub("YY", tostring(DateObject.Year):sub(-2))
R = R:gsub("Mm", DateObject.MonthName)
R = R:gsub("Dd", DateObject.DayName)
if isMilitary then
R = R:gsub("hh", fmt(doubleDigitFmt, DateObject.Hour))
R = R:sub(1, #R-2)
else
local h = DateObject.Hour
if h > 12 then
h = h - 12
meridiem = "PM"
elseif h == 0 then
h = 12
elseif h == 12 then
meridiem = "PM"
end
R = R:gsub("ss", fmt(doubleDigitFmt, DateObject.Second) .. " " .. meridiem)
R = R:gsub("mm", fmt(doubleDigitFmt, DateObject.Minute))
R = R:gsub("hh", fmt(doubleDigitFmt, h))
end
return R
end,
LessThan = function(Date1, Date2)
return Date1.Tick < Date2.Tick
end,
LessOrEqual = function(Date1, Date2)
return Date1.Tick <= Date2.Tick
end,
GreaterThan = function(Date1, Date2)
return not Date1:LessOrEqual(Date2)
end,
GreaterOrEqual = function(Date1, Date2)
return not Date1:LessThan(Date2)
end,
Equal = function(Date1, Date2)
return Date1.Tick == Date2.Tick
end,
NotEqual = function(Date1, Date2)
return not Date1:Equal(Date2)
end,
GetShortDate = function(Date)
return Date:GetString()
end,
GetLongDate = function(Date)
return Date:GetString("Dd, Mm DD, YYYY hh:mm:ss")
end,
}
function Mod.IsLeapYear(Yr)
if Yr % 100 == 0 and Yr % 400 == 0 then
return true
elseif Yr % 4 == 0 and Yr % 100 ~= 0 then
return true
else
return false
end
end
function newDate(Info)
local o = newproxy(true)
local mt = getmetatable(o)
mt.__index = function(t, k)
if Info[k] then
return Info[k]
elseif DateFuncs[k] then
return DateFuncs[k]
else
error("No such property " .. k)
end
end
mt.__newindex = function()
error("UTC-Date objects cannot be edited")
end
mt.__metatable = "The metatable is locked"
mt.__tostring = function(t)
return t:GetString()
end
return o
end
function Mod.GetCurrentDate()
local t = os.time()
local y = math.floor(t / (60 * 60 * 24 * 365.25)) + 1970
local mt = math.ceil((t % (60 * 60 * 24 * 365.25)) / DayInSeconds)
local h = math.floor((t % DayInSeconds) / 3600)
local m = math.floor((t % 3600) / 60)
local s = t % 60
local Di = mt
--print("Days since Jan 1, " .. y .. ":", mt)
local mo, d, mN, dN
for M, D in ipairs(Months) do
if M == 2 and Mod.IsLeapYear(y) then
D = D + 1
end
if mt - D > 0 then
mt = mt - D
elseif mt - D == 0 then
mo = M + 1
d = mt - D + 1
break
else
mo = M
d = mt + 1
break
end
--print("Time left:", mt)
end
--print(Di)
Di = Di%7 == 0 and 1 or Di%7
mN = Months.Names[mo]
dN = Days[Di]
--[[
print("Month:", mo, "(", mN, ")")
print("Day:", d, "(", dN, ")")
print("Year:" .. y)
print("Hour:", h)
print("Min:", m)
print("Sec:", s)
]]
local Date = {}
Date.Day = d
Date.Month = mo
Date.Year = y
Date.ClassName = "UTC-Date"
Date.DayName = dN
Date.MonthName = mN
Date.Hour = h
Date.Minute = m
Date.Second = s
Date.Tick = t
return newDate(Date)
end
Mod.Help = function(Mode)
local HelpStr;
if not Mode then
HelpStr = [[
UTC-Time Help
---------------------
Module.Help("format")
--
Prints help information on various format strings for the DateObject.
Module.Help("dateobject")
--
Prints help information on the DateObject - Properties and Methods.
]]
elseif Mode:lower() == "format" then
HelpStr = [[
Date Formats
---------------------
MM: MONTH
DD: DAY
YY: YEAR (last 2)
YYYY: YEAR (full)
Mm: MONTH NAME
Dd: DAY NAME
ss: SECOND
mm: MINUTE
hh: HOUR
I$: USE MILITARY TIME
Examples (< in, > out):
< Dd, Mm DD, YYYY hh:mm:ss
> Sunday, January 4, 2015 12:34:05 AM
----
< Dd
> Sunday
----
< Dd MM/DD/YY
> Sunday 1/4/15
----
< Dd MM/DD/YYY
> Sunday 1/4/2015
----
< Today is Dd, and the time is hh:mm:ss! I
> Today is Sunday, and the time is 00:34:05!
]]
elseif Mode:lower() == "dateobject" then
HelpStr = [[
DateObject
---------------------
Properties :
Tick - The os.time() value associated with the Date
Day - The numerical day value (1-28/29/20/31)
DayName - The name of the current day
Month - The numerical month value (1-12)
MonthName - The name of the current month
Year - The numerical year value
Hour - The current hour (0-23)
Minute - The current minute (0-59)
Second - The current second (0-59)
ClassName - The class of the object (UTC-Date)
----
Methods :
GetString(Date [, Format])
----
Retrieves a string of the Date following Format or 'MM/DD/YYYY hh:mm:ss'
if no Format is supplied. To view help on formatting, use Mod.Help("format").
GetShortDate(Date)
----
Retrieves a string of the Date following 'MM/DD/YYYY hh:mm:ss'. To view
help on formatting, use Mod.Help("format").
GetLongDate(Date)
----
Retrieves a string of the Date following 'Dd, Mm DD, YYYY hh:mm:ss'. To view
help on formatting, use Mod.Help("format").
LessThan(Date1, Date2)
----
Returns Date1 < Date2.
GreaterThan(Date1, Date2)
----
Returns Date2 <= Date1.
LessOrEqual(Date1, Date2)
----
Returns Date1 <= Date2.
GreaterOrEqual(Date1, Date2)
----
Returns Date2 < Date1.
Equal
----
Returns Date1 == Date2
NotEqual
----
Returns Date1 ~= Date2
]]
end
print(HelpStr)
end
return Mod
