webmcp

annotate framework/env/format/interval.lua @ 106:bbfbbddf13ad

Support for intervals
author jbe
date Sun Nov 04 04:55:22 2012 +0100 (2012-11-04)
parents
children
rev   line source
jbe@106 1 --[[--
jbe@106 2 text = -- text with the value formatted as a time, according to the locale settings
jbe@106 3 format.interval(
jbe@106 4 value, -- a time, a timestamp or nil
jbe@106 5 {
jbe@106 6 nil_as = nil_text -- text to be returned for a nil value
jbe@106 7 }
jbe@106 8 )
jbe@106 9
jbe@106 10 Formats an interval, according to the locale settings.
jbe@106 11
jbe@106 12 --]]--
jbe@106 13
jbe@106 14 function format.interval(value, options)
jbe@106 15 local options = options or {}
jbe@106 16 if value == nil then
jbe@106 17 return options.nil_as or ""
jbe@106 18 end
jbe@106 19 if not atom.has_type(value, atom.interval) then
jbe@106 20 error("Value passed to format.interval(...) is neither an interval, nor nil.")
jbe@106 21 end
jbe@106 22 if value.invalid then
jbe@106 23 return "invalid"
jbe@106 24 end
jbe@106 25 local parts = {}
jbe@106 26 if value.years ~= 0 then
jbe@106 27 parts[#parts+1] = tostring(value.years)
jbe@106 28 if value.years == 1 or value.years == -1 then
jbe@106 29 parts[#parts+1] = "year" -- TODO: localization
jbe@106 30 else
jbe@106 31 parts[#parts+1] = "years" -- TODO: localization
jbe@106 32 end
jbe@106 33 end
jbe@106 34 if value.months ~= 0 then
jbe@106 35 parts[#parts+1] = tostring(value.months)
jbe@106 36 if value.months == 1 or value.months == -1 then
jbe@106 37 parts[#parts+1] = "month" -- TODO: localization
jbe@106 38 else
jbe@106 39 parts[#parts+1] = "months" -- TODO: localization
jbe@106 40 end
jbe@106 41 end
jbe@106 42 if value.days ~= 0 then
jbe@106 43 parts[#parts+1] = tostring(value.days)
jbe@106 44 if value.days == 1 or value.days == -1 then
jbe@106 45 parts[#parts+1] = "day" -- TODO: localization
jbe@106 46 else
jbe@106 47 parts[#parts+1] = "days" -- TODO: localization
jbe@106 48 end
jbe@106 49 end
jbe@106 50 if options.hide_seconds then
jbe@106 51 parts[#parts+1] = string.format(
jbe@106 52 "%02i:%02i",
jbe@106 53 value.hours,
jbe@106 54 math.abs(value.minutes) -- NOTE: hours and minutes always have equal sign
jbe@106 55 )
jbe@106 56 else
jbe@106 57 parts[#parts+1] = string.format(
jbe@106 58 "%02i:%02i:%02i",
jbe@106 59 value.hours,
jbe@106 60 math.abs(value.minutes), -- NOTE: hours and minutes always have equal sign
jbe@106 61 math.abs(value.seconds) -- NOTE: hours and seconds always have equal sign
jbe@106 62 )
jbe@106 63 end
jbe@106 64 return table.concat(parts, " ")
jbe@106 65 end

Impressum / About Us