jbe/bsw@0: --[[-- jbe/bsw@0: text = -- text with the value formatted as a date, according to the locale settings jbe/bsw@0: format.date( jbe/bsw@0: value, -- a date, a timestamp or nil jbe/bsw@0: { jbe/bsw@0: nil_as = nil_text -- text to be returned for a nil value jbe/bsw@0: } jbe/bsw@0: ) jbe/bsw@0: jbe/bsw@0: Formats a date or timestamp as a date, according to the locale settings. jbe/bsw@0: jbe/bsw@0: --]]-- jbe/bsw@0: jbe/bsw@0: function format.date(value, options) jbe/bsw@0: local options = options or {} jbe/bsw@0: if value == nil then jbe/bsw@0: return options.nil_as or "" jbe/bsw@0: end jbe/bsw@0: if not ( jbe/bsw@0: atom.has_type(value, atom.date) or jbe/bsw@0: atom.has_type(value, atom.timestamp) jbe/bsw@0: ) then jbe/bsw@0: error("Value passed to format.date(...) is neither a date, a timestamp, nor nil.") jbe/bsw@0: end jbe/bsw@0: if value.invalid then jbe/bsw@0: return "invalid" jbe/bsw@0: end jbe/bsw@0: local result = locale.get("date_format") or "YYYY-MM-DD" jbe/bsw@0: result = string.gsub(result, "YYYY", function() jbe/bsw@0: return format.decimal(value.year, { digits = 4 }) jbe/bsw@0: end) jbe/bsw@0: result = string.gsub(result, "YY", function() jbe/bsw@0: return format.decimal(value.year % 100, { digits = 2 }) jbe/bsw@0: end) jbe/bsw@0: result = string.gsub(result, "Y", function() jbe/bsw@0: return format.decimal(value.year) jbe/bsw@0: end) jbe/bsw@0: result = string.gsub(result, "MM", function() jbe/bsw@0: return format.decimal(value.month, { digits = 2 }) jbe/bsw@0: end) jbe/bsw@0: result = string.gsub(result, "M", function() jbe/bsw@0: return format.decimal(value.month) jbe/bsw@0: end) jbe/bsw@0: result = string.gsub(result, "DD", function() jbe/bsw@0: return format.decimal(value.day, { digits = 2 }) jbe/bsw@0: end) jbe/bsw@0: result = string.gsub(result, "D", function() jbe/bsw@0: return format.decimal(value.day) jbe/bsw@0: end) jbe/bsw@0: return result jbe/bsw@0: end