1
1
"""
2
2
Unit test infrastructure for the Date class
3
3
4
+ Author
5
+ ------
4
6
Xylar Asay-Davis
5
- 11/02/2016
7
+
8
+ Last Modified
9
+ -------------
10
+ 02/09/2017
6
11
"""
7
12
8
13
import pytest
11
16
import MpasRelativeDelta
12
17
from mpas_analysis .test import TestCase
13
18
from mpas_analysis .shared .timekeeping .utility import stringToDatetime , \
14
- stringToRelativedelta , clampToNumpyDatetime64
19
+ stringToRelativeDelta , clampToNumpyDatetime64
15
20
16
21
17
22
class TestTimekeeping (TestCase ):
@@ -29,113 +34,100 @@ def test_timekeeping(self):
29
34
# YYYY-MM-DD
30
35
# SSSSS
31
36
32
- # test datetime.datetime
33
- # YYYY-MM-DD_hh:mm:ss
34
- date1 = stringToDatetime ('0001-01-01_00:00:00' )
35
- date2 = datetime .datetime (year = 1 , month = 1 , day = 1 , hour = 0 , minute = 0 ,
36
- second = 0 )
37
- self .assertEqual (date1 , date2 )
38
-
39
- # test relativedelta
40
- # YYYY-MM-DD_hh:mm:ss
41
- # gregorian_noleap
42
- calendar = 'gregorian_noleap'
43
- date1 = stringToRelativedelta ('0001-00-00_00:00:00' ,
44
- calendar = calendar )
45
- date2 = MpasRelativeDelta (years = 1 , months = 0 , days = 0 , hours = 0 ,
46
- minutes = 0 , seconds = 0 , calendar = calendar )
47
- self .assertEqual (date1 , date2 )
48
-
49
- # gregorian
50
- calendar = 'gregorian'
51
- date1 = stringToRelativedelta ('0001-00-00_00:00:00' ,
52
- calendar = calendar )
53
- date2 = MpasRelativeDelta (years = 1 , months = 0 , days = 0 , hours = 0 ,
54
- minutes = 0 , seconds = 0 , calendar = calendar )
55
- self .assertEqual (date1 , date2 )
56
-
57
- # YYYY-MM-DD_hh.mm.ss
58
- date1 = stringToDatetime ('0001-01-01_00.00.00' )
59
- date2 = datetime .datetime (year = 1 , month = 1 , day = 1 , hour = 0 , minute = 0 ,
60
- second = 0 )
61
- self .assertEqual (date1 , date2 )
62
-
63
- # YYYY-MM-DD_SSSSS
64
- date1 = stringToDatetime ('0001-01-01_00002' )
65
- date2 = datetime .datetime (year = 1 , month = 1 , day = 1 , hour = 0 , minute = 0 ,
66
- second = 2 )
67
- self .assertEqual (date1 , date2 )
68
-
69
- # DDD_hh:mm:ss
70
- calendar = 'gregorian_noleap'
71
- date1 = stringToRelativedelta ('0001_00:00:01' ,
72
- calendar = calendar )
73
- date2 = MpasRelativeDelta (years = 0 , months = 0 , days = 1 , hours = 0 ,
74
- minutes = 0 , seconds = 1 , calendar = calendar )
75
- self .assertEqual (date1 , date2 )
76
-
77
- # DDD_hh.mm.ss
78
- date1 = stringToRelativedelta ('0002_01.00.01' ,
79
- calendar = calendar )
80
- date2 = MpasRelativeDelta (years = 0 , months = 0 , days = 2 , hours = 1 ,
81
- minutes = 0 , seconds = 1 , calendar = calendar )
82
- self .assertEqual (date1 , date2 )
83
-
84
- # DDD_SSSSS
85
- date1 = stringToRelativedelta ('0002_00003' ,
86
- calendar = calendar )
87
- date2 = MpasRelativeDelta (years = 0 , months = 0 , days = 2 , hours = 0 ,
88
- minutes = 0 , seconds = 3 , calendar = calendar )
89
- self .assertEqual (date1 , date2 )
90
-
91
- # hh:mm:ss
92
- date1 = stringToDatetime ('00:00:01' )
93
- date2 = datetime .datetime (year = 1 , month = 1 , day = 1 , hour = 0 , minute = 0 ,
94
- second = 1 )
95
- self .assertEqual (date1 , date2 )
96
-
97
- # hh.mm.ss
98
- calendar = 'gregorian'
99
- date1 = stringToRelativedelta ('00.00.01' ,
100
- calendar = calendar )
101
- date2 = MpasRelativeDelta (years = 0 , months = 0 , days = 0 , hours = 0 ,
102
- minutes = 0 , seconds = 1 , calendar = calendar )
103
- self .assertEqual (date1 , date2 )
104
-
105
- # YYYY-MM-DD
106
- date1 = stringToDatetime ('0001-01-01' )
107
- date2 = datetime .datetime (year = 1 , month = 1 , day = 1 , hour = 0 , minute = 0 ,
108
- second = 0 )
109
- self .assertEqual (date1 , date2 )
110
-
111
- # SSSSS
112
- date1 = stringToRelativedelta ('00005' ,
113
- calendar = calendar )
114
- date2 = MpasRelativeDelta (years = 0 , months = 0 , days = 0 , hours = 0 ,
115
- minutes = 0 , seconds = 5 , calendar = calendar )
116
- self .assertEqual (date1 , date2 )
117
-
118
- date1 = stringToDatetime ('1996-01-15' )
119
- date2 = stringToRelativedelta ('0005-00-00' ,
120
- calendar = 'gregorian' )
121
- diff = date1 - date2
122
- self .assertEqual (diff , stringToDatetime ('1991-01-15' ))
123
-
124
- date1 = stringToDatetime ('1996-01-15' )
125
- date2 = stringToRelativedelta ('0000-02-00' ,
126
- calendar = 'gregorian' )
127
- diff = date1 - date2
128
- self .assertEqual (diff , stringToDatetime ('1995-11-15' ))
129
-
130
- date1 = stringToDatetime ('1996-01-15' )
131
- date2 = stringToRelativedelta ('0000-00-20' ,
132
- calendar = 'gregorian' )
133
- diff = date1 - date2
134
- self .assertEqual (diff , stringToDatetime ('1995-12-26' ))
37
+ for calendar in ['gregorian' , 'gregorian_noleap' ]:
38
+ # test datetime.datetime
39
+ # YYYY-MM-DD_hh:mm:ss
40
+ date1 = stringToDatetime ('0001-01-01_00:00:00' )
41
+ date2 = datetime .datetime (year = 1 , month = 1 , day = 1 , hour = 0 , minute = 0 ,
42
+ second = 0 )
43
+ self .assertEqual (date1 , date2 )
44
+
45
+ delta1 = stringToRelativeDelta ('0001-00-00_00:00:00' ,
46
+ calendar = calendar )
47
+ delta2 = MpasRelativeDelta (years = 1 , months = 0 , days = 0 , hours = 0 ,
48
+ minutes = 0 , seconds = 0 , calendar = calendar )
49
+ self .assertEqual (delta1 , delta2 )
50
+
51
+ # YYYY-MM-DD_hh.mm.ss
52
+ date1 = stringToDatetime ('0001-01-01_00.00.00' )
53
+ date2 = datetime .datetime (year = 1 , month = 1 , day = 1 , hour = 0 , minute = 0 ,
54
+ second = 0 )
55
+ self .assertEqual (date1 , date2 )
56
+
57
+ # YYYY-MM-DD_SSSSS
58
+ date1 = stringToDatetime ('0001-01-01_00002' )
59
+ date2 = datetime .datetime (year = 1 , month = 1 , day = 1 , hour = 0 , minute = 0 ,
60
+ second = 2 )
61
+ self .assertEqual (date1 , date2 )
62
+
63
+ # DDD_hh:mm:ss
64
+ delta1 = stringToRelativeDelta ('0001_00:00:01' ,
65
+ calendar = calendar )
66
+ delta2 = MpasRelativeDelta (years = 0 , months = 0 , days = 1 , hours = 0 ,
67
+ minutes = 0 , seconds = 1 , calendar = calendar )
68
+ self .assertEqual (delta1 , delta2 )
69
+
70
+ # DDD_hh.mm.ss
71
+ delta1 = stringToRelativeDelta ('0002_01.00.01' ,
72
+ calendar = calendar )
73
+ delta2 = MpasRelativeDelta (years = 0 , months = 0 , days = 2 , hours = 1 ,
74
+ minutes = 0 , seconds = 1 , calendar = calendar )
75
+ self .assertEqual (delta1 , delta2 )
76
+
77
+ # DDD_SSSSS
78
+ delta1 = stringToRelativeDelta ('0002_00003' ,
79
+ calendar = calendar )
80
+ delta2 = MpasRelativeDelta (years = 0 , months = 0 , days = 2 , hours = 0 ,
81
+ minutes = 0 , seconds = 3 , calendar = calendar )
82
+ self .assertEqual (delta1 , delta2 )
83
+
84
+ # hh:mm:ss
85
+ date1 = stringToDatetime ('00:00:01' )
86
+ date2 = datetime .datetime (year = 1 , month = 1 , day = 1 , hour = 0 , minute = 0 ,
87
+ second = 1 )
88
+ self .assertEqual (date1 , date2 )
89
+
90
+ # hh.mm.ss
91
+ delta1 = stringToRelativeDelta ('00.00.01' ,
92
+ calendar = calendar )
93
+ delta2 = MpasRelativeDelta (years = 0 , months = 0 , days = 0 , hours = 0 ,
94
+ minutes = 0 , seconds = 1 , calendar = calendar )
95
+ self .assertEqual (delta1 , delta2 )
96
+
97
+ # YYYY-MM-DD
98
+ date1 = stringToDatetime ('0001-01-01' )
99
+ date2 = datetime .datetime (year = 1 , month = 1 , day = 1 , hour = 0 , minute = 0 ,
100
+ second = 0 )
101
+ self .assertEqual (date1 , date2 )
102
+
103
+ # SSSSS
104
+ delta1 = stringToRelativeDelta ('00005' ,
105
+ calendar = calendar )
106
+ delta2 = MpasRelativeDelta (years = 0 , months = 0 , days = 0 , hours = 0 ,
107
+ minutes = 0 , seconds = 5 , calendar = calendar )
108
+ self .assertEqual (delta1 , delta2 )
109
+
110
+ date1 = stringToDatetime ('1996-01-15' )
111
+ delta = stringToRelativeDelta ('0005-00-00' ,
112
+ calendar = calendar )
113
+ date2 = date1 - delta
114
+ self .assertEqual (date2 , stringToDatetime ('1991-01-15' ))
115
+
116
+ date1 = stringToDatetime ('1996-01-15' )
117
+ delta = stringToRelativeDelta ('0000-02-00' ,
118
+ calendar = calendar )
119
+ date2 = date1 - delta
120
+ self .assertEqual (date2 , stringToDatetime ('1995-11-15' ))
121
+
122
+ date1 = stringToDatetime ('1996-01-15' )
123
+ delta = stringToRelativeDelta ('0000-00-20' ,
124
+ calendar = calendar )
125
+ date2 = date1 - delta
126
+ self .assertEqual (date2 , stringToDatetime ('1995-12-26' ))
135
127
136
128
# since pandas and xarray use the numpy type 'datetime[ns]`, which
137
- # has a limited range of dates, the date 0001-01-01 gets increased to
138
- # the minimum allowed year boundary, 1678-01-01 to avoid invalid
129
+ # has a limited range of dates, the date 0001-01-01 gets increased
130
+ # to the minimum allowed year boundary, 1678-01-01 to avoid invalid
139
131
# dates.
140
132
date1 = clampToNumpyDatetime64 (stringToDatetime ('0001-01-01' ),
141
133
yearOffset = 0 )
@@ -148,53 +140,92 @@ def test_timekeeping(self):
148
140
self .assertEqual (date1 , date2 )
149
141
150
142
# since pandas and xarray use the numpy type 'datetime[ns]`, which
151
- # has a limited range of dates, the date 9999-01-01 gets decreased to
152
- # the maximum allowed year boundary, 2262-01-01 to avoid invalid
143
+ # has a limited range of dates, the date 9999-01-01 gets decreased
144
+ # to the maximum allowed year boundary, 2262-01-01 to avoid invalid
153
145
# dates.
154
146
date1 = clampToNumpyDatetime64 (stringToDatetime ('9999-01-01' ),
155
147
yearOffset = 0 )
156
148
date2 = datetime .datetime (year = 2262 , month = 1 , day = 1 )
157
149
self .assertEqual (date1 , date2 )
158
150
159
- # test if the calendars behave as they should
160
- self .assertEqual (stringToDatetime ('2016-02-28' ) +
161
- stringToRelativedelta ('0000-00-01' ,
162
- calendar = 'gregorian' ),
163
- stringToDatetime ('2016-02-29' ))
164
-
165
- self .assertEqual (stringToDatetime ('2016-02-28' ) +
166
- stringToRelativedelta ('0000-00-01' ,
167
- calendar = 'gregorian_noleap' ),
168
- stringToDatetime ('2016-03-01' ))
169
-
170
- self .assertEqual (stringToDatetime ('2016-03-01' ) -
171
- stringToRelativedelta ('0000-00-01' ,
172
- calendar = 'gregorian' ),
173
- stringToDatetime ('2016-02-29' ))
174
-
175
- self .assertEqual (stringToDatetime ('2016-03-01' ) -
176
- stringToRelativedelta ('0000-00-01' ,
177
- calendar = 'gregorian_noleap' ),
178
- stringToDatetime ('2016-02-28' ))
179
-
180
- self .assertEqual (stringToDatetime ('2016-01-31' ) +
181
- stringToRelativedelta ('0000-01-00' ,
182
- calendar = 'gregorian' ),
183
- stringToDatetime ('2016-02-29' ))
184
-
185
- self .assertEqual (stringToDatetime ('2016-01-31' ) +
186
- stringToRelativedelta ('0000-01-00' ,
187
- calendar = 'gregorian_noleap' ),
188
- stringToDatetime ('2016-02-28' ))
189
-
190
- self .assertEqual (stringToDatetime ('2016-03-31' ) -
191
- stringToRelativedelta ('0000-01-00' ,
192
- calendar = 'gregorian' ),
193
- stringToDatetime ('2016-02-29' ))
194
-
195
- self .assertEqual (stringToDatetime ('2016-03-31' ) -
196
- stringToRelativedelta ('0000-01-00' ,
197
- calendar = 'gregorian_noleap' ),
198
- stringToDatetime ('2016-02-28' ))
151
+ def test_MpasRelativeDeltaOps (self ):
152
+ # test if the calendars behave as they should close to leap day
153
+ # also, test addition and subtraction of the form
154
+ # datetime.datetime +/- MpasRelativeDelta above
155
+ # both calendars with adding one day
156
+ for calendar , expected in zip (['gregorian' , 'gregorian_noleap' ],
157
+ ['2016-02-29' , '2016-03-01' ]):
158
+ self .assertEqual (stringToDatetime ('2016-02-28' ) +
159
+ stringToRelativeDelta ('0000-00-01' ,
160
+ calendar = calendar ),
161
+ stringToDatetime (expected ))
162
+
163
+ # both calendars with subtracting one day
164
+ for calendar , expected in zip (['gregorian' , 'gregorian_noleap' ],
165
+ ['2016-02-29' , '2016-02-28' ]):
166
+ self .assertEqual (stringToDatetime ('2016-03-01' ) -
167
+ stringToRelativeDelta ('0000-00-01' ,
168
+ calendar = calendar ),
169
+ stringToDatetime (expected ))
170
+
171
+ # both calendars with adding one month
172
+ for calendar , expected in zip (['gregorian' , 'gregorian_noleap' ],
173
+ ['2016-02-29' , '2016-02-28' ]):
174
+ self .assertEqual (stringToDatetime ('2016-01-31' ) +
175
+ stringToRelativeDelta ('0000-01-00' ,
176
+ calendar = calendar ),
177
+ stringToDatetime (expected ))
178
+
179
+ # both calendars with subtracting one month
180
+ for calendar , expected in zip (['gregorian' , 'gregorian_noleap' ],
181
+ ['2016-02-29' , '2016-02-28' ]):
182
+ self .assertEqual (stringToDatetime ('2016-03-31' ) -
183
+ stringToRelativeDelta ('0000-01-00' ,
184
+ calendar = calendar ),
185
+ stringToDatetime (expected ))
186
+
187
+ for calendar in ['gregorian' , 'gregorian_noleap' ]:
188
+
189
+ delta1 = stringToRelativeDelta ('0000-01-00' , calendar = calendar )
190
+ delta2 = stringToRelativeDelta ('0000-00-01' , calendar = calendar )
191
+ deltaSum = stringToRelativeDelta ('0000-01-01' , calendar = calendar )
192
+ # test MpasRelativeDelta + MpasRelativeDelta
193
+ self .assertEqual (delta1 + delta2 , deltaSum )
194
+ # test MpasRelativeDelta - MpasRelativeDelta
195
+ self .assertEqual (deltaSum - delta2 , delta1 )
196
+
197
+ # test MpasRelativeDelta(date1, date2)
198
+ date1 = stringToDatetime ('0002-02-02' )
199
+ date2 = stringToDatetime ('0001-01-01' )
200
+ delta = stringToRelativeDelta ('0001-01-01' , calendar = calendar )
201
+ self .assertEqual (MpasRelativeDelta (dt1 = date1 , dt2 = date2 ,
202
+ calendar = calendar ),
203
+ delta )
204
+
205
+ # test MpasRelativeDelta + datetime.datetime (an odd order but
206
+ # it's allowed...)
207
+ date1 = stringToDatetime ('0001-01-01' )
208
+ delta = stringToRelativeDelta ('0001-01-01' , calendar = calendar )
209
+ date2 = stringToDatetime ('0002-02-02' )
210
+ self .assertEqual (delta + date1 , date2 )
211
+
212
+ # test multiplication/division by scalars
213
+ delta1 = stringToRelativeDelta ('0001-01-01' , calendar = calendar )
214
+ delta2 = stringToRelativeDelta ('0002-02-02' , calendar = calendar )
215
+ self .assertEqual (2 * delta1 , delta2 )
216
+ self .assertEqual (delta2 / 2 , delta1 )
217
+
218
+
219
+ # make sure there's an error when we try to add MpasRelativeDeltas
220
+ # with different calendars
221
+ with self .assertRaisesRegexp (ValueError ,
222
+ 'MpasRelativeDelta objects can only be '
223
+ 'added if their calendars match.' ):
224
+ delta1 = stringToRelativeDelta ('0000-01-00' ,
225
+ calendar = 'gregorian' )
226
+ delta2 = stringToRelativeDelta ('0000-00-01' ,
227
+ calendar = 'gregorian_noleap' )
228
+ deltaSum = delta1 + delta2
229
+
199
230
200
231
# vim: foldmethod=marker ai ts=4 sts=4 et sw=4 ft=python
0 commit comments