Modul:Time

Technology
12 hours ago
8
4
2
Avatar
Author
Albert Flores

require "Modul:No globals"

local Time = {}

-- Check if a value is a number in the given range @param mixed value @param number min @param number max @return boolean -- local function validateNumberInRange( value, min, max ) return type( value ) == 'number' and value >= min and value

'number' or (definition.year

nil and precision == Time.PRECISION.DAY)) then return false end if definition.precision

nil then return Time.PRECISION.YEAR elseif definition.day

nil then return Time.PRECISION.MONTH elseif definition.hour

nil then return Time.PRECISION.DAY elseif definition.minute

nil then return Time.PRECISION.HOUR elseif definition.second == nil then return Time.PRECISION.MINUTE else return Time.PRECISION.SECOND end end

-- Try to find the relevant calendar for a time definition @param table time definition @return string the calendar name -- local function guessCalendar( definition ) if definition. year ~= nil and definition. +moreyear Time. PRECISION. MONTH then return Time. CALENDAR. JULIAN else return Time. CALENDAR. GREGORIAN end end.

-- Parse an ISO 8601 string and return it as a time definition @param string iso the iso datetime @param boolean withoutRecurrence concider date in the format XX-XX as year-month and not month-day @return table -- local function parseIso8601( iso, withoutRecurrence ) local definition = {}

--Split date and time iso = mw.text.trim( iso:upper ) local beginMatch, endMatch, date, time, offset = iso:find( '([%+%-]?[%d%-]+)[T ]?([%d%.:]*)([Z%+%-]?[%d:]*)' )

if beginMatch ~= 1 or endMatch ~= iso:len then --iso is not a valid ISO string return {} end

--date if date ~= nil then local isBC = false if date:sub( 1, 1 ) == '-' then isBC = true date = date:sub( 2, date:len ) end local parts = mw. text. +moresplit( date, '-' ) if not withoutRecurrence and table. maxn( parts ).

2 and parts[1]:len

2 then --MM-DD case definition. month = tonumber( parts[1] ) definition. +moreday = tonumber( parts[2] ) else if isBC then definition. year = -1 * tonumber( parts[1] ) - 1 --Years BC are counted since 0 and not -1 else definition. year = tonumber( parts[1] ) end definition. month = tonumber( parts[2] ) definition. day = tonumber( parts[3] ) end end.

--time if time ~= nil then local parts = mw.text.split( time, ':' ) definition.hour = tonumber( parts[1] ) definition.minute = tonumber( parts[2] ) definition.second = tonumber( parts[3] ) end

--ofset if offset ~= nil then if offset == 'Z' then definition.utcoffset = '+00:00' else definition.utcoffset = offset end end

return definition end

-- Format UTC offset for ISO output @param string offset UTC offset @return string UTC offset for ISO -- local function formatUtcOffsetForIso( offset ) if offset == '+00:00' then return 'Z' else return offset end end

-- Prepend as mutch as needed the character c to the string str in order to to have a string of length length @param mixed str @param string c @param number length @return string -- local function prepend(str, c, length) str = tostring( str ) while str:len node) @param mw. language|string|nil language to use. +more By default the content language. @param table|nil attributes table of attributes to add to the node. @return string ]]-- function Time:toHtml( language, attributes ) if attributes == nil then attributes = {} end attributes['datetime'] = self:toIso8601 return mw. text. tag( 'time', attributes, self:toString( language ) ) end.

-- All possible precisions for a Time (same ids as Wikibase) -- Time.PRECISION = { GY = 0, --Gigayear MY100 = 1, --100 Megayears MY10 = 2, --10 Megayears MY = 3, --Megayear KY100 = 4, --100 Kiloyears KY10 = 5, --10 Kiloyears KY = 6, --Kiloyear YEAR100 = 7, --100 years YEAR10 = 8, --10 years YEAR = 9, MONTH = 10, DAY = 11, HOUR = 12, MINUTE = 13, SECOND = 14 }

-- Check if the precision is known @param number precision ID @return boolean -- function Time. knowsPrecision( precision ) for _, id in pairs( Time. +morePRECISION ) do if id == precision then return true end end return false end.

-- Supported calendar models -- Time.CALENDAR = { GREGORIAN = 'Gregorian', JULIAN = 'Julian' }

return Time

5 min read
Share this post:
Like it 8

Leave a Comment

Please, enter your name.
Please, provide a valid email address.
Please, enter your comment.
Enjoy this post? Join Cesko.wiki
Don’t forget to share it
Top