[formatter] implement 'D' format specifier
To be able to parse any string into a 'datetime' object
and format it as necessary.
Example:
{created_at:D%Y-%m-%dT%H:%M:%S%z}
->
"2010-01-01 00:00:00"
{created_at:D%Y-%m-%dT%H:%M:%S%z/%b %d %Y %I:%M %p}
->
"Jan 01 2010 12:00 AM"
with 'created_at' == "2010-01-01T01:00:00+0100"
This commit is contained in:
@@ -274,6 +274,8 @@ def build_format_func(format_spec):
|
||||
return _parse_join(format_spec)
|
||||
if fmt == "R":
|
||||
return _parse_replace(format_spec)
|
||||
if fmt == "D":
|
||||
return _parse_datetime(format_spec)
|
||||
return _default_format(format_spec)
|
||||
return format
|
||||
|
||||
@@ -319,6 +321,16 @@ def _parse_replace(format_spec):
|
||||
return replace
|
||||
|
||||
|
||||
def _parse_datetime(format_spec):
|
||||
dt_format, _, format_spec = format_spec.partition("/")
|
||||
dt_format = dt_format[1:]
|
||||
fmt = build_format_func(format_spec)
|
||||
|
||||
def dt(obj):
|
||||
return fmt(text.parse_datetime(obj, dt_format))
|
||||
return dt
|
||||
|
||||
|
||||
def _default_format(format_spec):
|
||||
def wrap(obj):
|
||||
return format(obj, format_spec)
|
||||
|
||||
Reference in New Issue
Block a user