skarim / vobject

A full-featured Python package for parsing and creating iCalendar and vCard files
https://vobject.sameenkarim.com
258 stars 93 forks source link

Wrong updating date[time] attributes #180

Open tieugene opened 2 years ago

tieugene commented 2 years ago

I met this when tried to update 'Due' and 'DTStart' fields of VTODOs from date to datetime and vice versa. Behaviour is very strange (try to update date<>datetime, naive datetime <> timezoned datetime etc).

#!/usr/bin/env python3
# Test Date[time] fields of vobject
import datetime, vobject

def just_due(vcal: str):
    for s in vcal.split():
        if s.startswith('DUE'):
            return s

# Stage 1: create vtodo with due:date
vobj = vobject.iCalendar()
vobj.add('vtodo')
vobj.vtodo.add("due").value = datetime.datetime.now().date()
s = vobj.serialize()
print(type(vobj.vtodo.due.value), just_due(s))
# Stage 2: change due: date>datetime
vobj = vobject.readOne(s)
now = datetime.datetime.now()
vobj.vtodo.due.value = now
s = vobj.serialize()
print(type(now), "=>", type(vobj.vtodo.due.value), just_due(s))
# Stage 3: read result
vobj = vobject.readOne(s)
print(type(vobj.vtodo.due.value))

Result:

<class 'datetime.date'> DUE;VALUE=DATE:20211217
<class 'datetime.datetime'> => <class 'datetime.date'> DUE;VALUE=DATE:20211217T190416
<class 'datetime.date'>