Skip to content

Commit

Permalink
Remove unused Types
Browse files Browse the repository at this point in the history
Since we ported over Types from the Timex hex package, let's remove the
unused types
  • Loading branch information
treble37 committed Nov 29, 2017
1 parent 19c5d63 commit a99a448
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 48 deletions.
8 changes: 6 additions & 2 deletions lib/dates_times/comparator.ex
Expand Up @@ -2,16 +2,20 @@ defmodule ESpec.DatesTimes.Comparator do
@moduledoc false

alias ESpec.DatesTimes.Delegator
alias ESpec.DatesTimes.Types

@units [:years, :months, :weeks, :days,
:hours, :minutes, :seconds, :milliseconds, :microseconds]

@spec diff(Types.microseconds, Types.microseconds, Comparable.granularity) :: integer
@spec diff(Types.valid_datetime, Types.valid_datetime, Comparable.granularity) :: integer
@spec diff(non_neg_integer, non_neg_integer, Types.time_units) :: integer
def diff(a, a, granularity) when is_integer(a), do: zero(granularity)

@spec diff(non_neg_integer, non_neg_integer, Types.time_units) :: integer
def diff(a, b, granularity) when is_integer(a) and is_integer(b) and is_atom(granularity) do
do_diff(a, b, granularity)
end

@spec diff(non_neg_integer, non_neg_integer, Types.time_units) :: integer
def diff(a, b, granularity) do
case {Delegator.to_comparison_units(a), Delegator.to_comparison_units(b)} do
{{:error, _} = err, _} -> err
Expand Down
2 changes: 1 addition & 1 deletion lib/dates_times/date_time_protocol.ex
Expand Up @@ -8,6 +8,6 @@ defprotocol ESpec.DatesTimes.DateTimeProtocol do
@doc """
Convert a date/time value to gregorian microseconds (microseconds since the start of year zero)
"""
@spec to_comparison_units(Types.valid_datetime) :: non_neg_integer | {:error, term}
@spec to_comparison_units(Types.calendar_types) :: non_neg_integer | {:error, term}
def to_comparison_units(datetime)
end
7 changes: 5 additions & 2 deletions lib/dates_times/delegator.ex
@@ -1,4 +1,6 @@
defmodule ESpec.DatesTimes.Delegator do
alias ESpec.DatesTimes.Types

@moduledoc """
The ESpec.DateTimes.Delegator module. Used to delegate methods to the correct protocol
implementation.
Expand All @@ -8,6 +10,7 @@ defmodule ESpec.DatesTimes.Delegator do
Convert a date/time value to (gregorian) microseconds
(microseconds since start of year zero if gregorian)
"""
@spec to_comparison_units(Types.valid_datetime) :: non_neg_integer | {:error, term}
defdelegate to_comparison_units(datetime), to: ESpec.DatesTimes.DateTimeProtocol

@spec to_comparison_units(Types.calendar_types) :: non_neg_integer | {:error, term}
defdelegate to_comparison_units(calendar_type), to: ESpec.DatesTimes.DateTimeProtocol
end
5 changes: 3 additions & 2 deletions lib/dates_times/impl/date.ex
Expand Up @@ -3,11 +3,12 @@ defimpl ESpec.DatesTimes.DateTimeProtocol, for: Date do
This module represents all functions specific to creating/manipulating/comparing Dates (year/month/day)
"""

@spec to_comparison_units(Date.t) :: non_neg_integer
def to_comparison_units(date), do: to_gregorian_microseconds(date)

@spec to_gregorian_microseconds(Date.t) :: non_neg_integer
def to_gregorian_microseconds(date), do: (to_seconds(date, :zero) * (1_000*1_000))
def to_gregorian_microseconds(date), do: (to_seconds(date) * (1_000*1_000))

defp to_seconds(%Date{year: y, month: m, day: d}, :zero),
defp to_seconds(%Date{year: y, month: m, day: d}),
do: :calendar.datetime_to_gregorian_seconds({{y,m,d},{0,0,0}})
end
1 change: 1 addition & 0 deletions lib/dates_times/impl/datetime.ex
Expand Up @@ -9,6 +9,7 @@ defimpl ESpec.DatesTimes.DateTimeProtocol, for: DateTime do
Time intervals in this module don't account for leap seconds.
"""

