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

Now #7

Merged
merged 3 commits into from
Oct 6, 2015
Merged
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
5 changes: 4 additions & 1 deletion src/TimeZones.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export TimeZone, FixedTimeZone, VariableTimeZone, ZonedDateTime,
firstdayofyear, lastdayofyear,
firstdayofquarter, lastdayofquarter,
# Re-export from Base.Dates
yearmonthday, yearmonth, monthday, year, month, week, day, dayofmonth
yearmonthday, yearmonth, monthday, year, month, week, day, dayofmonth,
# conversion.jl
now

const PKG_DIR = normpath(joinpath(dirname(@__FILE__), "..", "deps"))
const TZDATA_DIR = joinpath(PKG_DIR, "tzdata")
Expand All @@ -26,6 +28,7 @@ include("timezones/arithmetic.jl")
include("timezones/io.jl")
include("timezones/adjusters.jl")
include("timezones/Olson.jl")
include("timezones/conversions.jl")

function TimeZone(name::AbstractString)
tz_path = joinpath(COMPILED_DIR, split(name, "/")...)
Expand Down
5 changes: 5 additions & 0 deletions src/timezones/accessors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ hour(dt::ZonedDateTime) = hour(localtime(dt))
minute(dt::ZonedDateTime) = minute(localtime(dt))
second(dt::ZonedDateTime) = second(localtime(dt))
millisecond(dt::ZonedDateTime) = millisecond(localtime(dt))

@vectorize_1arg ZonedDateTime hour
@vectorize_1arg ZonedDateTime minute
@vectorize_1arg ZonedDateTime second
@vectorize_1arg ZonedDateTime millisecond
9 changes: 9 additions & 0 deletions src/timezones/conversions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Base.Dates: now, unix2datetime, Second

DateTime(zdt::ZonedDateTime) = localtime(zdt)
@vectorize_1arg ZonedDateTime DateTime

function now(tz::TimeZone)
utc = trunc(unix2datetime(time()), Second)
ZonedDateTime(utc, tz, from_utc=true)
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ include("timezones/accessors.jl")
include("timezones/arithmetic.jl")
include("timezones/io.jl")
include("timezones/adjusters.jl")
include("timezones/conversions.jl")
include("timezone_names.jl")
16 changes: 16 additions & 0 deletions test/timezones/accessors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,19 @@ zdt = ZonedDateTime(DateTime(2014,6,12,23,59,58,57), fixed)
@test Dates.yearmonth(zdt) == (2014, 6)
@test Dates.monthday(zdt) == (6, 12)
@test Dates.yearmonthday(zdt) == (2014, 6, 12)

# Vectorized accessors
# Note: repmat is used over broadcast to test for size and equality.
arr = repmat([zdt], 10)
@test TimeZones.hour(arr) == repmat([23], 10)
@test TimeZones.minute(arr) == repmat([59], 10)
@test TimeZones.second(arr) == repmat([58], 10)
@test TimeZones.millisecond(arr) == repmat([57], 10)

@test Dates.year(arr) == repmat([2014], 10)
@test Dates.month(arr) == repmat([6], 10)
@test Dates.day(arr) == repmat([12], 10)
@test Dates.dayofmonth(arr) == repmat([12], 10)
@test Dates.yearmonth(arr) == repmat([(2014, 6)], 10)
@test Dates.monthday(arr) == repmat([(6, 12)], 10)
@test Dates.yearmonthday(arr) == repmat([(2014, 6, 12)], 10)
20 changes: 20 additions & 0 deletions test/timezones/conversions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
warsaw = resolve("Europe/Warsaw", tzdata["europe"]...)

# Converting a ZonedDateTime into a DateTime
dt = DateTime(2015, 1, 1, 0)
zdt = ZonedDateTime(dt, warsaw)
@test DateTime(zdt) == dt

# Converting from ZonedDateTime to DateTime isn't possible as it is always inexact.
@test_throws MethodError convert(DateTime, zdt)

# Vectorized accessors
arr = repmat([zdt], 10)
@test Dates.DateTime(arr) == repmat([dt], 10)

# now function
dt = Dates.unix2datetime(time()) # Base.now in UTC
zdt = now(warsaw)
@test zdt.timezone == warsaw
@test isapprox(map(Dates.datetime2unix, [dt, TimeZones.utc(zdt)])...)
@test millisecond(zdt) == 0 # Base.now only includes up to seconds