webmcp
view framework/env/format/time.lua @ 2:72860d232f32
Version 1.0.2
Fixed bug with explicit garbage collection (requests > 256kB caused an error)
Views prefixed with an underscore can't be called externally
ui.paginate now displays the last page, if the selected page number is too high.
Fixed bug with explicit garbage collection (requests > 256kB caused an error)
Views prefixed with an underscore can't be called externally
ui.paginate now displays the last page, if the selected page number is too high.
| author | jbe/bsw | 
|---|---|
| date | Thu Dec 10 12:00:00 2009 +0100 (2009-12-10) | 
| parents | 9fdfb27f8e67 | 
| children | fd31e0aa629a | 
 line source
     1 --[[--
     2 text =                 -- text with the value formatted as a time, according to the locale settings
     3 format.time(
     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 a time or timestamp as a time, according to the locale settings.
    12 --]]--
    14 function format.time(value, options)
    15   local options = options or {}
    16   if value == nil then
    17     return options.nil_as or ""
    18   end
    19   if not (
    20     atom.has_type(value, atom.time) or
    21     atom.has_type(value, atom.timestamp)
    22   ) then
    23     error("Value passed to format.time(...) is neither a time, a timestamp, nor nil.")
    24   end
    25   if value.invalid then
    26     return "invalid"
    27   end
    28   local result = locale.get("time_format") or "HH:MM{:SS}"
    29   if options.hide_seconds then
    30     result = string.gsub(result, "{[^{|}]*}", "")
    31   else
    32     result = string.gsub(result, "{([^|]*)}", "%1")
    33   end
    34   local am_pm
    35   local hour = value.hour
    36   result = string.gsub(result, "{([^{}]*)|([^{}]*)}", function(am, pm)
    37     if hour > 12 then
    38       am_pm = pm
    39     else
    40       am_pm = am
    41     end
    42     return "{|}"
    43   end)
    44   if am_pm and hour > 12 then
    45     hour = hour - 12
    46   end
    47   result = string.gsub(result, "HH", function()
    48     return format.decimal(hour, { digits = 2 })
    49   end)
    50   result = string.gsub(result, "MM", function()
    51     return format.decimal(value.minute, { digits = 2 })
    52   end)
    53   result = string.gsub(result, "SS", function()
    54     return format.decimal(value.second, { digits = 2 })
    55   end)
    56   if am_pm then
    57     result = string.gsub(result, "{|}", am_pm)
    58   end
    59   return result
    60 end