@spec to_comparison_units(DateTime.t) :: non_neg_integer
def to_comparison_units(%{std_offset: std_offset, utc_offset: utc_offset} = datetime) do
microseconds = datetime
|> Calendar.ISO.Extension.to_iso_days()
Expand Down
5 changes: 3 additions & 2 deletions lib/dates_times/impl/naive_datetime.ex
Expand Up @@ -3,14 +3,15 @@ defimpl ESpec.DatesTimes.DateTimeProtocol, for: NaiveDateTime do
This module represents all functions specific to creating/manipulating/comparing Dates (year/month/day)
"""

@spec to_comparison_units(NaiveDateTime.t) :: non_neg_integer
def to_comparison_units(date), do: to_gregorian_microseconds(date)

@spec to_gregorian_microseconds(NaiveDateTime.t) :: non_neg_integer
def to_gregorian_microseconds(%NaiveDateTime{microsecond: {us,_}} = naive_datetime) do
s = to_seconds(naive_datetime, :zero)
s = to_seconds(naive_datetime)
(s*(1_000*1_000)) + us
end

defp to_seconds(%NaiveDateTime{year: y, month: m, day: d, hour: h, minute: mm, second: s}, :zero),
defp to_seconds(%NaiveDateTime{year: y, month: m, day: d, hour: h, minute: mm, second: s}),
do: :calendar.datetime_to_gregorian_seconds({{y,m,d},{h,mm,s}})
end
7 changes: 4 additions & 3 deletions lib/dates_times/impl/time.ex
Expand Up @@ -3,12 +3,13 @@ defimpl ESpec.DatesTimes.DateTimeProtocol, for: Time do
This module represents all functions specific to creating/manipulating/comparing Times (year/month/day)
"""

def to_comparison_units(date), do: to_gregorian_microseconds(date)
@spec to_comparison_units(Time.t) :: non_neg_integer
def to_comparison_units(time), do: to_gregorian_microseconds(time)

@spec to_gregorian_microseconds(Time.t) :: non_neg_integer
def to_gregorian_microseconds(time), do: (to_microseconds(time, :zero))
def to_gregorian_microseconds(time), do: (to_microseconds(time))

defp to_microseconds(%Time{hour: h, minute: mm, second: s, microsecond: {usec, _precision}}, :zero) do
defp to_microseconds(%Time{hour: h, minute: mm, second: s, microsecond: {usec, _precision}}) do
(h * 36 * 100_000_000) + (mm * 60 * 1_000_000) + (s * 1_000_000) + usec
end
end
36 changes: 0 additions & 36 deletions lib/dates_times/types.ex
@@ -1,41 +1,5 @@
defmodule ESpec.DatesTimes.Types do

# Date types
@type year :: Calendar.year
@type month :: Calendar.month
@type day :: Calendar.day
@type num_of_days :: 28..31
@type daynum :: 1..366
@type week_of_month :: 1..5
@type weekday :: 1..7
@type weeknum :: 1..53
# Time types
@type hour :: Calendar.hour
@type minute :: Calendar.minute
@type second :: Calendar.second
@type microsecond :: Calendar.microsecond
@type timestamp :: {megaseconds, seconds, microseconds }
@type megaseconds :: non_neg_integer
@type seconds :: non_neg_integer
@type microseconds :: non_neg_integer
# Timezone types
@type time_zone :: Calendar.time_zone
@type zone_abbr :: Calendar.zone_abbr
@type utc_offset :: Calendar.utc_offset
@type std_offset :: Calendar.std_offset
@type tz_offset :: -14..12
@type valid_timezone :: String.t | tz_offset | :utc | :local
# Complex types
@type weekday_name :: :monday | :tuesday | :wednesday | :thursday | :friday | :saturday | :sunday
@type shift_units :: :milliseconds | :seconds | :minutes | :hours | :days | :weeks | :years
@type time_units :: :microseconds | :milliseconds | :seconds | :minutes | :hours | :days | :weeks | :years
@type time :: { hour, minute, second }
@type microsecond_time :: { hour, minute, second, microsecond | microseconds}
@type date :: { year, month, day }
@type datetime :: { date, time }
@type microsecond_datetime :: { date, microsecond_time }
@type iso_triplet :: { year, weeknum, weekday }
@type calendar_types :: Date.t | DateTime.t | NaiveDateTime.t | Time.t
@type valid_datetime :: calendar_types | datetime | date | microsecond_datetime
@type weekstart :: weekday | binary | atom
end

0 comments on commit a99a448

Please sign in to comment.