forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accessors.jl
195 lines (187 loc) · 6.49 KB
/
accessors.jl
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# This file is a part of Julia. License is MIT: http://julialang.org/license
# yearmonthday is the opposite of totaldays
# taking Rata Die Day # and returning proleptic Gregorian date
@test Dates.yearmonthday(-306) == (0,2,29)
@test Dates.yearmonth(-306) == (0,2)
@test Dates.monthday(-306) == (2,29)
@test Dates.yearmonthday(-305) == (0,3,1)
@test Dates.yearmonth(-305) == (0,3)
@test Dates.monthday(-305) == (3,1)
@test Dates.yearmonthday(-2) == (0,12,29)
@test Dates.yearmonth(-2) == (0,12)
@test Dates.monthday(-2) == (12,29)
@test Dates.yearmonthday(-1) == (0,12,30)
@test Dates.yearmonth(-1) == (0,12)
@test Dates.monthday(-1) == (12,30)
@test Dates.yearmonthday(0) == (0,12,31)
@test Dates.yearmonth(-0) == (0,12)
@test Dates.monthday(-0) == (12,31)
@test Dates.yearmonthday(1) == (1,1,1)
@test Dates.yearmonth(1) == (1,1)
@test Dates.monthday(1) == (1,1)
# year, month, and day return the indivial components
# of yearmonthday, avoiding additional calculations when possible
@test Dates.year(-1) == 0
@test Dates.month(-1) == 12
@test Dates.day(-1) == 30
@test Dates.year(0) == 0
@test Dates.month(0) == 12
@test Dates.day(0) == 31
@test Dates.year(1) == 1
@test Dates.month(1) == 1
@test Dates.day(1) == 1
@test Dates.yearmonthday(730120) == (2000,1,1)
@test Dates.year(730120) == 2000
@test Dates.month(730120) == 1
@test Dates.day(730120) == 1
# Test totaldays and yearmonthday from January 1st of "from" to December 31st of "to"
# test_dates(-10000,10000) takes about 15 seconds
# test_dates(year(typemin(Date)),year(typemax(Date))) is full range
# and would take.......a really long time
function test_dates1(from,to)
y = m = d = 0
test_day = Dates.totaldays(from,1,1)
for y in from:to
for m = 1:12
for d = 1:Dates.daysinmonth(y,m)
days = Dates.totaldays(y,m,d)
@test days == test_day
@test (y,m,d) == Dates.yearmonthday(days)
test_day += 1
end
end
end
end
test_dates1(0,2100)
# Test year, month, day, hour, minute
function test_dates1()
y = m = d = h = mi = 0
for m = 1:12
for d = 1:Dates.daysinmonth(y,m)
for h = 0:23
for mi = 0:59
dt = Dates.DateTime(y,m,d,h,mi)
@test y == Dates.year(dt)
@test m == Dates.month(dt)
@test d == Dates.day(dt)
@test d == Dates.dayofmonth(dt)
@test h == Dates.hour(dt)
@test mi == Dates.minute(dt)
@test (m,d) == Dates.monthday(dt)
#@test s == Dates.second(dt)
#@test ms == Dates.millisecond(dt)
end
end
end
end
end
test_dates1()
# Test second, millisecond
function test_dates2()
y = m = d = h = mi = s = ms = 0
for y in [-2013,-1,0,1,2013]
for m in [1,6,12]
for d in [1,15,Dates.daysinmonth(y,m)]
for h in [0,12,23]
for s = 0:59
for ms in [0,1,500,999]
dt = Dates.DateTime(y,m,d,h,mi,s,ms)
@test y == Dates.year(dt)
@test m == Dates.month(dt)
@test d == Dates.day(dt)
@test h == Dates.hour(dt)
@test s == Dates.second(dt)
@test ms == Dates.millisecond(dt)
end
end
end
end
end
end
end
test_dates2()
function test_dates2(from,to)
y = m = d = 0
for y in from:to
for m = 1:12
for d = 1:Dates.daysinmonth(y,m)
dt = Dates.Date(y,m,d)
@test y == Dates.year(dt)
@test m == Dates.month(dt)
@test d == Dates.day(dt)
end
end
end
end
test_dates2(0,2100)
# week function
# Tests from https://en.wikipedia.org/wiki/ISO_week_date
@test Dates.week(Dates.Date(2005,1,1)) == 53
@test Dates.week(Dates.Date(2005,1,2)) == 53
@test Dates.week(Dates.Date(2005,12,31)) == 52
@test Dates.week(Dates.Date(2007,1,1)) == 1
@test Dates.week(Dates.Date(2007,12,30)) == 52
@test Dates.week(Dates.Date(2007,12,31)) == 1
@test Dates.week(Dates.Date(2008,1,1)) == 1
@test Dates.week(Dates.Date(2008,12,28)) == 52
@test Dates.week(Dates.Date(2008,12,29)) == 1
@test Dates.week(Dates.Date(2008,12,30)) == 1
@test Dates.week(Dates.Date(2008,12,31)) == 1
@test Dates.week(Dates.Date(2009,1,1)) == 1
@test Dates.week(Dates.Date(2009,12,31)) == 53
@test Dates.week(Dates.Date(2010,1,1)) == 53
@test Dates.week(Dates.Date(2010,1,2)) == 53
@test Dates.week(Dates.Date(2010,1,2)) == 53
# Tests from http://www.epochconverter.com/date-and-time/weeknumbers-by-year.php?year=1999
dt = Dates.DateTime(1999,12,27)
dt1 = Dates.Date(1999,12,27)
check = (52,52,52,52,52,52,52,1,1,1,1,1,1,1,2,2,2,2,2,2,2)
for i = 1:21
@test Dates.week(dt) == check[i]
@test Dates.week(dt1) == check[i]
dt = dt + Dates.Day(1)
dt1 = dt1 + Dates.Day(1)
end
# Tests from http://www.epochconverter.com/date-and-time/weeknumbers-by-year.php?year=2000
dt = Dates.DateTime(2000,12,25)
dt1 = Dates.Date(2000,12,25)
for i = 1:21
@test Dates.week(dt) == check[i]
@test Dates.week(dt1) == check[i]
dt = dt + Dates.Day(1)
dt1 = dt1 + Dates.Day(1)
end
# Test from http://www.epochconverter.com/date-and-time/weeknumbers-by-year.php?year=2030
dt = Dates.DateTime(2030,12,23)
dt1 = Dates.Date(2030,12,23)
for i = 1:21
@test Dates.week(dt) == check[i]
@test Dates.week(dt1) == check[i]
dt = dt + Dates.Day(1)
dt1 = dt1 + Dates.Day(1)
end
# Tests from http://www.epochconverter.com/date-and-time/weeknumbers-by-year.php?year=2004
dt = Dates.DateTime(2004,12,20)
dt1 = Dates.Date(2004,12,20)
check = (52,52,52,52,52,52,52,53,53,53,53,53,53,53,1,1,1,1,1,1,1)
for i = 1:21
@test Dates.week(dt) == check[i]
@test Dates.week(dt1) == check[i]
dt = dt + Dates.Day(1)
dt1 = dt1 + Dates.Day(1)
end
# Vectorized accessors
a = Dates.Date(2014,1,1)
dr = [a,a,a,a,a,a,a,a,a,a]
@test Dates.year(dr) == repmat([2014],10)
@test Dates.month(dr) == repmat([1],10)
@test Dates.day(dr) == repmat([1],10)
a = Dates.DateTime(2014,1,1)
dr = [a,a,a,a,a,a,a,a,a,a]
@test Dates.year(dr) == repmat([2014],10)
@test Dates.month(dr) == repmat([1],10)
@test Dates.day(dr) == repmat([1],10)
@test Dates.hour(dr) == repmat([0],10)
@test Dates.minute(dr) == repmat([0],10)
@test Dates.second(dr) == repmat([0],10)
@test Dates.millisecond(dr) == repmat([0],10)