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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
from __future__ import annotations
import pytest
import pendulum
from pendulum.exceptions import PendulumException
from tests.conftest import assert_datetime
def test_start_of_week():
d = pendulum.datetime(1980, 8, 7, 12, 11, 9).start_of("week")
assert_datetime(d, 1980, 8, 4, 0, 0, 0)
def test_start_of_week_from_week_start():
d = pendulum.datetime(1980, 8, 4).start_of("week")
assert_datetime(d, 1980, 8, 4, 0, 0, 0)
def test_start_of_week_crossing_year_boundary():
d = pendulum.datetime(2014, 1, 1).start_of("week")
assert_datetime(d, 2013, 12, 30, 0, 0, 0)
def test_end_of_week():
d = pendulum.datetime(1980, 8, 7, 12, 11, 9).end_of("week")
assert_datetime(d, 1980, 8, 10, 23, 59, 59)
def test_end_of_week_from_week_end():
d = pendulum.datetime(1980, 8, 10).end_of("week")
assert_datetime(d, 1980, 8, 10, 23, 59, 59)
def test_end_of_week_crossing_year_boundary():
d = pendulum.datetime(2013, 12, 31).end_of("week")
assert_datetime(d, 2014, 1, 5, 23, 59, 59)
def test_next():
d = pendulum.datetime(1975, 5, 21).next()
assert_datetime(d, 1975, 5, 28, 0, 0, 0)
def test_next_monday():
d = pendulum.datetime(1975, 5, 21).next(pendulum.MONDAY)
assert_datetime(d, 1975, 5, 26, 0, 0, 0)
def test_next_saturday():
d = pendulum.datetime(1975, 5, 21).next(5)
assert_datetime(d, 1975, 5, 24, 0, 0, 0)
def test_next_keep_time():
d = pendulum.datetime(1975, 5, 21, 12).next()
assert_datetime(d, 1975, 5, 28, 0, 0, 0)
d = pendulum.datetime(1975, 5, 21, 12).next(keep_time=True)
assert_datetime(d, 1975, 5, 28, 12, 0, 0)
def test_next_invalid():
dt = pendulum.datetime(1975, 5, 21, 12)
with pytest.raises(ValueError):
dt.next(7)
def test_previous():
d = pendulum.datetime(1975, 5, 21).previous()
assert_datetime(d, 1975, 5, 14, 0, 0, 0)
def test_previous_monday():
d = pendulum.datetime(1975, 5, 21).previous(pendulum.MONDAY)
assert_datetime(d, 1975, 5, 19, 0, 0, 0)
def test_previous_saturday():
d = pendulum.datetime(1975, 5, 21).previous(5)
assert_datetime(d, 1975, 5, 17, 0, 0, 0)
def test_previous_keep_time():
d = pendulum.datetime(1975, 5, 21, 12).previous()
assert_datetime(d, 1975, 5, 14, 0, 0, 0)
d = pendulum.datetime(1975, 5, 21, 12).previous(keep_time=True)
assert_datetime(d, 1975, 5, 14, 12, 0, 0)
def test_previous_invalid():
dt = pendulum.datetime(1975, 5, 21, 12)
with pytest.raises(ValueError):
dt.previous(7)
def test_first_day_of_month():
d = pendulum.datetime(1975, 11, 21).first_of("month")
assert_datetime(d, 1975, 11, 1, 0, 0, 0)
def test_first_wednesday_of_month():
d = pendulum.datetime(1975, 11, 21).first_of("month", pendulum.WEDNESDAY)
assert_datetime(d, 1975, 11, 5, 0, 0, 0)
def test_first_friday_of_month():
d = pendulum.datetime(1975, 11, 21).first_of("month", 4)
assert_datetime(d, 1975, 11, 7, 0, 0, 0)
def test_last_day_of_month():
d = pendulum.datetime(1975, 12, 5).last_of("month")
assert_datetime(d, 1975, 12, 31, 0, 0, 0)
def test_last_tuesday_of_month():
d = pendulum.datetime(1975, 12, 1).last_of("month", pendulum.TUESDAY)
assert_datetime(d, 1975, 12, 30, 0, 0, 0)
def test_last_friday_of_month():
d = pendulum.datetime(1975, 12, 5).last_of("month", 4)
assert_datetime(d, 1975, 12, 26, 0, 0, 0)
def test_nth_of_month_outside_scope():
d = pendulum.datetime(1975, 6, 5)
with pytest.raises(PendulumException):
d.nth_of("month", 6, pendulum.MONDAY)
def test_nth_of_month_outside_year():
d = pendulum.datetime(1975, 12, 5)
with pytest.raises(PendulumException):
d.nth_of("month", 55, pendulum.MONDAY)
def test_nth_of_month_first():
d = pendulum.datetime(1975, 12, 5).nth_of("month", 1, pendulum.MONDAY)
assert_datetime(d, 1975, 12, 1, 0, 0, 0)
def test_2nd_monday_of_month():
d = pendulum.datetime(1975, 12, 5).nth_of("month", 2, pendulum.MONDAY)
assert_datetime(d, 1975, 12, 8, 0, 0, 0)
def test_3rd_wednesday_of_month():
d = pendulum.datetime(1975, 12, 5).nth_of("month", 3, 2)
assert_datetime(d, 1975, 12, 17, 0, 0, 0)
def test_first_day_of_quarter():
d = pendulum.datetime(1975, 11, 21).first_of("quarter")
assert_datetime(d, 1975, 10, 1, 0, 0, 0)
def test_first_wednesday_of_quarter():
d = pendulum.datetime(1975, 11, 21).first_of("quarter", pendulum.WEDNESDAY)
assert_datetime(d, 1975, 10, 1, 0, 0, 0)
def test_first_friday_of_quarter():
d = pendulum.datetime(1975, 11, 21).first_of("quarter", 4)
assert_datetime(d, 1975, 10, 3, 0, 0, 0)
def test_first_of_quarter_from_a_day_that_will_not_exist_in_the_first_month():
d = pendulum.datetime(2014, 5, 31).first_of("quarter")
assert_datetime(d, 2014, 4, 1, 0, 0, 0)
def test_last_day_of_quarter():
d = pendulum.datetime(1975, 8, 5).last_of("quarter")
assert_datetime(d, 1975, 9, 30, 0, 0, 0)
def test_last_tuesday_of_quarter():
d = pendulum.datetime(1975, 8, 5).last_of("quarter", pendulum.TUESDAY)
assert_datetime(d, 1975, 9, 30, 0, 0, 0)
def test_last_friday_of_quarter():
d = pendulum.datetime(1975, 8, 5).last_of("quarter", pendulum.FRIDAY)
assert_datetime(d, 1975, 9, 26, 0, 0, 0)
def test_last_day_of_quarter_that_will_not_exist_in_the_last_month():
d = pendulum.datetime(2014, 5, 31).last_of("quarter")
assert_datetime(d, 2014, 6, 30, 0, 0, 0)
def test_nth_of_quarter_outside_scope():
d = pendulum.datetime(1975, 1, 5)
with pytest.raises(PendulumException):
d.nth_of("quarter", 20, pendulum.MONDAY)
def test_nth_of_quarter_outside_year():
d = pendulum.datetime(1975, 1, 5)
with pytest.raises(PendulumException):
d.nth_of("quarter", 55, pendulum.MONDAY)
def test_nth_of_quarter_first():
d = pendulum.datetime(1975, 12, 5).nth_of("quarter", 1, pendulum.MONDAY)
assert_datetime(d, 1975, 10, 6, 0, 0, 0)
def test_nth_of_quarter_from_a_day_that_will_not_exist_in_the_first_month():
d = pendulum.datetime(2014, 5, 31).nth_of("quarter", 2, pendulum.MONDAY)
assert_datetime(d, 2014, 4, 14, 0, 0, 0)
def test_2nd_monday_of_quarter():
d = pendulum.datetime(1975, 8, 5).nth_of("quarter", 2, pendulum.MONDAY)
assert_datetime(d, 1975, 7, 14, 0, 0, 0)
def test_3rd_wednesday_of_quarter():
d = pendulum.datetime(1975, 8, 5).nth_of("quarter", 3, 2)
assert_datetime(d, 1975, 7, 16, 0, 0, 0)
def test_first_day_of_year():
d = pendulum.datetime(1975, 11, 21).first_of("year")
assert_datetime(d, 1975, 1, 1, 0, 0, 0)
def test_first_wednesday_of_year():
d = pendulum.datetime(1975, 11, 21).first_of("year", pendulum.WEDNESDAY)
assert_datetime(d, 1975, 1, 1, 0, 0, 0)
def test_first_friday_of_year():
d = pendulum.datetime(1975, 11, 21).first_of("year", 4)
assert_datetime(d, 1975, 1, 3, 0, 0, 0)
def test_last_day_of_year():
d = pendulum.datetime(1975, 8, 5).last_of("year")
assert_datetime(d, 1975, 12, 31, 0, 0, 0)
def test_last_tuesday_of_year():
d = pendulum.datetime(1975, 8, 5).last_of("year", pendulum.TUESDAY)
assert_datetime(d, 1975, 12, 30, 0, 0, 0)
def test_last_friday_of_year():
d = pendulum.datetime(1975, 8, 5).last_of("year", 4)
assert_datetime(d, 1975, 12, 26, 0, 0, 0)
def test_nth_of_year_outside_scope():
d = pendulum.datetime(1975, 1, 5)
with pytest.raises(PendulumException):
d.nth_of("year", 55, pendulum.MONDAY)
def test_nth_of_year_first():
d = pendulum.datetime(1975, 12, 5).nth_of("year", 1, pendulum.MONDAY)
assert_datetime(d, 1975, 1, 6, 0, 0, 0)
def test_2nd_monday_of_year():
d = pendulum.datetime(1975, 8, 5).nth_of("year", 2, pendulum.MONDAY)
assert_datetime(d, 1975, 1, 13, 0, 0, 0)
def test_2rd_wednesday_of_year():
d = pendulum.datetime(1975, 8, 5).nth_of("year", 3, pendulum.WEDNESDAY)
assert_datetime(d, 1975, 1, 15, 0, 0, 0)
def test_7th_thursday_of_year():
d = pendulum.datetime(1975, 8, 31).nth_of("year", 7, pendulum.THURSDAY)
assert_datetime(d, 1975, 2, 13, 0, 0, 0)
def test_first_of_invalid_unit():
d = pendulum.datetime(1975, 8, 5)
with pytest.raises(ValueError):
d.first_of("invalid")
def test_last_of_invalid_unit():
d = pendulum.datetime(1975, 8, 5)
with pytest.raises(ValueError):
d.last_of("invalid")
def test_nth_of_invalid_unit():
d = pendulum.datetime(1975, 8, 5)
with pytest.raises(ValueError):
d.nth_of("invalid", 3, pendulum.MONDAY)
|