Skip to content

Commit

Permalink
Rename Timeframes
Browse files Browse the repository at this point in the history
Yearly=>YearEnd
YearlyStart=>YearBegin
...
Hourly=>Hour
...
  • Loading branch information
femtotrader committed Nov 18, 2016
1 parent 914bd95 commit a825687
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 82 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -22,7 +22,7 @@ julia> Pkg.add("TimeFrames")
julia> using TimeFrames

julia> tf = TimeFrame("5T")
TimeFrames.TimePeriodFrame{Base.Dates.Minute}(5 minutes,Begin::TimeFrames.Boundary = 1)
TimeFrames.Minute(5 minutes,Begin::TimeFrames.Boundary = 1)

julia> apply(tf, DateTime(2016, 9, 11, 20, 9))
2016-09-11T20:05:00
Expand Down
89 changes: 45 additions & 44 deletions src/TimeFrames.jl
Expand Up @@ -3,8 +3,9 @@ module TimeFrames
import Base: range

export TimeFrame, Boundary
export Monthly, Yearly, MonthlyStart, YearlyStart
export Millisecondly, Secondly, Minutely, Hourly, Daily, Weekly
export YearBegin, YearEnd
export MonthBegin, MonthEnd
export Millisecond, Second, Minute, Hour, Day, Week
export NoTimeFrame
export apply, range, shortcut
export Begin, End
Expand Down Expand Up @@ -54,74 +55,74 @@ function period_step(::Type{DateTime})
Dates.Millisecond(1)
end

immutable Millisecondly <: AbstractTimePeriodFrame
immutable Millisecond <: AbstractTimePeriodFrame
time_period::Dates.TimePeriod
boundary::Boundary
Millisecondly() = new(Dates.Millisecond(1), Begin)
Millisecondly(n::Integer) = new(Dates.Millisecond(n), Begin)
Millisecond() = new(Dates.Millisecond(1), Begin)
Millisecond(n::Integer) = new(Dates.Millisecond(n), Begin)
end

immutable Secondly <: AbstractTimePeriodFrame
immutable Second <: AbstractTimePeriodFrame
time_period::Dates.TimePeriod
boundary::Boundary
Secondly() = new(Dates.Second(1), Begin)
Secondly(n::Integer) = new(Dates.Second(n), Begin)
Second() = new(Dates.Second(1), Begin)
Second(n::Integer) = new(Dates.Second(n), Begin)
end

immutable Minutely <: AbstractTimePeriodFrame
immutable Minute <: AbstractTimePeriodFrame
time_period::Dates.TimePeriod
boundary::Boundary
Minutely() = new(Dates.Minute(1), Begin)
Minutely(n::Integer) = new(Dates.Minute(n), Begin)
Minute() = new(Dates.Minute(1), Begin)
Minute(n::Integer) = new(Dates.Minute(n), Begin)
end

immutable Hourly <: AbstractTimePeriodFrame
immutable Hour <: AbstractTimePeriodFrame
time_period::Dates.TimePeriod
boundary::Boundary
Hourly() = new(Dates.Hour(1), Begin)
Hourly(n::Integer) = new(Dates.Hour(n), Begin)
Hour() = new(Dates.Hour(1), Begin)
Hour(n::Integer) = new(Dates.Hour(n), Begin)
end

immutable Daily <: AbstractDatePeriodFrame
immutable Day <: AbstractDatePeriodFrame
time_period::Dates.DatePeriod
boundary::Boundary
Daily() = new(Dates.Day(1), Begin)
Daily(n::Integer) = new(Dates.Day(n), Begin)
Day() = new(Dates.Day(1), Begin)
Day(n::Integer) = new(Dates.Day(n), Begin)
end

immutable Weekly <: AbstractDatePeriodFrame
immutable Week <: AbstractDatePeriodFrame
time_period::Dates.DatePeriod
boundary::Boundary
Weekly() = new(Dates.Week(1), Begin)
Weekly(n::Integer) = new(Dates.Week(n), Begin)
Week() = new(Dates.Week(1), Begin)
Week(n::Integer) = new(Dates.Week(n), Begin)
end

