Skip to content

Commit

Permalink
[FIX] calendar_import_ics: convert other tz to UTC
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnauCForgeFlow committed Mar 7, 2024
1 parent b7c3176 commit a9ad5e5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions calendar_import_ics/tests/sample_files/test_calendar.ics
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ TRANSP:OPAQUE
UID:ed27f2b89f945c7692547a2903c20cbe80de6cc6
END:VEVENT
BEGIN:VEVENT
DTEND:20061011T190000Z
DTEND;TZID=America/Denver:20061011T190000
DTSTAMP:20240221T094129Z
DTSTART:20061011T160000Z
DTSTART;TZID=America/Denver:20061011T160000
SEQUENCE:0
SUMMARY:Unavailable
TRANSP:OPAQUE
Expand Down
18 changes: 18 additions & 0 deletions calendar_import_ics/wizards/wizard_import_ics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import base64
from datetime import datetime

import pytz
from dateutil import parser

from odoo import _, fields, models
from odoo.exceptions import ValidationError

Expand Down Expand Up @@ -42,6 +45,8 @@ def button_import(self):
lines = file_str.split("\n")
ics_event = {}
for line in lines:
if line.startswith(("DTSTART", "DTEND")) and "TZID=" in line:
line = self.convert_date_to_z(line)
if line.startswith("BEGIN:VEVENT"):
ics_event = {}
elif line.startswith("END:VEVENT"):
Expand Down Expand Up @@ -116,3 +121,16 @@ def _delete_non_imported_events(self, imported_events):
non_imported_event.write({"partner_ids": [(3, self.partner_id.id)]})
if not non_imported_events.partner_ids:
non_imported_events.unlink()

def convert_date_to_z(self, line):
split_parts = line.split(":")
event_phase = split_parts[0].split(";TZID=")[0]
tz_id = split_parts[0].split(";TZID=")[1]
date = split_parts[1]

date_obj = parser.parse(date)
tz = pytz.timezone(tz_id)

utc_date = tz.localize(date_obj).astimezone(pytz.UTC)
utc_date_string = utc_date.strftime(event_phase + ":%Y%m%dT%H%M%SZ")
return utc_date_string

0 comments on commit a9ad5e5

Please sign in to comment.