jbe@106: --[[-- jbe@106: text = -- text with the value formatted as a time, according to the locale settings jbe@106: format.interval( jbe@106: value, -- a time, a timestamp or nil jbe@106: { jbe@106: nil_as = nil_text -- text to be returned for a nil value jbe@106: } jbe@106: ) jbe@106: jbe@106: Formats an interval, according to the locale settings. jbe@106: jbe@106: --]]-- jbe@106: jbe@106: function format.interval(value, options) jbe@106: local options = options or {} jbe@106: if value == nil then jbe@106: return options.nil_as or "" jbe@106: end jbe@106: if not atom.has_type(value, atom.interval) then jbe@106: error("Value passed to format.interval(...) is neither an interval, nor nil.") jbe@106: end jbe@106: if value.invalid then jbe@106: return "invalid" jbe@106: end jbe@106: local parts = {} jbe@106: if value.years ~= 0 then jbe@106: parts[#parts+1] = tostring(value.years) jbe@106: if value.years == 1 or value.years == -1 then jbe@106: parts[#parts+1] = "year" -- TODO: localization jbe@106: else jbe@106: parts[#parts+1] = "years" -- TODO: localization jbe@106: end jbe@106: end jbe@106: if value.months ~= 0 then jbe@106: parts[#parts+1] = tostring(value.months) jbe@106: if value.months == 1 or value.months == -1 then jbe@106: parts[#parts+1] = "month" -- TODO: localization jbe@106: else jbe@106: parts[#parts+1] = "months" -- TODO: localization jbe@106: end jbe@106: end jbe@106: if value.days ~= 0 then jbe@106: parts[#parts+1] = tostring(value.days) jbe@106: if value.days == 1 or value.days == -1 then jbe@106: parts[#parts+1] = "day" -- TODO: localization jbe@106: else jbe@106: parts[#parts+1] = "days" -- TODO: localization jbe@106: end jbe@106: end jbe@106: if options.hide_seconds then jbe@106: parts[#parts+1] = string.format( jbe@106: "%02i:%02i", jbe@106: value.hours, jbe@106: math.abs(value.minutes) -- NOTE: hours and minutes always have equal sign jbe@106: ) jbe@106: else jbe@106: parts[#parts+1] = string.format( jbe@106: "%02i:%02i:%02i", jbe@106: value.hours, jbe@106: math.abs(value.minutes), -- NOTE: hours and minutes always have equal sign jbe@106: math.abs(value.seconds) -- NOTE: hours and seconds always have equal sign jbe@106: ) jbe@106: end jbe@106: return table.concat(parts, " ") jbe@106: end