Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time zone support in schedule_from_ical #526

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

epologee
Copy link

The base implementation of IceCube::IcalParser.schedule_from_ical ignores time zone identifiers, resulting in all times being parsed in the current time zone of the host.

This PR exposes the issue with a series of specs, and fixes it with a (somewhat lengthy) time zone lookup. This last part was added because the TZID strings of the iCal format don't map cleanly onto Rails time zones.

Input schedule

DTSTART;TZID=MDT:20130731T143000
RRULE:FREQ=WEEKLY;UNTIL=20140730T203000Z;BYDAY=MO,WE,FR
RDATE;TZID=MDT:20150812T143000
RDATE;TZID=MDT:20150807T143000
EXDATE;TZID=MDT:20130823T143000
EXDATE;TZID=MDT:20130812T143000
EXDATE;TZID=MDT:20130807T143000
DTEND;TZID=MDT:20130731T153000

Output schedule on master

DTSTART;TZID=CEST:20130731T143000
RRULE:FREQ=WEEKLY;UNTIL=20140730T203000Z;BYDAY=MO,WE,FR
RDATE;TZID=CEST:20150812T143000
RDATE;TZID=CEST:20150807T143000
EXDATE;TZID=CEST:20130823T143000
EXDATE;TZID=CEST:20130812T143000
EXDATE;TZID=CEST:20130807T143000
DTEND;TZID=CEST:20130731T153000

Output schedule on this branch

DTSTART;TZID=MDT:20130731T143000
RRULE:FREQ=WEEKLY;UNTIL=20140730T203000Z;BYDAY=MO,WE,FR
RDATE;TZID=MDT:20150812T143000
RDATE;TZID=MDT:20150807T143000
EXDATE;TZID=MDT:20130823T143000
EXDATE;TZID=MDT:20130812T143000
EXDATE;TZID=MDT:20130807T143000
DTEND;TZID=MDT:20130731T153000

P.S. Note that time zones will not always match the exact same string, because something like "America/Denver" will get converted to "MDT" along the way. The times will still match semantically.

@epologee epologee marked this pull request as ready for review April 28, 2022 10:49
@@ -4,18 +4,27 @@ def self.schedule_from_ical(ical_string, options = {})
data = {}
ical_string.each_line do |line|
(property, value) = line.split(":")
(property, _tzid) = property.split(";")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This _tzid contained the ignored time zone information.

Time.find_zone!(zone) if zone.present?
rescue ArgumentError
(rails_zone, _tzinfo_id) = ActiveSupport::TimeZone::MAPPING.find do |(k, _)|
Time.find_zone!(k).now.strftime("%Z") == zone
Copy link
Author

@epologee epologee Apr 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where time zone identifiers like "MDT" are matched with Rails time zones, using strftime.

Comment on lines +381 to +395
it "round trips from and to ical with time zones in the summer (MDT)" do
original = <<-ICAL.gsub(/^\s*/, "").strip
DTSTART;TZID=MDT:20130731T143000
RRULE:FREQ=WEEKLY;UNTIL=20140730T203000Z;BYDAY=MO,WE,FR
RDATE;TZID=MDT:20150812T143000
RDATE;TZID=MDT:20150807T143000
EXDATE;TZID=MDT:20130823T143000
EXDATE;TZID=MDT:20130812T143000
EXDATE;TZID=MDT:20130807T143000
DTEND;TZID=MDT:20130731T153000
ICAL

schedule_from_ical = IceCube::Schedule.from_ical original
expect(schedule_from_ical.to_ical).to eq original
end
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may seem like a superfluous test compared to the one below, but before the last commit on this branch, this test would have failed when run during the winter (standard time vs. daylight saving time period).

I tested this using Timecop locally, but I didn't want to add a dependency to your repository just for this PR, so I didn't commit that part.

@pacso
Copy link
Collaborator

pacso commented May 3, 2022

Hey @epologee, thanks for sending this through!

I'll find some time in the next day or two to review this properly. In the mean time, can you add a changelog entry and ensure your tests pass?

epologee added a commit to stekker/ice_cube that referenced this pull request May 3, 2022
epologee added a commit to stekker/ice_cube that referenced this pull request May 3, 2022
epologee and others added 4 commits May 3, 2022 10:25
Using `.now` causes a search for "MST" in the winter, even if the time
of the given schedule is in summer and would only match "MDT". By
parsing the time value and using the components as values in the
candidate time zone, this issue is fixed.
@epologee
Copy link
Author

epologee commented May 3, 2022

Hi, thanks, will do / already done!

In the mean time, can you add a changelog entry and ensure your tests pass?

standardrb reports an indentation issue on lines that I did not touch (in a file I did touch), would you like me to add a commit to fix that indentation? Because the workflow needs your approval to run, I'm not sure if it passes or errors right now:

  lib/ice_cube/time_util.rb:289:18: Layout/CaseIndentation: Indent `when` as deep as `end`.
  lib/ice_cube/time_util.rb:290:18: Layout/CaseIndentation: Indent `when` as deep as `end`.
  lib/ice_cube/time_util.rb:291:18: Layout/CaseIndentation: Indent `when` as deep as `end`.
  lib/ice_cube/time_util.rb:292:18: Layout/CaseIndentation: Indent `when` as deep as `end`.
  lib/ice_cube/time_util.rb:293:18: Layout/CaseIndentation: Indent `when` as deep as `end`.
  lib/ice_cube/time_util.rb:294:18: Layout/CaseIndentation: Indent `when` as deep as `end`.

Cheers!

@pacso
Copy link
Collaborator

pacso commented May 3, 2022

I think once you get your first PR through, you don't need approval for future PR test runs.

@epologee
Copy link
Author

epologee commented May 5, 2022

would you like me to add a commit to fix that indentation?

Update: I added a commit because the standardrb lint prevents the other tests from running.

@suzumejakku
Copy link

Hi - any chance this will be merged ?

@jankeesvw
Copy link

We are running this PR in production now, would be great to get this in master.

@pacso
Copy link
Collaborator

pacso commented Jun 1, 2022

Can you resolve the failing tests? I'll then do a full review and merge it 👍🏻

Although ... these might be tests that I've broken by reverting another branch 🤔 If so ... I'll hopefully sort it soon, and release everything together.

@suzumejakku
Copy link

Hi @pacso,
Did you have any chance to review this ?

@suzumejakku
Copy link

Hi @pacso
Sorry to insist, but is there any way to make it into production ?

@frsantos
Copy link

frsantos commented Jul 6, 2023

Hi @seejohnrun @avit @pacso

I'm wondering... are any of you still maintaining this repo?

@pacso
Copy link
Collaborator

pacso commented Jul 8, 2023

Yes, we are. However I sadly don't have time to resolve the issues on this branch. If you can push changes to fix the failing tests, I'll happily merge it.

@epologee
Copy link
Author

I pushed a minor edit to re-run the specs. I recall that the failures looked like they weren't created by the changes in this PR, but can have another look after the workflow is approved.

CleanShot 2023-07-18 at 12 50 48@2x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants