webmcp
annotate framework/env/format/time.lua @ 33:fc98e5c6bfab
add missing debug_table
trace.debug_table prints a table into the log.
trace.debug_table prints a table into the log.
author | Daniel Poelzleithner <poelzi@poelzi.org> |
---|---|
date | Tue Oct 05 05:19:47 2010 +0200 (2010-10-05) |
parents | 9fdfb27f8e67 |
children | fd31e0aa629a |
rev | line source |
---|---|
jbe/bsw@0 | 1 --[[-- |
jbe/bsw@0 | 2 text = -- text with the value formatted as a time, according to the locale settings |
jbe/bsw@0 | 3 format.time( |
jbe/bsw@0 | 4 value, -- a time, a timestamp or nil |
jbe/bsw@0 | 5 { |
jbe/bsw@0 | 6 nil_as = nil_text -- text to be returned for a nil value |
jbe/bsw@0 | 7 } |
jbe/bsw@0 | 8 ) |
jbe/bsw@0 | 9 |
jbe/bsw@0 | 10 Formats a time or timestamp as a time, according to the locale settings. |
jbe/bsw@0 | 11 |
jbe/bsw@0 | 12 --]]-- |
jbe/bsw@0 | 13 |
jbe/bsw@0 | 14 function format.time(value, options) |
jbe/bsw@0 | 15 local options = options or {} |
jbe/bsw@0 | 16 if value == nil then |
jbe/bsw@0 | 17 return options.nil_as or "" |
jbe/bsw@0 | 18 end |
jbe/bsw@0 | 19 if not ( |
jbe/bsw@0 | 20 atom.has_type(value, atom.time) or |
jbe/bsw@0 | 21 atom.has_type(value, atom.timestamp) |
jbe/bsw@0 | 22 ) then |
jbe/bsw@0 | 23 error("Value passed to format.time(...) is neither a time, a timestamp, nor nil.") |
jbe/bsw@0 | 24 end |
jbe/bsw@0 | 25 if value.invalid then |
jbe/bsw@0 | 26 return "invalid" |
jbe/bsw@0 | 27 end |
jbe/bsw@0 | 28 local result = locale.get("time_format") or "HH:MM{:SS}" |
jbe/bsw@0 | 29 if options.hide_seconds then |
jbe/bsw@0 | 30 result = string.gsub(result, "{[^{|}]*}", "") |
jbe/bsw@0 | 31 else |
jbe/bsw@0 | 32 result = string.gsub(result, "{([^|]*)}", "%1") |
jbe/bsw@0 | 33 end |
jbe/bsw@0 | 34 local am_pm |
jbe/bsw@0 | 35 local hour = value.hour |
jbe/bsw@0 | 36 result = string.gsub(result, "{([^{}]*)|([^{}]*)}", function(am, pm) |
jbe/bsw@0 | 37 if hour > 12 then |
jbe/bsw@0 | 38 am_pm = pm |
jbe/bsw@0 | 39 else |
jbe/bsw@0 | 40 am_pm = am |
jbe/bsw@0 | 41 end |
jbe/bsw@0 | 42 return "{|}" |
jbe/bsw@0 | 43 end) |
jbe/bsw@0 | 44 if am_pm and hour > 12 then |
jbe/bsw@0 | 45 hour = hour - 12 |
jbe/bsw@0 | 46 end |
jbe/bsw@0 | 47 result = string.gsub(result, "HH", function() |
jbe/bsw@0 | 48 return format.decimal(hour, { digits = 2 }) |
jbe/bsw@0 | 49 end) |
jbe/bsw@0 | 50 result = string.gsub(result, "MM", function() |
jbe/bsw@0 | 51 return format.decimal(value.minute, { digits = 2 }) |
jbe/bsw@0 | 52 end) |
jbe/bsw@0 | 53 result = string.gsub(result, "SS", function() |
jbe/bsw@0 | 54 return format.decimal(value.second, { digits = 2 }) |
jbe/bsw@0 | 55 end) |
jbe/bsw@0 | 56 if am_pm then |
jbe/bsw@0 | 57 result = string.gsub(result, "{|}", am_pm) |
jbe/bsw@0 | 58 end |
jbe/bsw@0 | 59 return result |
jbe/bsw@0 | 60 end |