Skip to content

Commit

Permalink
Implement todayat
Browse files Browse the repository at this point in the history
  • Loading branch information
omus committed Jan 30, 2018
1 parent f383115 commit 73cca75
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/TimeZones.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export TimeZone, @tz_str, istimezone, FixedTimeZone, VariableTimeZone, ZonedDate
# Re-export from Dates
yearmonthday, yearmonth, monthday, year, month, week, day, dayofmonth,
# conversion.jl
now, today, astimezone,
now, today, todayat, astimezone,
# local.jl
localzone,
# ranges.jl
Expand Down
24 changes: 24 additions & 0 deletions src/conversions.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Compat.Dates: unix2datetime, datetime2unix, julian2datetime, datetime2julian,
now, today
using Mocking

# UTC is an abstract type defined in Dates, for some reason
const utc_tz = FixedTimeZone("UTC")
Expand Down Expand Up @@ -41,6 +42,29 @@ julia> today(tz"Pacific/Midway"), today(tz"Pacific/Apia")
"""
today(tz::TimeZone) = Date(localtime(now(tz)))

"""
todayat(tod::Time, tz::TimeZone, [amb]) -> ZonedDateTime
Creates a `ZonedDateTime` for today at the specified time of day. If the result is ambiguous
in the given `TimeZone` then `amb` can be supplied to resolve ambiguity.
# Examples
```julia
julia> today(tz"Europe/Warsaw")
2017-11-09
julia> todayat(Time(10, 30), tz"Europe/Warsaw")
2017-11-09T10:30:00+01:00
```
"""
function todayat(tod::Time, tz::VariableTimeZone, amb::Union{Integer,Bool})
ZonedDateTime((@mock today(tz)) + tod, tz, amb)
end

todayat(tod::Time, tz::TimeZone) = ZonedDateTime((@mock today(tz)) + tod, tz)


"""
astimezone(zdt::ZonedDateTime, tz::TimeZone) -> ZonedDateTime
Expand Down
15 changes: 15 additions & 0 deletions test/conversions.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Compat.Dates
using Mocking

utc = FixedTimeZone("UTC")
warsaw = resolve("Europe/Warsaw", tzdata["europe"]...)
Expand Down Expand Up @@ -28,6 +29,20 @@ zdt = now(warsaw)
@test abs(today() - today(warsaw)) <= Dates.Day(1)
@test today(apia) - today(midway) == Dates.Day(1)

# todayat function
zdt = now(warsaw)
now_time = Time(TimeZones.localtime(zdt))
@test todayat(now_time, warsaw) == zdt

if !compiled_modules_enabled
patch = @patch today(tz::TimeZone) = Date(1916, 10, 1)
apply(patch) do
@test_throws AmbiguousTimeError todayat(Time(0), warsaw)
@test todayat(Time(0), warsaw, 1) == ZonedDateTime(1916, 10, 1, 0, warsaw, 1)
@test todayat(Time(0), warsaw, 2) == ZonedDateTime(1916, 10, 1, 0, warsaw, 2)
end
end


# Changing time zones
dt = DateTime(2015, 1, 1, 0)
Expand Down

0 comments on commit 73cca75

Please sign in to comment.