Commits

Anonymous committed 88c81c3

parser/misc/bplist: miscellaneous fixes

Comments (0)

Files changed (1)

hachoir-parser/hachoir_parser/misc/bplist.py

         elif markertype == 3:
             # Date
             yield Bits(self, "extra", 4, "Extra value, should be 3")
-            cvt_time=lambda v:datetime(2001,1,1) + timedelta(seconds=v)
+            # Use a heuristic to determine which epoch to use
+            def cvt_time(v):
+                v=timedelta(seconds=v)
+                epoch2001 = datetime(2001,1,1)
+                epoch1970 = datetime(1970,1,1)
+                if (epoch2001 + v - datetime.today()).days > 5*365:
+                    return epoch1970 + v
+                return epoch2001 + v
             yield displayHandler(Float64(self, "value"),lambda x:humanDatetime(cvt_time(x)))
-            self.xml=lambda prefix:prefix + "<date>%s</date>"%(cvt_time(self['value'].value).isoformat())
+            self.xml=lambda prefix:prefix + "<date>%sZ</date>"%(cvt_time(self['value'].value).isoformat())
 
         elif markertype == 4:
             # Data
             yield BPListSize(self, "size")
             if self['size'].value:
                 yield String(self, "value", self['size'].value, charset="ASCII")
-                self.xml=lambda prefix:prefix + "<string>%s</string>"%(self['value'].value.encode('iso-8859-1'))
+                self.xml=lambda prefix:prefix + "<string>%s</string>"%(self['value'].value.replace('&','&amp;').encode('iso-8859-1'))
             else:
                 self.xml=lambda prefix:prefix + '<string></string>'
 
             yield BPListSize(self, "size")
             if self['size'].value:
                 yield String(self, "value", self['size'].value*2, charset="UTF-16-BE")
-                self.xml=lambda prefix:prefix + "<string>%s</string>"%(self['value'].value.encode('utf-8'))
+                self.xml=lambda prefix:prefix + "<string>%s</string>"%(self['value'].value.replace('&','&amp;').encode('utf-8'))
             else:
                 self.xml=lambda prefix:prefix + '<string></string>'