Skip to content

Commit

Permalink
period_step, tonext
Browse files Browse the repository at this point in the history
  • Loading branch information
femtotrader committed Dec 11, 2017
1 parent 6a69547 commit 914a36f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
20 changes: 19 additions & 1 deletion src/TimeFrames.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,19 @@ function _period_step(::Type{Date})
Dates.Day(1)
end

const period_step = Dates.Millisecond(1)

function _period_step(::Type{DateTime})
Dates.Millisecond(1)
period_step
end

#struct Microsecond <: AbstractTimePeriodFrame
# period::Dates.TimePeriod
# boundary::Boundary
#end
#Microsecond() = Microsecond(Dates.Microsecond(1), Begin)
#Microsecond(n::Integer) = Microsecond(Dates.Microsecond(n), Begin)

struct Millisecond <: AbstractTimePeriodFrame
period::Dates.TimePeriod
boundary::Boundary
Expand Down Expand Up @@ -234,6 +243,15 @@ function apply(tf::TimeFrame, dt)
dt_grouper(tf, typeof(dt))(dt)
end

function tonext(tf::TimeFrame, dt::Dates.TimeType)
dt2 = apply(tf, dt)
if dt2 < dt
dt2 + tf
else
dt2
end
end

# range
function range(dt1::TimeType, tf::AbstractPeriodFrame, dt2::TimeType; apply_tf=true)
td = _period_step(typeof(dt2))
Expand Down
31 changes: 27 additions & 4 deletions test/test_timeframe.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using TimeFrames
using TimeFrames: TimePeriodFrame, DatePeriodFrame, _period_step, CustomTimeFrame
using TimeFrames: TimePeriodFrame, DatePeriodFrame
using TimeFrames: period_step, _period_step
using TimeFrames: CustomTimeFrame, tonext


using Base.Test
Expand Down Expand Up @@ -46,7 +48,7 @@ using Base.Test
end

@testset "_period_step" begin
@test _period_step(DateTime) == Dates.Millisecond(1)
@test _period_step(DateTime) == period_step
@test _period_step(Date) == Dates.Day(1)
end
end
Expand Down Expand Up @@ -153,7 +155,7 @@ end

@testset "grouper/apply" begin
@test apply(MonthEnd(), Date(2010, 2, 20)) == Date(2010, 2, 28)
@test apply(MonthEnd(), DateTime(2010, 2, 20)) == DateTime(2010, 2, 28, 23, 59, 59, 999)
@test apply(MonthEnd(), DateTime(2010, 2, 20)) == DateTime(2010, 3, 1) - period_step

d = Date(2016, 7, 20)
dt = DateTime(2016, 7, 20, 13, 24, 35, 245)
Expand Down Expand Up @@ -206,12 +208,33 @@ end

tf = YearEnd(10)
@test apply(tf, d) == DateTime(2019, 12, 31)
@test apply(tf, dt) == DateTime(2019, 12, 31, 23, 59, 59, 999)
@test apply(tf, dt) == DateTime(2020, 1, 1) - period_step

tf = Minute(15)
@test apply(tf, dt) == DateTime(2016, 7, 20, 13, 15, 0, 0)
end

@testset "tonext" begin
dt = DateTime(2010, 1, 1, 10, 30)
@test tonext(TimeFrame("2H"), dt) == DateTime(2010, 1, 1, 12, 0)

dt = DateTime(2010, 1, 1, 10, 30)
@test tonext(TimeFrame("1D"), dt) == DateTime(2010, 1, 2, 0, 0)

dt = DateTime(2010, 1, 1, 10, 30)
@test tonext(TimeFrame("1MS"), dt) == DateTime(2010, 2, 1, 0, 0)

dt = DateTime(2010, 1, 1, 10, 30)
@test tonext(TimeFrame("1M"), dt) == DateTime(2010, 2, 1) - period_step

dt = DateTime(2010, 6, 5, 10, 30)
@test tonext(TimeFrame("1A"), dt) == DateTime(2011, 1, 1) - period_step

dt = DateTime(2010, 6, 5, 10, 30)
@test tonext(TimeFrame("1AS"), dt) == DateTime(2011, 1, 1)

end

@testset "range" begin
dt1 = DateTime(2010, 1, 1, 20)
dt2 = DateTime(2010, 1, 14, 16)
Expand Down

1 comment on commit 914a36f

@femtotrader
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closes #41

Please sign in to comment.