Ticket #7921: t7921-iso-8601-fixes-r7795.diff
| File t7921-iso-8601-fixes-r7795.diff, 2.2 KB (added by cboos, 3 years ago) |
|---|
-
trac/util/datefmt.py
102 102 103 103 `tzinfo` will default to the local timezone if left to `None`. 104 104 """ 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 108 123 text = t.strftime(format) 124 if normalize_Z: 125 text = text.replace('+0000', 'Z') 109 126 encoding = locale.getpreferredencoding() or sys.getdefaultencoding() 110 127 if sys.platform != 'win32' or sys.version_info[:2] > (2, 3): 111 128 encoding = locale.getlocale(locale.LC_TIME)[1] or encoding … … 117 134 See `format_datetime` for more details. 118 135 """ 119 136 if format == 'iso8601': 120 format = ' %Y-%m-%d'137 format = 'iso8601date' 121 138 return format_datetime(t, format, tzinfo=tzinfo) 122 139 123 140 def format_time(t=None, format='%X', tzinfo=None): … … 125 142 See `format_datetime` for more details. 126 143 """ 127 144 if format == 'iso8601': 128 format = ' %H:%M:%SZ%z'145 format = 'iso8601time' 129 146 return format_datetime(t, format, tzinfo=tzinfo) 130 147 131 148 def get_date_format_hint(): … … 167 184 168 185 _ISO_8601_RE = re.compile(r'(\d\d\d\d)(?:-?(\d\d)(?:-?(\d\d))?)?' # date 169 186 r'(?:T(\d\d)(?::?(\d\d)(?::?(\d\d))?)?)?' # time 170 r'(Z (?:([-+])?(\d\d):?(\d\d)?)?)?$'# timezone187 r'(Z?(?:([-+])?(\d\d):?(\d\d)?)?)?$' # timezone 171 188 ) 172 189 173 190 def parse_date(text, tzinfo=None):
