I'm using taskwarrior as a task manager and want to convert task into event for my calender in a .ics
fformat (ICal) using python (ics
package).
if I run the following code:
from ics import Calendar, Event
import json
task1 = {'description': 'blabla', 'scheduled': '20210730T220000Z' }
task2 = {'description': 'blabla', 'scheduled': '2021-07-30T00:00' }
task = task1
if __name__ == "__main__":
c = Calendar()
print(task)
e = Event()
e.name = task['description']
e.begin = task['scheduled']
c.events.add(e)
it throws me an error:
arrow.parser.ParserError: Could not match input to any of ['YYYY-MM-DDTHH:mm'] on '20210730T220000Z'
There is no problem if I replace task = task1
by task = task2
. I suspect that the format of the JSON with timezone is not supported by ics
. Is there an easy way / package to convert 20210730T220000Z
to the format 2021-07-30T00:00
?
Edit N°1 After the comment of @mkrieger1 and the link I tried the following without success:
import datetime
task1 = {'description': 'blabla', 'scheduled': '20210730T220000Z' }
task2 = {'description': 'blabla', 'scheduled': '2021-07-30T00:00' }
print(task1["scheduled"])
dt = datetime.datetime.strptime(task1["scheduled"], 'YYYY-MM-DDTHH:mm').strftime('YYYY-MM-DDTHH:mm')
print(dt)
Edit N°2: this finally worked:
import datetime
task1 = {'description': 'blabla', 'scheduled': '20210730T220000Z' }
task2 = {'description': 'blabla', 'scheduled': '2021-07-30T00:00' }
print(task1["scheduled"])
dt = datetime.datetime.strptime(task1["scheduled"], "%Y%m%dT%H%M%S%fZ")
print(dt)
The time format
20210730T220000Z
is known as "ISO 8601 basic format":There are different formats such as RFC3339 and ISO8601, this post discusses their relationship in detail. But this error you're seeing is not due to the presence of a timezone (
Z
for UTC).Arrow has recently added support in v0.15:
The latest version of
ics
is 0.7 (that you are likely using), as of this writing. It pinsarrow
to <0.15, which directly explains and reproduces the error you're seeing.To move forward, you have a few options:
arrow
,dateutil
,datetime
, as already suggested by @mrkrieger1.ics
gives us a convenient escape hatch toarrow
: This corresponds to the two string parameter form, e.g.arrow.get('2013-05-05 12:30:45', 'YYYY-MM-DD HH:mm:ss')
ics
, which has new developments and no longer depends onarrow
. If newerics
doesn't handle it, at least you're free to use the latestarrow
package.pendulum
,dateutil
, etc.). The whole datetime handling situation can be messy in Python but they all tend to interoperate with stdlibdatetime
.