Skip to content

Commit

Permalink
apply function
Browse files Browse the repository at this point in the history
  • Loading branch information
femtotrader committed Nov 7, 2016
1 parent d8e80ea commit 8a0d190
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 38 deletions.
8 changes: 6 additions & 2 deletions src/TimeFrames.jl
Expand Up @@ -2,8 +2,9 @@ module TimeFrames

using Base: Dates

#export TimeFrame, Boundary
#export Millisecondly, Secondly, Minutely, Hourly, Daily, Weekly, Monthly, Yearly
export TimeFrame, Boundary
export Millisecondly, Secondly, Minutely, Hourly, Daily, Weekly, Monthly, Yearly
export apply

abstract TimeFrame

Expand Down Expand Up @@ -91,5 +92,8 @@ _d_f_boundary = Dict(
End::Boundary => ceil
)

function apply(tf, dt)
dt_grouper(tf)(dt)
end

end # module
72 changes: 36 additions & 36 deletions test/test_timeframe.jl
@@ -1,7 +1,8 @@
using TimeFrames: TimeFrame
using TimeFrames: Yearly, Monthly, Weekly, Daily, Hourly, Minutely, Secondly, Millisecondly
using TimeFrames: shortcut, dt_grouper
using TimeFrames: shortcut
using TimeFrames: Begin, End
using TimeFrames: apply

using Base.Test

Expand Down Expand Up @@ -36,50 +37,49 @@ tf = TimeFrame("15T")
dt = DateTime(2016, 7, 20, 13, 24, 35, 245)

tf = TimeFrame(dt -> floor(dt, Dates.Minute(15))) # custom TimeFrame with lambda function as DateTime grouper
f_group = dt_grouper(tf)
@test f_group(dt) == DateTime(2016, 7, 20, 13, 15, 0, 0)
@test apply(tf, dt) == DateTime(2016, 7, 20, 13, 15, 0, 0)

f_group = dt_grouper(Yearly())
#@test f_group(dt) == 2016
#@test f_group(dt) == DateTime(2016, 1, 1, 0, 0, 0, 0)
@test f_group(dt) == Date(2016, 1, 1)
tf = Yearly()
#@test apply(tf, dt) == 2016
#@test apply(tf, dt) == DateTime(2016, 1, 1, 0, 0, 0, 0)
@test apply(tf, dt) == Date(2016, 1, 1)
#@test typeof(f_group(dt)) == Date

f_group = dt_grouper(Monthly())
#@test f_group(dt) == (2016, 7)
#@test f_group(dt) == DateTime(2016, 7, 1, 0, 0, 0, 0)
@test f_group(dt) == Date(2016, 7, 1)
tf = Monthly()
#@test apply(tf, dt) == (2016, 7)
#@test apply(tf, dt) == DateTime(2016, 7, 1, 0, 0, 0, 0)
@test apply(tf, dt) == Date(2016, 7, 1)

f_group = dt_grouper(Weekly())
#@test f_group(dt) == DateTime(2016, 7, 18, 0, 0, 0, 0)
@test f_group(dt) == Date(2016, 7, 18)
tf = Weekly()
#@test apply(tf, dt) == DateTime(2016, 7, 18, 0, 0, 0, 0)
@test apply(tf, dt) == Date(2016, 7, 18)

f_group = dt_grouper(Daily())
#@test f_group(dt) == (2016, 7, 20)
#@test f_group(dt) == DateTime(2016, 7, 20, 0, 0, 0, 0)
@test f_group(dt) == Date(2016, 7, 20)
tf = Daily()
#@test apply(tf, dt) == (2016, 7, 20)
#@test apply(tf, dt) == DateTime(2016, 7, 20, 0, 0, 0, 0)
@test apply(tf, dt) == Date(2016, 7, 20)

f_group = dt_grouper(Hourly())
#@test f_group(dt) == (2016, 7, 20, 13)
@test f_group(dt) == DateTime(2016, 7, 20, 13, 0, 0, 0)
tf = Hourly()
#@test apply(tf, dt) == (2016, 7, 20, 13)
@test apply(tf, dt) == DateTime(2016, 7, 20, 13, 0, 0, 0)

f_group = dt_grouper(Minutely())
#@test f_group(dt) == (2016, 7, 20, 13, 24)
@test f_group(dt) == DateTime(2016, 7, 20, 13, 24, 0, 0)
tf = Minutely()
#@test apply(tf, dt) == (2016, 7, 20, 13, 24)
@test apply(tf, dt) == DateTime(2016, 7, 20, 13, 24, 0, 0)

f_group = dt_grouper(Secondly())
#@test f_group(dt) == (2016, 7, 20, 13, 24, 35)
@test f_group(dt) == DateTime(2016, 7, 20, 13, 24, 35, 0)
tf = Secondly()
#@test apply(tf, dt) == (2016, 7, 20, 13, 24, 35)
@test apply(tf, dt) == DateTime(2016, 7, 20, 13, 24, 35, 0)

f_group = dt_grouper(Millisecondly())
#@test f_group(dt) == (2016, 7, 20, 13, 24, 35, 245)
@test f_group(dt) == DateTime(2016, 7, 20, 13, 24, 35, 245)
tf = Millisecondly()
#@test apply(tf, dt) == (2016, 7, 20, 13, 24, 35, 245)
@test apply(tf, dt) == DateTime(2016, 7, 20, 13, 24, 35, 245)

f_group = dt_grouper(Yearly(10))
@test f_group(dt) == DateTime(2010, 1, 1, 0, 0, 0, 0)
tf = Yearly(10)
@test apply(tf, dt) == DateTime(2010, 1, 1, 0, 0, 0, 0)

f_group = dt_grouper(Yearly(10, boundary=End))
@test f_group(dt) == DateTime(2020, 1, 1, 0, 0, 0, 0)
tf = Yearly(10, boundary=End)
@test apply(tf, dt) == DateTime(2020, 1, 1, 0, 0, 0, 0)

f_group = dt_grouper(Minutely(15))
@test f_group(dt) == DateTime(2016, 7, 20, 13, 15, 0, 0)
tf = Minutely(15)
@test apply(tf, dt) == DateTime(2016, 7, 20, 13, 15, 0, 0)

0 comments on commit 8a0d190

Please sign in to comment.