immutable Monthly <: AbstractDatePeriodFrame
immutable MonthEnd <: AbstractDatePeriodFrame
time_period::Dates.DatePeriod
boundary::Boundary
Monthly() = new(Dates.Month(1), End)
Monthly(n::Integer) = new(Dates.Month(n), End)
MonthEnd() = new(Dates.Month(1), End)
MonthEnd(n::Integer) = new(Dates.Month(n), End)
end

immutable MonthlyStart <: AbstractDatePeriodFrame
immutable MonthBegin <: AbstractDatePeriodFrame
time_period::Dates.DatePeriod
boundary::Boundary
MonthlyStart() = new(Dates.Month(1), Begin)
MonthlyStart(n::Integer) = new(Dates.Month(n), Begin)
MonthBegin() = new(Dates.Month(1), Begin)
MonthBegin(n::Integer) = new(Dates.Month(n), Begin)
end

immutable Yearly <: AbstractDatePeriodFrame
immutable YearEnd <: AbstractDatePeriodFrame
time_period::Dates.DatePeriod
boundary::Boundary
Yearly() = new(Dates.Year(1), End)
Yearly(n::Integer) = new(Dates.Year(n), End)
YearEnd() = new(Dates.Year(1), End)
YearEnd(n::Integer) = new(Dates.Year(n), End)
end

immutable YearlyStart <: AbstractDatePeriodFrame
immutable YearBegin <: AbstractDatePeriodFrame
time_period::Dates.DatePeriod
boundary::Boundary
YearlyStart() = new(Dates.Year(1), Begin)
YearlyStart(n::Integer) = new(Dates.Year(n), Begin)
YearBegin() = new(Dates.Year(1), Begin)
YearBegin(n::Integer) = new(Dates.Year(n), Begin)
end

TimeFrame() = Secondly(0)
Expand All @@ -141,17 +142,17 @@ function dt_grouper(tf::AbstractPeriodFrame, t::Type)
end

_D_STR2TIMEFRAME = Dict(
"A"=>Yearly,
"AS"=>YearlyStart,
"M"=>Monthly,
"MS"=>MonthlyStart,
"W"=>Weekly,
"D"=>Daily,
"H"=>Hourly,
"T"=>Minutely,
"S"=>Secondly,
"L"=>Millisecondly,
#"U"=>Microsecondly,
"A"=>YearEnd,
"AS"=>YearBegin,
"M"=>MonthEnd,
"MS"=>MonthBegin,
"W"=>Week,
"D"=>Day,
"H"=>Hour,
"T"=>Minute,
"S"=>Second,
"L"=>Millisecond,
#"U"=>Microsecond,
""=>NoTimeFrame
)
# Reverse key/value
Expand All @@ -161,7 +162,7 @@ for (key, typ) in _D_STR2TIMEFRAME
end
# Additional shortcuts
_D_STR2TIMEFRAME_ADDITIONAL = Dict(
"MIN"=>Minutely,
"MIN"=>Minute,
)
for (key, value) in _D_STR2TIMEFRAME_ADDITIONAL
_D_STR2TIMEFRAME[key] = value
Expand Down
74 changes: 37 additions & 37 deletions test/test_timeframe.jl
Expand Up @@ -3,71 +3,71 @@ using TimeFrames: shortcut

using Base.Test

tf = Yearly()
tf = YearEnd()
@test tf.time_period.value == 1

@test Yearly() == Yearly()
@test Yearly() != Yearly(5)
@test YearEnd() == YearEnd()
@test YearEnd() != YearEnd(5)

tf = Minutely()
tf = Minute()
@test tf.time_period.value == 1

tf = Minutely(15)
tf = Minute(15)
@test tf.time_period.value == 15

tf1 = Minutely(15)
tf2 = Minutely(15)
tf1 = Minute(15)
tf2 = Minute(15)
@test tf1 == tf2

tf1 = Minutely(15)
tf2 = Minutely(30)
tf1 = Minute(15)
tf2 = Minute(30)
@test tf1 != tf2

tf = Minutely()
tf = Minute()
@test shortcut(tf) == "T"

tf = Minutely(15)
tf = Minute(15)
@test shortcut(tf) == "15T"

tf = TimeFrame("15T")
@test tf.time_period.value == 15
@test typeof(tf) == Minutely
@test typeof(tf) == Minute

