Changeset 225 for pyyaml/trunk/lib/yaml

Show
Ignore:
Timestamp:
08/16/06 14:22:38 (6 years ago)
Author:
xi
Message:

Fix timestamp constructing and representing (close #25).

Location:
pyyaml/trunk/lib/yaml
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • pyyaml/trunk/lib/yaml/constructor.py

    r222 r225  
    66from nodes import * 
    77 
    8 try: 
    9     import datetime 
    10     datetime_available = True 
    11 except ImportError: 
    12     datetime_available = False 
     8import datetime 
    139 
    1410try: 
     
    305301                :(?P<minute>[0-9][0-9]) 
    306302                :(?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) 
    310306 
    311307    def construct_yaml_timestamp(self, node): 
     
    313309        match = self.timestamp_regexp.match(node.value) 
    314310        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 
    329333 
    330334    def construct_yaml_omap(self, node): 
     
    430434        SafeConstructor.construct_yaml_binary) 
    431435 
    432 if datetime_available: 
    433     SafeConstructor.add_constructor( 
    434             u'tag:yaml.org,2002:timestamp', 
    435             SafeConstructor.construct_yaml_timestamp) 
     436SafeConstructor.add_constructor( 
     437        u'tag:yaml.org,2002:timestamp', 
     438        SafeConstructor.construct_yaml_timestamp) 
    436439 
    437440SafeConstructor.add_constructor( 
  • pyyaml/trunk/lib/yaml/representer.py

    r222 r225  
    66from nodes import * 
    77 
    8 try: 
    9     import datetime 
    10     datetime_available = True 
    11 except ImportError: 
    12     datetime_available = False 
     8import datetime 
    139 
    1410try: 
     
    242238 
    243239    def represent_date(self, data): 
    244         value = u'%04d-%02d-%02d' % (data.year, data.month, data.day) 
     240        value = unicode(data.isoformat()) 
    245241        return self.represent_scalar(u'tag:yaml.org,2002:timestamp', value) 
    246242 
    247243    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(' ')) 
    255245        return self.represent_scalar(u'tag:yaml.org,2002:timestamp', value) 
    256246 
     
    298288        SafeRepresenter.represent_set) 
    299289 
    300 if datetime_available: 
    301     SafeRepresenter.add_representer(datetime.date, 
    302             SafeRepresenter.represent_date) 
    303     SafeRepresenter.add_representer(datetime.datetime, 
    304             SafeRepresenter.represent_datetime) 
     290SafeRepresenter.add_representer(datetime.date, 
     291        SafeRepresenter.represent_date) 
     292SafeRepresenter.add_representer(datetime.datetime, 
     293        SafeRepresenter.represent_datetime) 
    305294 
    306295SafeRepresenter.add_representer(None,