mirror of
https://github.com/b4tman/sync_ics2gcal
synced 2025-01-21 07:28:24 +00:00
rewrite date/dateTime conversion func's
This commit is contained in:
parent
8ea8512c11
commit
c34b4cdb21
@ -1,8 +1,47 @@
|
|||||||
from icalendar import Calendar, Event
|
import datetime
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from icalendar import Calendar, Event
|
||||||
from pytz import utc
|
from pytz import utc
|
||||||
|
|
||||||
import datetime
|
|
||||||
|
def format_datetime_utc(value):
|
||||||
|
"""utc datetime as string from date or datetime value
|
||||||
|
Arguments:
|
||||||
|
value -- date or datetime value
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
utc datetime value as string in iso format
|
||||||
|
"""
|
||||||
|
if not isinstance(value, datetime.datetime):
|
||||||
|
value = datetime.datetime(
|
||||||
|
value.year, value.month, value.day, tzinfo=utc)
|
||||||
|
value = value.replace(microsecond=1)
|
||||||
|
return utc.normalize(value.astimezone(utc)).replace(tzinfo=None).isoformat() + 'Z'
|
||||||
|
|
||||||
|
|
||||||
|
def gcal_date_or_dateTime(value, check_value=None):
|
||||||
|
"""date or dateTime to gcal (start or end dict)
|
||||||
|
Arguments:
|
||||||
|
value -- date or datetime value
|
||||||
|
check_value - date or datetime to choise result type (if not None)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
dict { 'date': ... } or { 'dateTime': ... }
|
||||||
|
"""
|
||||||
|
|
||||||
|
if check_value is None:
|
||||||
|
check_value = value
|
||||||
|
|
||||||
|
result = {}
|
||||||
|
if isinstance(check_value, datetime.datetime):
|
||||||
|
result['dateTime'] = format_datetime_utc(value)
|
||||||
|
else:
|
||||||
|
if isinstance(check_value, datetime.date):
|
||||||
|
if isinstance(value, datetime.datetime):
|
||||||
|
value = datetime.date(value.year, value.month, value.day)
|
||||||
|
result['date'] = value.isoformat()
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
class EventConverter(Event):
|
class EventConverter(Event):
|
||||||
@ -32,12 +71,7 @@ class EventConverter(Event):
|
|||||||
utc datetime value as string in iso format
|
utc datetime value as string in iso format
|
||||||
"""
|
"""
|
||||||
|
|
||||||
date = self.decoded(prop)
|
return format_datetime_utc(self.decoded(prop))
|
||||||
if not isinstance(date, datetime.datetime):
|
|
||||||
date = datetime.datetime(
|
|
||||||
date.year, date.month, date.day, tzinfo=utc)
|
|
||||||
date = date.replace(microsecond=1)
|
|
||||||
return utc.normalize(date.astimezone(utc)).replace(tzinfo=None).isoformat() + 'Z'
|
|
||||||
|
|
||||||
def _gcal_start(self):
|
def _gcal_start(self):
|
||||||
""" event start dict from icalendar event
|
""" event start dict from icalendar event
|
||||||
@ -49,58 +83,31 @@ class EventConverter(Event):
|
|||||||
dict
|
dict
|
||||||
"""
|
"""
|
||||||
|
|
||||||
start_date = self.decoded('DTSTART')
|
value = self.decoded('DTSTART')
|
||||||
if isinstance(start_date, datetime.datetime):
|
return gcal_date_or_dateTime(value)
|
||||||
return {
|
|
||||||
'dateTime': self._datetime_str_prop('DTSTART')
|
|
||||||
}
|
|
||||||
else:
|
|
||||||
if isinstance(start_date, datetime.date):
|
|
||||||
return {
|
|
||||||
'date': start_date.isoformat()
|
|
||||||
}
|
|
||||||
raise ValueError('DTSTART must be date or datetime')
|
|
||||||
|
|
||||||
def _gcal_end(self):
|
def _gcal_end(self):
|
||||||
"""event end dict from icalendar event
|
"""event end dict from icalendar event
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
ValueError -- if DTEND not date or datetime
|
|
||||||
ValueError -- if no DTEND or DURATION
|
ValueError -- if no DTEND or DURATION
|
||||||
ValueError -- if end date/datetime not found
|
|
||||||
Returns:
|
Returns:
|
||||||
dict
|
dict
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
result = None
|
||||||
if 'DTEND' in self:
|
if 'DTEND' in self:
|
||||||
end_date = self.decoded('DTEND')
|
value = self.decoded('DTEND')
|
||||||
if isinstance(end_date, datetime.datetime):
|
result = gcal_date_or_dateTime(value)
|
||||||
return {
|
elif 'DURATION' in self:
|
||||||
'dateTime': self._datetime_str_prop('DTEND')
|
start_val = self.decoded('DTSTART')
|
||||||
}
|
|
||||||
else:
|
|
||||||
if isinstance(end_date, datetime.date):
|
|
||||||
return {
|
|
||||||
'date': end_date.isoformat()
|
|
||||||
}
|
|
||||||
raise ValueError('DTEND must be date or datetime')
|
|
||||||
else:
|
|
||||||
if 'DURATION' in self:
|
|
||||||
start_date = self.decoded('DTSTART')
|
|
||||||
duration = self.decoded('DURATION')
|
duration = self.decoded('DURATION')
|
||||||
end_date = start_date + duration
|
end_val = start_val + duration
|
||||||
|
|
||||||
if isinstance(start_date, datetime.datetime):
|
result = gcal_date_or_dateTime(end_val, check_value=start_val)
|
||||||
return {
|
|
||||||
'dateTime': utc.normalize(end_date.astimezone(utc)).replace(tzinfo=None, microsecond=1).isoformat() + 'Z'
|
|
||||||
}
|
|
||||||
else:
|
else:
|
||||||
if isinstance(start_date, datetime.date):
|
|
||||||
return {
|
|
||||||
'date': datetime.date(end_date.year, end_date.month, end_date.day).isoformat()
|
|
||||||
}
|
|
||||||
raise ValueError('no DTEND or DURATION')
|
raise ValueError('no DTEND or DURATION')
|
||||||
raise ValueError('end date/time not found')
|
return result
|
||||||
|
|
||||||
def _put_to_gcal(self, gcal_event, prop, func, ics_prop=None):
|
def _put_to_gcal(self, gcal_event, prop, func, ics_prop=None):
|
||||||
"""get property from ical event if exist, and put to gcal event
|
"""get property from ical event if exist, and put to gcal event
|
||||||
|
Loading…
x
Reference in New Issue
Block a user