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":
Representations can be done in one of two formats – a basic format with a minimal number of separators or an extended format with separators added to enhance human readability.
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:
[NEW] ISO 8601 basic format for dates and times is now supported (e.g. YYYYMMDDTHHmmssZ).
The latest version of ics
is 0.7 (that you are likely using), as of this writing.
It pins arrow
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 to arrow
:
e.begin = task['scheduled'], 'YYYYMMDDTHHmmssZ'
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 on arrow
. If newer ics
doesn't handle it, at least you're free to use the latest arrow
package.pendulum
, dateutil
, etc.). The whole datetime handling situation can be messy in Python but they all tend to interoperate with stdlib datetime
.