SimplePeriods.jl 1.58 KB
Newer Older
MichelJuillard's avatar
MichelJuillard committed
1
@enum Frequency Year Semester Quarter Month Week Business Day Undated
Michel Juillard's avatar
Michel Juillard committed
2

Michel Juillard's avatar
Michel Juillard committed
3
struct Period <: AbstractPeriod
Michel Juillard's avatar
Michel Juillard committed
4
5
6
7
    ordinal::Int64
    frequency::Frequency
end

Michel Juillard's avatar
Michel Juillard committed
8
function Period(arg1::Integer, frequency::Frequency)
Michel Juillard's avatar
Michel Juillard committed
9
    if frequency == Year
Stéphane Adjemian's avatar
Stéphane Adjemian committed
10
        ordinal = arg1 - 1970
Michel Juillard's avatar
Michel Juillard committed
11
    elseif frequency == Undated
opentable fetcher's avatar
opentable fetcher committed
12
        ordinal = arg1
Michel Juillard's avatar
Michel Juillard committed
13
14
15
16
17
18
19
20
21
    else
        error("Frequency $frequency needs more than one argument")
    end
    Period(ordinal, frequency)
end

function Period(arg1::Integer, arg2::Integer, frequency::Frequency)
    if frequency == Year
        ordinal = arg1 - 1970
Michel Juillard's avatar
Michel Juillard committed
22
    elseif frequency == Semester
Michel Juillard's avatar
Michel Juillard committed
23
24
        @assert(arg2 in 1:2)
        ordinal = 2*(arg1 - 1970) + arg2 - 1
Michel Juillard's avatar
Michel Juillard committed
25
    elseif frequency == Quarter
Michel Juillard's avatar
Michel Juillard committed
26
27
        @assert(arg2 in 1:4)
        ordinal = 4*(arg1 - 1970) + arg2 - 1
Michel Juillard's avatar
Michel Juillard committed
28
29
    elseif frequency == Month
        @assert(subperiod in 1:12)
Michel Juillard's avatar
Michel Juillard committed
30
        ordinal = 12*(arg1 - 1970) + arg2 - 1
Michel Juillard's avatar
Michel Juillard committed
31
    elseif frequency == Week
Michel Juillard's avatar
Michel Juillard committed
32
33
34
        ordinal = week1970(arg1, arg2)
    elseif frequency == Undated
        ordinal = arg1
Michel Juillard's avatar
Michel Juillard committed
35
    end
Michel Juillard's avatar
Michel Juillard committed
36
    Period(ordinal, frequency)
Michel Juillard's avatar
Michel Juillard committed
37
end
Michel Juillard's avatar
Michel Juillard committed
38

opentable fetcher's avatar
opentable fetcher committed
39
function Period(arg1::Integer, arg2::Integer, arg3::Integer, frequency::Frequency)
Michel Juillard's avatar
Michel Juillard committed
40
41
42
43
44
45
46
47
48
    if frequency == Day
        return Period(arg1, arg2, arg3)
    else
        return Period(arg1, arg2, frequency)
    end
end

Period(year::Integer, month::Integer, day::Integer) = (Date(year, month, day).value, Day)
Period(d::Date) = (d.value, Day)
Michel Juillard's avatar
Michel Juillard committed
49
50
51
52
53
54
55
56

import Base.copy
copy(p::Period) = Period(p.ordinal, p.frequency)

import Base.:+
+(p1::Period, k::Integer) = Period(p1.ordinal + k, p1.frequency)
import Base.:-
-(p1::Period, k::Integer) = Period(p1.ordinal - k, p1.frequency)