diff options
| author | Tom Christie | 2014-09-09 17:46:28 +0100 |
|---|---|---|
| committer | Tom Christie | 2014-09-09 17:46:28 +0100 |
| commit | b1c07670ca65084c5fef2bbb63d1f4163763014b (patch) | |
| tree | 4f08654d698990d97fe275d8dbbbcc1164524086 /rest_framework/utils/humanize_datetime.py | |
| parent | 21980b800d04a1d82a6003823abfdf4ab80ae979 (diff) | |
| download | django-rest-framework-b1c07670ca65084c5fef2bbb63d1f4163763014b.tar.bz2 | |
Fleshing out serializer fields
Diffstat (limited to 'rest_framework/utils/humanize_datetime.py')
| -rw-r--r-- | rest_framework/utils/humanize_datetime.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/rest_framework/utils/humanize_datetime.py b/rest_framework/utils/humanize_datetime.py new file mode 100644 index 00000000..649f2abc --- /dev/null +++ b/rest_framework/utils/humanize_datetime.py @@ -0,0 +1,47 @@ +""" +Helper functions that convert strftime formats into more readable representations. +""" +from rest_framework import ISO_8601 + + +def datetime_formats(formats): + format = ', '.join(formats).replace( + ISO_8601, + 'YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]' + ) + return humanize_strptime(format) + + +def date_formats(formats): + format = ', '.join(formats).replace(ISO_8601, 'YYYY[-MM[-DD]]') + return humanize_strptime(format) + + +def time_formats(formats): + format = ', '.join(formats).replace(ISO_8601, 'hh:mm[:ss[.uuuuuu]]') + return humanize_strptime(format) + + +def humanize_strptime(format_string): + # Note that we're missing some of the locale specific mappings that + # don't really make sense. + mapping = { + "%Y": "YYYY", + "%y": "YY", + "%m": "MM", + "%b": "[Jan-Dec]", + "%B": "[January-December]", + "%d": "DD", + "%H": "hh", + "%I": "hh", # Requires '%p' to differentiate from '%H'. + "%M": "mm", + "%S": "ss", + "%f": "uuuuuu", + "%a": "[Mon-Sun]", + "%A": "[Monday-Sunday]", + "%p": "[AM|PM]", + "%z": "[+HHMM|-HHMM]" + } + for key, val in mapping.items(): + format_string = format_string.replace(key, val) + return format_string |
