webmcp

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

Impressum / About Us