Skip to content

Commit

Permalink
Merge pull request #45 from femtotrader/update_julia_0.7
Browse files Browse the repository at this point in the history
Julia 0.7
  • Loading branch information
femtotrader authored Jul 19, 2018
2 parents e388ea6 + 34e7dd2 commit 4b6cfa8
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ os:
- linux
- osx
julia:
- 0.6
- 0.7
- nightly
matrix:
allow_failures:
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ julia> apply(TimeFrame("2H"), DateTime(2016, 9, 11, 20, 9))
```

This library is used by
- [TimeSeriesResampler.jl](https://github.com/femtotrader/TimeSeriesResampler.jl)
- [TimeSeriesIO.jl](https://github.com/femtotrader/TimeSeriesIO.jl)

- [TimeSeriesResampler.jl](https://github.com/femtotrader/TimeSeriesResampler.jl)
- [TimeSeriesIO.jl](https://github.com/femtotrader/TimeSeriesIO.jl)
3 changes: 2 additions & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
julia 0.6
julia 0.7
Dates
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
environment:
matrix:
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.7/julia-0.7-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.7/julia-0.7-latest-win64.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"

Expand Down
38 changes: 19 additions & 19 deletions src/TimeFrames.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ module TimeFrames

import Base: range, +, -, *

using Base.Dates: TimeType
using Dates

export TimeFrame, Boundary
export YearBegin, YearEnd
export MonthBegin, MonthEnd
export Millisecond, Second, Minute, Hour, Day, Week
# export Millisecond, Second, Minute, Hour, Day, Week
export NoTimeFrame
export apply, range
export Begin, End
Expand Down Expand Up @@ -67,70 +67,70 @@ end
#Microsecond(n::Integer) = Microsecond(Dates.Microsecond(n), Begin)

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

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

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

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

struct Day <: AbstractDatePeriodFrame
period::Dates.DatePeriod
period::Dates.Day
boundary::Boundary
end
Day() = Day(Dates.Day(1), Begin)
Day(n::Integer) = Day(Dates.Day(n), Begin)

struct Week <: AbstractDatePeriodFrame
period::Dates.DatePeriod
period::Dates.Week
boundary::Boundary
end
Week() = Week(Dates.Week(1), Begin)
Week(n::Integer) = Week(Dates.Week(n), Begin)

struct MonthEnd <: AbstractDatePeriodFrame
period::Dates.DatePeriod
period::Dates.Month
boundary::Boundary
end
MonthEnd() = MonthEnd(Dates.Month(1), End)
MonthEnd(n::Integer) = MonthEnd(Dates.Month(n), End)

struct MonthBegin <: AbstractDatePeriodFrame
period::Dates.DatePeriod
period::Dates.Month
boundary::Boundary
end
MonthBegin() = MonthBegin(Dates.Month(1), Begin)
MonthBegin(n::Integer) = MonthBegin(Dates.Month(n), Begin)

struct YearEnd <: AbstractDatePeriodFrame
period::Dates.DatePeriod
period::Dates.Year
boundary::Boundary
end
YearEnd() = YearEnd(Dates.Year(1), End)
YearEnd(n::Integer) = YearEnd(Dates.Year(n), End)

struct YearBegin <: AbstractDatePeriodFrame
period::Dates.DatePeriod
period::Dates.Year
boundary::Boundary
end
YearBegin() = YearBegin(Dates.Year(1), Begin)
Expand Down Expand Up @@ -257,7 +257,7 @@ function tonext(tf::TimeFrame, dt::Dates.TimeType; same=false)
end

# range
function range(dt1::TimeType, tf::AbstractPeriodFrame, dt2::TimeType; apply_tf=true)
function range(dt1::Dates.TimeType, tf::AbstractPeriodFrame, dt2::Dates.TimeType; apply_tf=true)
td = _period_step(typeof(dt2))
if apply_tf
apply(tf, dt1):tf.period:apply(tf, dt2-td)
Expand All @@ -266,11 +266,11 @@ function range(dt1::TimeType, tf::AbstractPeriodFrame, dt2::TimeType; apply_tf=t
end
end

function range(dt1::TimeType, tf::AbstractPeriodFrame, len::Integer)
function range(dt1::Dates.TimeType, tf::AbstractPeriodFrame, len::Integer)
range(dt1, tf.period, len)
end

function range(tf::AbstractPeriodFrame, dt2::TimeType, len::Integer)
function range(tf::AbstractPeriodFrame, dt2::Dates.TimeType, len::Integer)
range(dt2 - len * tf.period, tf.period, len)
end

Expand All @@ -296,13 +296,13 @@ promote_timetype(::Type{Dates.Time}, ::Type{MonthEnd}) = throw(InexactError())
promote_timetype(::Type{Dates.Time}, ::Type{Week}) = throw(InexactError())
promote_timetype(::Type{Dates.Time}, ::Type{Day}) = throw(InexactError())

+(t::TimeType, tf::TimeFrame) =
convert(promote_timetype(typeof(t), typeof(tf)), t) + tf.period
+(t::T, tf::TF) where {T<:Dates.TimeType, TF<:TimeFrame} =
convert(promote_timetype(T, TF), t) + tf.period

+(tf::TimeFrame, t::TimeType) = t + tf

-(t::TimeType, tf::TimeFrame) =
convert(promote_timetype(typeof(t), typeof(tf)), t) - tf.period
-(t::T, tf::TF) where {T<:Dates.TimeType, TF<:TimeFrame} =
convert(promote_timetype(T, TF), t) - tf.period


*(tf::AbstractPeriodFrame, n::Int) = typeof(tf)(tf.period * n, tf.boundary)
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Base.Test
using Test

# write your own tests here
include("test_timeframe.jl")
41 changes: 21 additions & 20 deletions test/test_timeframe.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Dates
using TimeFrames
using TimeFrames: TimePeriodFrame, DatePeriodFrame
using TimeFrames: period_step, _period_step
using TimeFrames: CustomTimeFrame, tonext


using Base.Test
using Test

@testset "low level (period frame)" begin
@testset "TimePeriodFrame" begin
Expand Down Expand Up @@ -63,26 +64,26 @@ end
@test YearEnd() != YearBegin()
@test YearEnd() != YearEnd(5)

tf = Minute()
tf = TimeFrames.Minute()
@test tf.period.value == 1

tf = Minute(15)
tf = TimeFrames.Minute(15)
@test tf.period.value == 15

tf1 = Minute(15)
tf2 = Minute(15)
tf1 = TimeFrames.Minute(15)
tf2 = TimeFrames.Minute(15)
@test tf1 == tf2

tf1 = Minute(15)
tf2 = Minute(30)
tf1 = TimeFrames.Minute(15)
tf2 = TimeFrames.Minute(30)
@test tf1 != tf2
end

@testset "to string" begin
tf = Minute()
tf = TimeFrames.Minute()
@test String(tf) == "T"

tf = Minute(15)
tf = TimeFrames.Minute(15)
@test String(tf) == "15T"
end

Expand All @@ -92,20 +93,20 @@ end
@testset "simple parse" begin
tf = TimeFrame("15T")
@test tf.period.value == 15
@test typeof(tf) == Minute
@test typeof(tf) == TimeFrames.Minute

tf = TimeFrame("T")
@test tf.period.value == 1
@test typeof(tf) == Minute
@test typeof(tf) == TimeFrames.Minute

tf = TimeFrame("15Min")
@test tf.period.value == 15
@test String(tf) == "15T"
@test typeof(tf) == Minute
@test typeof(tf) == TimeFrames.Minute

tf = TimeFrame("5H")
@test tf.period.value == 5
@test typeof(tf) == Hour
@test typeof(tf) == TimeFrames.Hour
end

@testset "NoTimeFrame parse" begin
Expand Down Expand Up @@ -183,24 +184,24 @@ end
#@test apply(tf, dt) == DateTime(2016, 7, 1, 0, 0, 0, 0)
@test apply(tf, dt) == Date(2016, 7, 1)

tf = Week()
tf = TimeFrames.Week()
#@test apply(tf, dt) == DateTime(2016, 7, 18, 0, 0, 0, 0)
@test apply(tf, dt) == Date(2016, 7, 18)

tf = Day()
tf = TimeFrames.Day()
#@test apply(tf, dt) == DateTime(2016, 7, 20, 0, 0, 0, 0)
@test apply(tf, dt) == Date(2016, 7, 20)

tf = Hour()
tf = TimeFrames.Hour()
@test apply(tf, dt) == DateTime(2016, 7, 20, 13, 0, 0, 0)

tf = Minute()
tf = TimeFrames.Minute()
@test apply(tf, dt) == DateTime(2016, 7, 20, 13, 24, 0, 0)

tf = Second()
tf = TimeFrames.Second()
@test apply(tf, dt) == DateTime(2016, 7, 20, 13, 24, 35, 0)

tf = Millisecond()
tf = TimeFrames.Millisecond()
@test apply(tf, dt) == DateTime(2016, 7, 20, 13, 24, 35, 245)

tf = YearBegin(10)
Expand All @@ -210,7 +211,7 @@ end
@test apply(tf, d) == DateTime(2019, 12, 31)
@test apply(tf, dt) == DateTime(2020, 1, 1) - period_step

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

Expand Down

0 comments on commit 4b6cfa8

Please sign in to comment.