Changeset 225 for pyyaml/trunk/lib/yaml
- Timestamp:
- 08/16/06 14:22:38 (6 years ago)
- Location:
- pyyaml/trunk/lib/yaml
- Files:
-
- 2 modified
-
constructor.py (modified) (4 diffs)
-
representer.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
pyyaml/trunk/lib/yaml/constructor.py
r222 r225 6 6 from nodes import * 7 7 8 try: 9 import datetime 10 datetime_available = True 11 except ImportError: 12 datetime_available = False 8 import datetime 13 9 14 10 try: … … 305 301 :(?P<minute>[0-9][0-9]) 306 302 :(?P<second>[0-9][0-9]) 307 (?: \.(?P<fraction>[0-9]*))?308 (?:[ \t]*(? :Z|(?P<tz_hour>[-+][0-9][0-9]?)309 (?::(?P<tz_minute>[0-9][0-9]) ?)?))?)?$''', re.X)303 (?:(?P<fraction>\.[0-9]*))? 304 (?:[ \t]*(?P<tz>Z|(?P<tz_sign>[-+])(?P<tz_hour>[0-9][0-9]?) 305 (?::(?P<tz_minute>[0-9][0-9]))?))?)?$''', re.X) 310 306 311 307 def construct_yaml_timestamp(self, node): … … 313 309 match = self.timestamp_regexp.match(node.value) 314 310 values = match.groupdict() 315 for key in values: 316 if values[key]: 317 values[key] = int(values[key]) 318 else: 319 values[key] = 0 320 fraction = values['fraction'] 321 if fraction: 322 while 10*fraction < 1000000: 323 fraction *= 10 324 values['fraction'] = fraction 325 stamp = datetime.datetime(values['year'], values['month'], values['day'], 326 values['hour'], values['minute'], values['second'], values['fraction']) 327 diff = datetime.timedelta(hours=values['tz_hour'], minutes=values['tz_minute']) 328 return stamp-diff 311 year = int(values['year']) 312 month = int(values['month']) 313 day = int(values['day']) 314 if not values['hour']: 315 return datetime.date(year, month, day) 316 hour = int(values['hour']) 317 minute = int(values['minute']) 318 second = int(values['second']) 319 fraction = 0 320 if values['fraction']: 321 fraction = int(float(values['fraction'])*1000000) 322 delta = None 323 if values['tz_sign']: 324 tz_hour = int(values['tz_hour']) 325 tz_minute = int(values['tz_minute'] or 0) 326 delta = datetime.timedelta(hours=tz_hour, minutes=tz_minute) 327 if values['tz_sign'] == '-': 328 delta = -delta 329 data = datetime.datetime(year, month, day, hour, minute, second, fraction) 330 if delta: 331 data -= delta 332 return data 329 333 330 334 def construct_yaml_omap(self, node): … … 430 434 SafeConstructor.construct_yaml_binary) 431 435 432 if datetime_available: 433 SafeConstructor.add_constructor( 434 u'tag:yaml.org,2002:timestamp', 435 SafeConstructor.construct_yaml_timestamp) 436 SafeConstructor.add_constructor( 437 u'tag:yaml.org,2002:timestamp', 438 SafeConstructor.construct_yaml_timestamp) 436 439 437 440 SafeConstructor.add_constructor( -
pyyaml/trunk/lib/yaml/representer.py
r222 r225 6 6 from nodes import * 7 7 8 try: 9 import datetime 10 datetime_available = True 11 except ImportError: 12 datetime_available = False 8 import datetime 13 9 14 10 try: … … 242 238 243 239 def represent_date(self, data): 244 value = u '%04d-%02d-%02d' % (data.year, data.month, data.day)240 value = unicode(data.isoformat()) 245 241 return self.represent_scalar(u'tag:yaml.org,2002:timestamp', value) 246 242 247 243 def represent_datetime(self, data): 248 value = u'%04d-%02d-%02d %02d:%02d:%02d' \ 249 % (data.year, data.month, data.day, 250 data.hour, data.minute, data.second) 251 if data.microsecond: 252 value += u'.' + unicode(data.microsecond/1000000.0).split(u'.')[1] 253 if data.utcoffset(): 254 value += unicode(data.utcoffset()) 244 value = unicode(data.isoformat(' ')) 255 245 return self.represent_scalar(u'tag:yaml.org,2002:timestamp', value) 256 246 … … 298 288 SafeRepresenter.represent_set) 299 289 300 if datetime_available: 301 SafeRepresenter.add_representer(datetime.date, 302 SafeRepresenter.represent_date) 303 SafeRepresenter.add_representer(datetime.datetime, 304 SafeRepresenter.represent_datetime) 290 SafeRepresenter.add_representer(datetime.date, 291 SafeRepresenter.represent_date) 292 SafeRepresenter.add_representer(datetime.datetime, 293 SafeRepresenter.represent_datetime) 305 294 306 295 SafeRepresenter.add_representer(None,
