webmcp

diff framework/env/format/date.lua @ 0:9fdfb27f8e67

Version 1.0.0
author jbe/bsw
date Sun Oct 25 12:00:00 2009 +0100 (2009-10-25)
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/framework/env/format/date.lua	Sun Oct 25 12:00:00 2009 +0100
     1.3 @@ -0,0 +1,51 @@
     1.4 +--[[--
     1.5 +text =                 -- text with the value formatted as a date, according to the locale settings
     1.6 +format.date(
     1.7 +  value,               -- a date, a timestamp or nil
     1.8 +  {
     1.9 +    nil_as = nil_text  -- text to be returned for a nil value
    1.10 +  }
    1.11 +)
    1.12 +
    1.13 +Formats a date or timestamp as a date, according to the locale settings.
    1.14 +
    1.15 +--]]--
    1.16 +
    1.17 +function format.date(value, options)
    1.18 +  local options = options or {}
    1.19 +  if value == nil then
    1.20 +    return options.nil_as or ""
    1.21 +  end
    1.22 +  if not (
    1.23 +    atom.has_type(value, atom.date) or
    1.24 +    atom.has_type(value, atom.timestamp)
    1.25 +  ) then
    1.26 +    error("Value passed to format.date(...) is neither a date, a timestamp, nor nil.")
    1.27 +  end
    1.28 +  if value.invalid then
    1.29 +    return "invalid"
    1.30 +  end
    1.31 +  local result = locale.get("date_format") or "YYYY-MM-DD"
    1.32 +  result = string.gsub(result, "YYYY", function()
    1.33 +    return format.decimal(value.year, { digits = 4 })
    1.34 +  end)
    1.35 +  result = string.gsub(result, "YY", function()
    1.36 +    return format.decimal(value.year % 100, { digits = 2 })
    1.37 +  end)
    1.38 +  result = string.gsub(result, "Y", function()
    1.39 +    return format.decimal(value.year)
    1.40 +  end)
    1.41 +  result = string.gsub(result, "MM", function()
    1.42 +    return format.decimal(value.month, { digits = 2 })
    1.43 +  end)
    1.44 +  result = string.gsub(result, "M", function()
    1.45 +    return format.decimal(value.month)
    1.46 +  end)
    1.47 +  result = string.gsub(result, "DD", function()
    1.48 +    return format.decimal(value.day, { digits = 2 })
    1.49 +  end)
    1.50 +  result = string.gsub(result, "D", function()
    1.51 +    return format.decimal(value.day)
    1.52 +  end)
    1.53 +  return result
    1.54 +end

Impressum / About Us