forked from JeffreySarnoff/TimesDates.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperiods.jl
More file actions
170 lines (143 loc) · 4.04 KB
/
periods.jl
File metadata and controls
170 lines (143 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
for P in (:Year, :Month, :Week, :Day, :Hour, :Minute, :Second, :Millisecond)
@eval begin
@inline fastpart(p::$P) = Nanosecond(0)
@inline slowpart(p::$P) = p
@inline fastslow(p::$P) = Nanosecond(0), p
end
end
for P in (:Microsecond, :Nanosecond)
@eval begin
@inline fastpart(p::$P) = p
@inline slowpart(p::$P) = Millisecond(0)
@inline fastslow(p::$P) = p, Millisecond(0)
end
end
fastpart(cperiod::CompoundPeriod) = Nanosecond(cperiod) + Microsecond(cperiod)
slowpart(cperiod::CompoundPeriod) = cperiod - fastpart(cperiod)
@inline function fastslow(cperiod::CompoundPeriod)
fast_part = fastpart(cperiod)
slow_part = cperiod - fast_part
return fast_part, slow_part
end
@inline function fastslow(x)
tm = Time(x)
fast_time = fastpart(tm)
slow_time = tm - fast_time
return fast_time, slow_time
end
function CompoundPeriod(x::TimeDate)
tim, dat = Time(x), Date(x)
fast_time = fastpart(tim)
slow_time = slowpart(tim)
tm = CompoundPeriod(slow_time) + fast_time
dt = CompoundPeriod(dat)
return dt + tm
end
function CompoundPeriod(x::TimeDateZone)
tim, dat = Time(x), Date(x)
fast_time = fastpart(tim)
slow_time = slowpart(tim)
tm = CompoundPeriod(slow_time) + fast_time
dt = CompoundPeriod(dat)
return dt + tm
end
function (+)(x::TimeDate, y::Period)
tim, dat = Time(x), Date(x)
fasttime, slowtime = fastpart(tim), slowpart(tim)
fastperiod, slowperiod = fastslow(y)
fasttime, lessfast = fastslow(fasttime + fastperiod)
slowtime = slowtime + lessfast
datetime = dat + slowtime
timedate = TimeDate(datetime)
timedate = timedate + fasttime
return timedate
end
function (-)(x::TimeDate, y::Period)
return x + (-y)
end
(+)(y::Period, x::TimeDate) = x + y
function (+)(x::TimeDate, z::CompoundPeriod)
result = x
z = canonical(z)
for period in z
result += period
end
return result
end
function (-)(x::TimeDate, z::CompoundPeriod)
result = x
z = canonical(z)
for period in z
result -= period
end
return result
end
(+)(y::CompoundPeriod, x::TimeDate) = x + y
function (+)(x::TimeDateZone, y::Period)
fast_part, slow_part = fastslow(y)
zdt = ZonedDateTime(x)
fast_time = at_time(x) - at_time(zdt)
fast_time += fast_part
zdt = zdt + slow_part
tim, dat = timedate(zdt)
tim = tim + fast_time
inzone = in_zone(zdt)
atzone = at_zone(zdt)
TimeDateZone(tim, dat, inzone, atzone)
end
function (-)(x::TimeDateZone, y::Period)
return x + (-y)
end
(+)(y::Period, x::TimeDateZone) = x + y
function (+)(x::TimeDateZone, y::Period, idx::Int)
fast_part, slow_part = fastslow(y)
zdt = ZonedDateTime(x)
zdt = ZonedDateTime(DateTime(x), in_zone(zdt), idx)
fast_time = at_time(x) - at_time(zdt)
fast_time += fast_part
zdt = zdt + slow_part
tim, dat = timedate(zdt)
tim = tim + fast_time
inzone = in_zone(zdt)
atzone = at_zone(zdt)
TimeDateZone(tim, dat, inzone, atzone)
end
function (-)(x::TimeDateZone, y::Period, idx::Int)
return (+)(x, (-y), idx)
end
(+)(y::Period, x::TimeDateZone, idx::Int) = (+)(x, y, idx)
function (+)(x::TimeDateZone, y::CompoundPeriod)
result = x
for period in y
result += period
end
return result
end
function (-)(x::TimeDateZone, y::CompoundPeriod)
result = x
for period in y
result -= period
end
return result
end
(+)(y::CompoundPeriod, x::TimeDateZone) = x + y
for P in (:Hour, :Minute, :Second, :Millisecond, :Microsecond, :Nanosecond)
@eval begin
function (+)(dt::Date, period::$P)
if signbit(period)
return dt - abs(period)
end
return TimeDate(dt) + period
end
function (-)(dt::Date, period::$P)
if signbit(period)
return dt + abs(period)
end
return TimeDate(dt) - period
end
end
end
function canonical(x::TimeDate)
tm, dt = timedate(x)
return canonical(canonical(dt) + canonical(tm))
end