clalancette / pycdlib

Python library to read and write ISOs
GNU Lesser General Public License v2.1
143 stars 38 forks source link

Parse VolumeDateDescriptor as datetime object #97

Open rlaphoenix opened 2 years ago

rlaphoenix commented 2 years ago

Is it possible at all that you could rework the VolumeDateDescriptor class as a class inheriting a datetime object, with the initialization overridden to parse ISO9660 dates?

def date_convert(vdd: VolumeDescriptorDate) -> Optional[datetime]:
    if not vdd.year:
        return None
    return datetime(
        year=vdd.year, month=vdd.month, day=vdd.dayofmonth,
        hour=vdd.hour, minute=vdd.minute, second=vdd.second,
        microsecond=vdd.hundredthsofsecond,  # TODO: Is this really microseconds?
        # offset the timezone, since ISO's dates are offsets of GMT in 15 minute intervals, we
        # need to calculate that but in seconds to pass to `tzoffset`.
        tzinfo=tzoffset("GMT", (15 * vdd.gmtoffset) * 60)
    )

Right now I'm having to do the above to deal with converting every specific VDD to make them useful in scenarios where you want to read it in context for printing or working with timedelta's or other modifications.

The only thing I'm unsure about in terms of the conversion code I made above, is the hundrethsofsecond. I believe it's a centisecond, therefore, I believe I need to multiply the value by 10,000 to get it as a microsecond but I'm unsure.

The reason I ask for this change is I use the PVD by printing all of its data under a repr(). This results in FileOrTextIdentifier and VolumeDescriptorDate printing in an effectively unusable manner. Even if I str() it's not much different if at all. My solution is to just v = v.text on FileOrTextIdentifiers and v = date_convert(v) on VolumeDescriptorDates, and then print. Meaning I need to iterate throughout the data and compare by isintance or by key. Not ideal.

P.S., Notice I had to do a not vdd.year, this is because there's no __bool__ override either, which would help in situations like this.