Edgewall Software

Ticket #7921: t7921-iso-8601-fixes-r7795.diff

File t7921-iso-8601-fixes-r7795.diff, 2.2 KB (added by cboos, 3 years ago)

Fix formatting of dates when using the iso:8601 format.

  • trac/util/datefmt.py

     
    102102 
    103103    `tzinfo` will default to the local timezone if left to `None`. 
    104104    """ 
    105     t = to_datetime(t, tzinfo).astimezone(tzinfo or localtz) 
    106     if format.lower() == 'iso8601': 
    107         format = '%Y-%m-%dT%H:%M:%SZ%z' 
     105    tz = tzinfo or localtz 
     106    t = to_datetime(t, tzinfo).astimezone(tz) 
     107    normalize_Z = False 
     108    if format.lower().startswith('iso8601'): 
     109        date_only = time_only = False 
     110        if 'date' in format: 
     111            date_only = True 
     112        elif 'time' in format: 
     113            time_only = True 
     114        if date_only: 
     115            format = '%Y-%m-%d' 
     116        elif time_only: 
     117            format = '%H:%M:%S' 
     118        else: 
     119            format = '%Y-%m-%dT%H:%M:%S' 
     120        if not date_only: 
     121            format += '%z' 
     122            normalize_Z = True 
    108123    text = t.strftime(format) 
     124    if normalize_Z: 
     125        text = text.replace('+0000', 'Z') 
    109126    encoding = locale.getpreferredencoding() or sys.getdefaultencoding() 
    110127    if sys.platform != 'win32' or sys.version_info[:2] > (2, 3): 
    111128        encoding = locale.getlocale(locale.LC_TIME)[1] or encoding 
     
    117134    See `format_datetime` for more details. 
    118135    """ 
    119136    if format == 'iso8601': 
    120         format = '%Y-%m-%d' 
     137        format = 'iso8601date' 
    121138    return format_datetime(t, format, tzinfo=tzinfo) 
    122139 
    123140def format_time(t=None, format='%X', tzinfo=None): 
     
    125142    See `format_datetime` for more details. 
    126143    """ 
    127144    if format == 'iso8601': 
    128         format = '%H:%M:%SZ%z' 
     145        format = 'iso8601time' 
    129146    return format_datetime(t, format, tzinfo=tzinfo) 
    130147 
    131148def get_date_format_hint(): 
     
    167184 
    168185_ISO_8601_RE = re.compile(r'(\d\d\d\d)(?:-?(\d\d)(?:-?(\d\d))?)?'   # date 
    169186                          r'(?:T(\d\d)(?::?(\d\d)(?::?(\d\d))?)?)?' # time 
    170                           r'(Z(?:([-+])?(\d\d):?(\d\d)?)?)?$'       # timezone 
     187                          r'(Z?(?:([-+])?(\d\d):?(\d\d)?)?)?$'      # timezone 
    171188                          ) 
    172189 
    173190def parse_date(text, tzinfo=None):