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

Handle EXDATE with a comma separated list of datetimes #213

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions lib/cocktail/parser/i_calendar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,28 @@ defmodule Cocktail.Parser.ICalendar do
end
end

@spec parse_datetimes_list(String.t()) :: {:ok, [Cocktail.time()]} | {:error, term}
defp parse_datetimes_list(time_string) do
with [tzid, datetimes] <- String.split(time_string, ":") do
datetimes
|> String.split(",")
|> Enum.map(&("#{tzid}:#{&1}"))
|> parse_datetimes_values([])
else
_ ->
{:error, :invalid_time_format}
end
end

@spec parse_datetimes_values([String.t()], [Cocktail.time()]) :: {:ok, [Cocktail.time()]} | {:error, term}
defp parse_datetimes_values([], datetimes_list), do: {:ok, datetimes_list}

defp parse_datetimes_values([head | rest], datetimes_list) do
with {:ok, datetime} <- parse_datetime(head) do
parse_datetimes_values(rest, [datetime | datetimes_list])
end
end

@spec parse_naive_datetime(String.t()) :: {:ok, NaiveDateTime.t()} | {:error, term}
defp parse_naive_datetime(time_string), do: Timex.parse(time_string, @datetime_format)

Expand Down Expand Up @@ -470,9 +492,12 @@ defmodule Cocktail.Parser.ICalendar do

@spec parse_exdate(String.t(), Schedule.t(), non_neg_integer) :: {:ok, Schedule.t()} | {:error, term}
defp parse_exdate(time_string, schedule, index) do
case parse_datetime(time_string) do
{:ok, datetime} -> {:ok, Schedule.add_exception_time(schedule, datetime)}
{:error, term} -> {:error, {term, index}}
case parse_datetimes_list(time_string) do
{:ok, datetimes} ->
{:ok, Enum.reduce(datetimes, schedule, &(Schedule.add_exception_time(&2, &1)))}

{:error, term} ->
{:error, {term, index}}
end
end
end
24 changes: 24 additions & 0 deletions test/cocktail/parser/i_calendar_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,30 @@ defmodule Cocktail.Parser.ICalendarTest do
assert datetime == ~N[2017-01-01 06:00:00]
end

test "parses a schedule with an EXDATE list" do
schedule_string = """
DTSTART:20170101T060000
EXDATE:20170101T060000,20170102T060000
"""

assert {:ok, schedule} = parse(schedule_string)
assert [%NaiveDateTime{} = first_datetime, %NaiveDateTime{} = second_datetime] = schedule.exception_times
assert first_datetime == ~N[2017-01-01 06:00:00]
assert second_datetime == ~N[2017-01-02 06:00:00]
end

test "parses a schedule with an EXDATE list and a TZID" do
schedule_string = """
DTSTART;TZID=America/Los_Angeles:20170101T060000
EXDATE;TZID=America/Los_Angeles:20170101T060000,20170102T060000
"""

assert {:ok, schedule} = parse(schedule_string)
assert [%DateTime{} = first_datetime, %DateTime{} = second_datetime] = schedule.exception_times
assert first_datetime == ~Y[2017-01-01 06:00:00 America/Los_Angeles]
assert second_datetime == ~Y[2017-01-02 06:00:00 America/Los_Angeles]
end

##########
# Errors #
##########
Expand Down