tf = TimeFrame("T")
@test tf.time_period.value == 1
@test typeof(tf) == Minutely
@test typeof(tf) == Minute

tf = TimeFrame("15Min")
@test tf.time_period.value == 15
@test shortcut(tf) == "15T"
@test typeof(tf) == Minutely
@test typeof(tf) == Minute

tf = TimeFrame("5H")
@test tf.time_period.value == 5
@test typeof(tf) == Hourly
@test typeof(tf) == Hour

# Boundary
@test Yearly() != YearlyStart()
@test apply(Monthly(), Date(2010, 2, 20)) == Date(2010, 2, 28)
@test apply(Monthly(), DateTime(2010, 2, 20)) == DateTime(2010, 2, 28, 23, 59, 59, 999)
@test YearEnd() != YearBegin()
@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)

tf = TimeFrame("3A")
@test tf == Yearly(3)
@test tf == YearEnd(3)
@test tf.time_period == Dates.Year(3)
@test tf.boundary == End

tf = TimeFrame("3AS")
@test tf == YearlyStart(3)
@test tf == YearBegin(3)
@test tf.time_period == Dates.Year(3)
#@test tf.boundary == Begin # ToFix

tf = TimeFrame("3M")
@test tf == Monthly(3)
@test tf == MonthEnd(3)
@test tf.time_period == Dates.Month(3)
@test tf.boundary == End

tf = TimeFrame("3MS")
@test tf == MonthlyStart(3)
@test tf == MonthBegin(3)
@test tf.time_period == Dates.Month(3)
#@test tf.boundary == Begin # ToFix

Expand All @@ -84,48 +84,48 @@ tf = TimeFrame(Dates.Minute(15)) # TimePeriodFrame using TimePeriod
tf = TimeFrame(Dates.Day(1)) # TimePeriodFrame using DatePeriod
@test apply(tf, dt) == DateTime(2016, 7, 20, 0, 0, 0, 0)

tf = YearlyStart()
tf = YearBegin()
#@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

#tf = Yearly(boundary=End)
#tf = YearEnd(boundary=End)
#@test apply(tf, dt) == DateTime(2016, 1, 1, 0, 0, 0, 0)
#@test apply(tf, dt) == Date(2016, 1, 1)


tf = MonthlyStart()
tf = MonthBegin()
#@test apply(tf, dt) == DateTime(2016, 7, 1, 0, 0, 0, 0)
@test apply(tf, dt) == Date(2016, 7, 1)

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

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

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

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

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

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

tf = YearlyStart(10)
tf = YearBegin(10)
@test apply(tf, dt) == DateTime(2010, 1, 1, 0, 0, 0, 0)

tf = Yearly(10)
tf = YearEnd(10)
@test apply(tf, d) == DateTime(2019, 12, 31)
@test apply(tf, dt) == DateTime(2019, 12, 31, 23, 59, 59, 999)

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

# No timeframe
Expand All @@ -136,7 +136,7 @@ tf = TimeFrame("")
# range
dt1 = DateTime(2010, 1, 1, 20)
dt2 = DateTime(2010, 1, 14, 16)
for tf in [Dates.Day(1), Daily(1)] # Should works both with Period and TimeFrame
for tf in [Dates.Day(1), Day(1)] # Should works both with Period and TimeFrame
rng = range(dt1, tf, dt2)
@test rng[1] == DateTime(2010, 1, 1)
@test rng[end] == DateTime(2010, 1, 14)
Expand All @@ -147,7 +147,7 @@ for tf in [Dates.Day(1), Daily(1)] # Should works both with Period and TimeFram
end


for tf in [Dates.Day(1), Daily(1)] # Should works both with Period and TimeFrame
for tf in [Dates.Day(1), Day(1)] # Should works both with Period and TimeFrame
N = 5
rng = range(dt1, tf, N)
@test length(rng) == N
Expand All @@ -156,7 +156,7 @@ for tf in [Dates.Day(1), Daily(1)] # Should works both with Period and TimeFram
end


for tf in [Dates.Day(1), Daily(1)] # Should works both with Period and TimeFrame
for tf in [Dates.Day(1), Day(1)] # Should works both with Period and TimeFrame
N = 5
rng = range(tf, dt2, N)
@test length(rng) == N
Expand Down

0 comments on commit a825687

Please sign in to comment.