Modul:DateTime
Vzhled
Dokumentaci tohoto modulu lze vytvořit na stránce Nápověda:Modul:DateTime
-- @brief
-- Date and time utilities.
--
-- @author
-- [[meta:User:Danny B.]]
local DateTime = {}
----------------------------------------
DateTime = {
days = {
{
name = mw.message.new( "sunday" ):plain(),
abbr = mw.message.new( "sun" ):plain()
}, {
name = mw.message.new( "monday" ):plain(),
abbr = mw.message.new( "mon" ):plain()
}, {
name = mw.message.new( "tuesday" ):plain(),
abbr = mw.message.new( "tue" ):plain()
}, {
name = mw.message.new( "wednesday" ):plain(),
abbr = mw.message.new( "wed" ):plain()
}, {
name = mw.message.new( "thursday" ):plain(),
abbr = mw.message.new( "thu" ):plain()
}, {
name = mw.message.new( "friday" ):plain(),
abbr = mw.message.new( "fri" ):plain()
}, {
name = mw.message.new( "saturday" ):plain(),
abbr = mw.message.new( "sat" ):plain()
}
},
months = {
{
name = mw.message.new( "january" ):plain(),
abbr = mw.message.new( "jan" ):plain(),
genitive = mw.message.new( "january-gen" ):plain()
}, {
name = mw.message.new( "february" ):plain(),
abbr = mw.message.new( "feb" ):plain(),
genitive = mw.message.new( "february-gen" ):plain()
}, {
name = mw.message.new( "march" ):plain(),
abbr = mw.message.new( "mar" ):plain(),
genitive = mw.message.new( "march-gen" ):plain()
}, {
name = mw.message.new( "april" ):plain(),
abbr = mw.message.new( "apr" ):plain(),
genitive = mw.message.new( "april-gen" ):plain()
}, {
name = mw.message.new( "may_long" ):plain(),
abbr = mw.message.new( "may" ):plain(),
genitive = mw.message.new( "may-gen" ):plain()
}, {
name = mw.message.new( "june" ):plain(),
abbr = mw.message.new( "jun" ):plain(),
genitive = mw.message.new( "june-gen" ):plain()
}, {
name = mw.message.new( "july" ):plain(),
abbr = mw.message.new( "jul" ):plain(),
genitive = mw.message.new( "july-gen" ):plain()
}, {
name = mw.message.new( "august" ):plain(),
abbr = mw.message.new( "aug" ):plain(),
genitive = mw.message.new( "august-gen" ):plain()
}, {
name = mw.message.new( "september" ):plain(),
abbr = mw.message.new( "sep" ):plain(),
genitive = mw.message.new( "september-gen" ):plain()
}, {
name = mw.message.new( "october" ):plain(),
abbr = mw.message.new( "oct" ):plain(),
genitive = mw.message.new( "october-gen" ):plain()
}, {
name = mw.message.new( "november" ):plain(),
abbr = mw.message.new( "nov" ):plain(),
genitive = mw.message.new( "november-gen" ):plain()
}, {
name = mw.message.new( "december" ):plain(),
abbr = mw.message.new( "dec" ):plain(),
genitive = mw.message.new( "december-gen" ):plain()
}
}
}
function DateTime.getCurrentTimestamp()
return DateTime.getTimestamp()
end
function DateTime.getCurrentUtcOffset()
return DateTime.getUtcOffset()
end
function DateTime.getTimestamp( dateTable )
return string.format( "%014d", os.date( "%Y%m%d%H%M%S", os.time( dateTable ) ) )
end
function DateTime.getLocalDate( time )
local utc = os.date( "!*t", time );
local isDst = DateTime.isEuDst( utc );
utc.hour = utc.hour + ( isDst and 2 or 1 );
local result = os.date( "!*t", os.time( utc ) );
result.isdst = isDst;
return result;
end
function DateTime.getUtcOffset( timeDef )
return 1 + ( DateTime.isEuDst( timeDef ) and 1 or 0 )
end
function DateTime.isEuDst( timeDef )
local timestamp
local year
if pcall( os.time, timeDef ) then
timestamp = tonumber( DateTime.getTimestamp( timeDef ) )
year = timeDef and timeDef.year or os.date( "%Y" )
elseif type( timeDef ) == "string" and string.match( timeDef, "%d%d%d%d%d%d%d%d%d%d%d%d%d%d" ) then
timestamp = tonumber( timeDef )
year = string.sub( timeDef, 1, 4 )
else
error( "Date table or timestamp or nil expected." )
end
return (
tonumber( year .. "03" .. ( 31 - ( math.floor( 5 * year / 4 ) + 4 ) % 7 ) .. "010000" ) < timestamp
and
timestamp <= tonumber( year .. "10" .. ( 31 - ( math.floor( 5 * year / 4 ) + 1 ) % 7 ) .. "010000" )
)
end
function DateTime.parseVerbalDate( verbalDate )
local _
local pos
local day
local monthName
local month
local year
local form = ""
local lang = mw.language.new( "cs" )
_, _, day, pos = mw.ustring.find( verbalDate, "^(%d+)%. ()" )
_, _, monthName, pos = mw.ustring.find( verbalDate, "^([^%d ]+) ?()", pos )
_, _, year, pos = mw.ustring.find( verbalDate, "(%d+)$", pos )
if monthName then
monthName = lang:lc( monthName )
for index, value in ipairs( DateTime.months ) do
if ( value.name == monthName or value.genitive == monthName ) then
month = index
break
end
end
month = month or 0
end
form = form .. ( day and "D" or "" ) .. ( month and "M" or "" ) .. ( year and "Y" or "" )
return day, month, year, form
end
----------------------------------------
return DateTime