1""" 2Helper functions that convert strftime formats into more readable representations. 3""" 4from rest_framework import ISO_8601 5 6 7def datetime_formats(formats): 8 format = ', '.join(formats).replace( 9 ISO_8601, 10 'YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]' 11 ) 12 return humanize_strptime(format) 13 14 15def date_formats(formats): 16 format = ', '.join(formats).replace(ISO_8601, 'YYYY-MM-DD') 17 return humanize_strptime(format) 18 19 20def time_formats(formats): 21 format = ', '.join(formats).replace(ISO_8601, 'hh:mm[:ss[.uuuuuu]]') 22 return humanize_strptime(format) 23 24 25def humanize_strptime(format_string): 26 # Note that we're missing some of the locale specific mappings that 27 # don't really make sense. 28 mapping = { 29 "%Y": "YYYY", 30 "%y": "YY", 31 "%m": "MM", 32 "%b": "[Jan-Dec]", 33 "%B": "[January-December]", 34 "%d": "DD", 35 "%H": "hh", 36 "%I": "hh", # Requires '%p' to differentiate from '%H'. 37 "%M": "mm", 38 "%S": "ss", 39 "%f": "uuuuuu", 40 "%a": "[Mon-Sun]", 41 "%A": "[Monday-Sunday]", 42 "%p": "[AM|PM]", 43 "%z": "[+HHMM|-HHMM]" 44 } 45 for key, val in mapping.items(): 46 format_string = format_string.replace(key, val) 47 return format_string 48