Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- Fixed `Timex.Duration.now/1` raising for `:minutes`, `:hours`, `:days` and `:weeks`
- Corrected typespecs of `Timex.Calendar.Julian.julian_date/1`, `Timex.normalize/2`, `Timex.Protocol.DateTime.shift/2` and `Timex.Parse.ZoneInfo.Parser.parse/1`
- Updated `Timex.now/1` typespec to remove the `AmbiguousDateTime`
- Corrected pluralization rules for bg/cs/he/id/ro/ru
- Fixed documentation formatting of `Timex.TimezoneInfo.create/6`
Expand Down
2 changes: 1 addition & 1 deletion lib/calendar/julian.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule Timex.Calendar.Julian do
do: julian_date(year, month, day)

# Same as julian_date/1, except takes an Erlang datetime, and returns a more precise Julian date number
@spec julian_date(Types.datetime()) :: integer
@spec julian_date(Types.datetime()) :: float
def julian_date({{year, month, day}, {hour, minute, second}}) do
julian_date(year, month, day, hour, minute, second)
end
Expand Down
3 changes: 2 additions & 1 deletion lib/datetime/datetime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,8 @@ defimpl Timex.Protocol, for: DateTime do

See docs for Timex.shift/2 for details.
"""
@spec shift(DateTime.t(), list({atom(), term})) :: DateTime.t() | {:error, term}
@spec shift(DateTime.t(), list({atom(), term})) ::
DateTime.t() | AmbiguousDateTime.t() | {:error, term}
def shift(%DateTime{} = datetime, shifts) when is_list(shifts) do
{logical_shifts, shifts} = Keyword.split(shifts, [:years, :months, :weeks, :days])
shift = calculate_shift(shifts)
Expand Down
2 changes: 1 addition & 1 deletion lib/parse/zoneinfo/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ defmodule Timex.Parse.ZoneInfo.Parser do

Parses the timezone information inside, and returns it as a Zone struct.
"""
@spec parse(binary) :: {:ok, Zone.t()} | {:error, binary}
@spec parse(binary) :: {:ok, Zone.t()} | {:error, binary | :invalid_zoneinfo_content}
def parse(<<?T, ?Z, ?i, ?f, version::bytes(1), _reserved::bytes(15), rest::binary>>) do
version =
case version do
Expand Down
8 changes: 4 additions & 4 deletions lib/time/duration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -625,10 +625,10 @@ defmodule Timex.Duration do
do: System.system_time(:millisecond)

def now(s) when s in [:second, :seconds], do: System.system_time(:second)
def now(:minutes), do: to_minutes(now(:microsecond))
def now(:hours), do: to_hours(now(:microsecond))
def now(:days), do: to_days(now(:microsecond))
def now(:weeks), do: to_weeks(now(:microsecond))
def now(:minutes), do: to_minutes(now(), truncate: true)
def now(:hours), do: to_hours(now(), truncate: true)
def now(:days), do: to_days(now(), truncate: true)
def now(:weeks), do: to_weeks(now(), truncate: true)

@doc """
An alias for `Duration.diff/3`
Expand Down
4 changes: 2 additions & 2 deletions lib/timex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1898,8 +1898,8 @@ defmodule Timex do
23
"""
@spec normalize(:date, {integer, integer, integer}) :: Types.date()
@spec normalize(:time, {integer, integer, integer} | {integer, integer, integer, integer}) ::
Types.time()
@spec normalize(:time, {integer, integer, integer}) :: Types.time()
@spec normalize(:time, {integer, integer, integer, integer}) :: Types.microsecond_time()
@spec normalize(:day, {integer, integer, integer}) :: non_neg_integer
@spec normalize(
:year | :month | :day | :hour | :minute | :second | :millisecond | :microsecond,
Expand Down
11 changes: 11 additions & 0 deletions test/duration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,17 @@ defmodule DurationTests do
difference_seconds = timex_now_seconds - erlang_now_seconds
assert_in_delta(difference_seconds, 0.5, 1)
end

test "now/1 returns whole minutes, hours, days and weeks" do
for {unit, divisor} <- [minutes: 60, hours: 3_600, days: 86_400, weeks: 604_800] do
before_seconds = System.system_time(:second)
result = Timex.Duration.now(unit)
after_seconds = System.system_time(:second)

assert is_integer(result)
assert result in div(before_seconds, divisor)..div(after_seconds, divisor)
end
end
end

def something_to_measure(x), do: x
Expand Down