webmcp

annotate framework/env/format/date.lua @ 466:2751b6b81c23

Minor efficiency enhancement in <db_object>:try_save() method
author jbe
date Mon Nov 07 19:32:51 2016 +0100 (2016-11-07)
parents 9fdfb27f8e67
children
rev   line source
jbe/bsw@0 1 --[[--
jbe/bsw@0 2 text = -- text with the value formatted as a date, according to the locale settings
jbe/bsw@0 3 format.date(
jbe/bsw@0 4 value, -- a date, 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 date or timestamp as a date, according to the locale settings.
jbe/bsw@0 11
jbe/bsw@0 12 --]]--
jbe/bsw@0 13
jbe/bsw@0 14 function format.date(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.date) 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.date(...) is neither a date, 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("date_format") or "YYYY-MM-DD"
jbe/bsw@0 29 result = string.gsub(result, "YYYY", function()
jbe/bsw@0 30 return format.decimal(value.year, { digits = 4 })
jbe/bsw@0 31 end)
jbe/bsw@0 32 result = string.gsub(result, "YY", function()
jbe/bsw@0 33 return format.decimal(value.year % 100, { digits = 2 })
jbe/bsw@0 34 end)
jbe/bsw@0 35 result = string.gsub(result, "Y", function()
jbe/bsw@0 36 return format.decimal(value.year)
jbe/bsw@0 37 end)
jbe/bsw@0 38 result = string.gsub(result, "MM", function()
jbe/bsw@0 39 return format.decimal(value.month, { digits = 2 })
jbe/bsw@0 40 end)
jbe/bsw@0 41 result = string.gsub(result, "M", function()
jbe/bsw@0 42 return format.decimal(value.month)
jbe/bsw@0 43 end)
jbe/bsw@0 44 result = string.gsub(result, "DD", function()
jbe/bsw@0 45 return format.decimal(value.day, { digits = 2 })
jbe/bsw@0 46 end)
jbe/bsw@0 47 result = string.gsub(result, "D", function()
jbe/bsw@0 48 return format.decimal(value.day)
jbe/bsw@0 49 end)
jbe/bsw@0 50 return result
jbe/bsw@0 51 end

Impressum / About Us