Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_year(self):
result = list(
arrow.Arrow.range(
"year", datetime(2013, 1, 2, 3, 4, 5), datetime(2016, 4, 5, 6, 7, 8)
)
)
self.assertEqual(
result,
[
arrow.Arrow(2013, 1, 2, 3, 4, 5),
arrow.Arrow(2014, 1, 2, 3, 4, 5),
arrow.Arrow(2015, 1, 2, 3, 4, 5),
arrow.Arrow(2016, 1, 2, 3, 4, 5),
],
def test_year(self):
result = list(
arrow.Arrow.span_range("year", datetime(2013, 2, 1), datetime(2016, 3, 31))
)
self.assertEqual(
result,
[
(
arrow.Arrow(2013, 1, 1),
arrow.Arrow(2013, 12, 31, 23, 59, 59, 999999),
),
(
arrow.Arrow(2014, 1, 1),
arrow.Arrow(2014, 12, 31, 23, 59, 59, 999999),
),
(
arrow.Arrow(2015, 1, 1),
arrow.Arrow(2015, 12, 31, 23, 59, 59, 999999),
),
(
arrow.Arrow(2016, 1, 1),
arrow.Arrow(2016, 12, 31, 23, 59, 59, 999999),
),
result = list(
arrow.Arrow.span_range(
"day", datetime(2013, 1, 1, 12), datetime(2013, 1, 4, 12)
)
)
self.assertEqual(
result,
[
(
arrow.Arrow(2013, 1, 1, 0),
arrow.Arrow(2013, 1, 1, 23, 59, 59, 999999),
),
(
arrow.Arrow(2013, 1, 2, 0),
arrow.Arrow(2013, 1, 2, 23, 59, 59, 999999),
),
(
arrow.Arrow(2013, 1, 3, 0),
arrow.Arrow(2013, 1, 3, 23, 59, 59, 999999),
),
(
arrow.Arrow(2013, 1, 4, 0),
arrow.Arrow(2013, 1, 4, 23, 59, 59, 999999),
),
self.assertEqual(arw.shift(weekday=4), arrow.Arrow(2013, 5, 10, 12, 30, 45))
self.assertEqual(arw.shift(weekday=5), arrow.Arrow(2013, 5, 11, 12, 30, 45))
self.assertEqual(arw.shift(weekday=6), arw)
with self.assertRaises(IndexError):
arw.shift(weekday=7)
# Use dateutil.relativedelta's convenient day instances
self.assertEqual(arw.shift(weekday=MO), arrow.Arrow(2013, 5, 6, 12, 30, 45))
self.assertEqual(arw.shift(weekday=MO(0)), arrow.Arrow(2013, 5, 6, 12, 30, 45))
self.assertEqual(arw.shift(weekday=MO(1)), arrow.Arrow(2013, 5, 6, 12, 30, 45))
self.assertEqual(arw.shift(weekday=MO(2)), arrow.Arrow(2013, 5, 13, 12, 30, 45))
self.assertEqual(arw.shift(weekday=TU), arrow.Arrow(2013, 5, 7, 12, 30, 45))
self.assertEqual(arw.shift(weekday=TU(0)), arrow.Arrow(2013, 5, 7, 12, 30, 45))
self.assertEqual(arw.shift(weekday=TU(1)), arrow.Arrow(2013, 5, 7, 12, 30, 45))
self.assertEqual(arw.shift(weekday=TU(2)), arrow.Arrow(2013, 5, 14, 12, 30, 45))
self.assertEqual(arw.shift(weekday=WE), arrow.Arrow(2013, 5, 8, 12, 30, 45))
self.assertEqual(arw.shift(weekday=WE(0)), arrow.Arrow(2013, 5, 8, 12, 30, 45))
self.assertEqual(arw.shift(weekday=WE(1)), arrow.Arrow(2013, 5, 8, 12, 30, 45))
self.assertEqual(arw.shift(weekday=WE(2)), arrow.Arrow(2013, 5, 15, 12, 30, 45))
self.assertEqual(arw.shift(weekday=TH), arrow.Arrow(2013, 5, 9, 12, 30, 45))
self.assertEqual(arw.shift(weekday=TH(0)), arrow.Arrow(2013, 5, 9, 12, 30, 45))
self.assertEqual(arw.shift(weekday=TH(1)), arrow.Arrow(2013, 5, 9, 12, 30, 45))
self.assertEqual(arw.shift(weekday=TH(2)), arrow.Arrow(2013, 5, 16, 12, 30, 45))
self.assertEqual(arw.shift(weekday=FR), arrow.Arrow(2013, 5, 10, 12, 30, 45))
self.assertEqual(arw.shift(weekday=FR(0)), arrow.Arrow(2013, 5, 10, 12, 30, 45))
self.assertEqual(arw.shift(weekday=FR(1)), arrow.Arrow(2013, 5, 10, 12, 30, 45))
self.assertEqual(arw.shift(weekday=FR(2)), arrow.Arrow(2013, 5, 17, 12, 30, 45))
self.assertEqual(arw.shift(weekday=SA), arrow.Arrow(2013, 5, 11, 12, 30, 45))
self.assertEqual(arw.shift(weekday=SA(0)), arrow.Arrow(2013, 5, 11, 12, 30, 45))
self.assertEqual(arw.shift(weekday=SA(1)), arrow.Arrow(2013, 5, 11, 12, 30, 45))
self.assertEqual(arw.shift(weekday=SA(2)), arrow.Arrow(2013, 5, 18, 12, 30, 45))
result = list(
arrow.Arrow.span_range(
"minute", datetime(2013, 1, 1, 0, 0, 30), datetime(2013, 1, 1, 0, 3, 30)
)
)
self.assertEqual(
result,
[
(
arrow.Arrow(2013, 1, 1, 0, 0),
arrow.Arrow(2013, 1, 1, 0, 0, 59, 999999),
),
(
arrow.Arrow(2013, 1, 1, 0, 1),
arrow.Arrow(2013, 1, 1, 0, 1, 59, 999999),
),
(
arrow.Arrow(2013, 1, 1, 0, 2),
arrow.Arrow(2013, 1, 1, 0, 2, 59, 999999),
),
(
arrow.Arrow(2013, 1, 1, 0, 3),
arrow.Arrow(2013, 1, 1, 0, 3, 59, 999999),
),
def test_shift(self):
arw = arrow.Arrow(2013, 5, 5, 12, 30, 45)
self.assertEqual(arw.shift(years=1), arrow.Arrow(2014, 5, 5, 12, 30, 45))
self.assertEqual(arw.shift(quarters=1), arrow.Arrow(2013, 8, 5, 12, 30, 45))
self.assertEqual(
arw.shift(quarters=1, months=1), arrow.Arrow(2013, 9, 5, 12, 30, 45)
)
self.assertEqual(arw.shift(months=1), arrow.Arrow(2013, 6, 5, 12, 30, 45))
self.assertEqual(arw.shift(weeks=1), arrow.Arrow(2013, 5, 12, 12, 30, 45))
self.assertEqual(arw.shift(days=1), arrow.Arrow(2013, 5, 6, 12, 30, 45))
self.assertEqual(arw.shift(hours=1), arrow.Arrow(2013, 5, 5, 13, 30, 45))
self.assertEqual(arw.shift(minutes=1), arrow.Arrow(2013, 5, 5, 12, 31, 45))
self.assertEqual(arw.shift(seconds=1), arrow.Arrow(2013, 5, 5, 12, 30, 46))
self.assertEqual(
arw.shift(microseconds=1), arrow.Arrow(2013, 5, 5, 12, 30, 45, 1)
)
# Remember: Python's weekday 0 is Monday
self.assertEqual(arw.shift(weekday=0), arrow.Arrow(2013, 5, 6, 12, 30, 45))
self.assertEqual(arw.shift(weekday=1), arrow.Arrow(2013, 5, 7, 12, 30, 45))
self.assertEqual(arw.shift(weekday=2), arrow.Arrow(2013, 5, 8, 12, 30, 45))
self.assertEqual(arw.shift(weekday=3), arrow.Arrow(2013, 5, 9, 12, 30, 45))
self.assertEqual(arw.shift(weekday=4), arrow.Arrow(2013, 5, 10, 12, 30, 45))
self.assertEqual(arw.shift(weekday=5), arrow.Arrow(2013, 5, 11, 12, 30, 45))
def test_month(self):
result = list(
arrow.Arrow.range(
"month", datetime(2013, 2, 3, 4, 5, 6), datetime(2013, 5, 6, 7, 8, 9)
)
)
self.assertEqual(
result,
[
arrow.Arrow(2013, 2, 3, 4, 5, 6),
arrow.Arrow(2013, 3, 3, 4, 5, 6),
arrow.Arrow(2013, 4, 3, 4, 5, 6),
arrow.Arrow(2013, 5, 3, 4, 5, 6),
],
self.assertEqual(arw.shift(weekday=TU), arrow.Arrow(2013, 5, 7, 12, 30, 45))
self.assertEqual(arw.shift(weekday=TU(0)), arrow.Arrow(2013, 5, 7, 12, 30, 45))
self.assertEqual(arw.shift(weekday=TU(1)), arrow.Arrow(2013, 5, 7, 12, 30, 45))
self.assertEqual(arw.shift(weekday=TU(2)), arrow.Arrow(2013, 5, 14, 12, 30, 45))
self.assertEqual(arw.shift(weekday=WE), arrow.Arrow(2013, 5, 8, 12, 30, 45))
self.assertEqual(arw.shift(weekday=WE(0)), arrow.Arrow(2013, 5, 8, 12, 30, 45))
self.assertEqual(arw.shift(weekday=WE(1)), arrow.Arrow(2013, 5, 8, 12, 30, 45))
self.assertEqual(arw.shift(weekday=WE(2)), arrow.Arrow(2013, 5, 15, 12, 30, 45))
self.assertEqual(arw.shift(weekday=TH), arrow.Arrow(2013, 5, 9, 12, 30, 45))
self.assertEqual(arw.shift(weekday=TH(0)), arrow.Arrow(2013, 5, 9, 12, 30, 45))
self.assertEqual(arw.shift(weekday=TH(1)), arrow.Arrow(2013, 5, 9, 12, 30, 45))
self.assertEqual(arw.shift(weekday=TH(2)), arrow.Arrow(2013, 5, 16, 12, 30, 45))
self.assertEqual(arw.shift(weekday=FR), arrow.Arrow(2013, 5, 10, 12, 30, 45))
self.assertEqual(arw.shift(weekday=FR(0)), arrow.Arrow(2013, 5, 10, 12, 30, 45))
self.assertEqual(arw.shift(weekday=FR(1)), arrow.Arrow(2013, 5, 10, 12, 30, 45))
self.assertEqual(arw.shift(weekday=FR(2)), arrow.Arrow(2013, 5, 17, 12, 30, 45))
self.assertEqual(arw.shift(weekday=SA), arrow.Arrow(2013, 5, 11, 12, 30, 45))
self.assertEqual(arw.shift(weekday=SA(0)), arrow.Arrow(2013, 5, 11, 12, 30, 45))
self.assertEqual(arw.shift(weekday=SA(1)), arrow.Arrow(2013, 5, 11, 12, 30, 45))
self.assertEqual(arw.shift(weekday=SA(2)), arrow.Arrow(2013, 5, 18, 12, 30, 45))
self.assertEqual(arw.shift(weekday=SU), arw)
self.assertEqual(arw.shift(weekday=SU(0)), arw)
self.assertEqual(arw.shift(weekday=SU(1)), arw)
self.assertEqual(arw.shift(weekday=SU(2)), arrow.Arrow(2013, 5, 12, 12, 30, 45))
def pp_date(date):
"""
Pretty print the Arrow datetime object.
:param date: Arrow datetime - User's birthday
:return: String - Human readable formatted date
"""
if not isinstance(date, arrow.arrow.Arrow):
date = arrow.get(date)
return date.format("MMMM D, YYYY")
def format_datetime(self, ar):
if isinstance(ar,arrow.arrow.Arrow):
return ar.strftime(self.output_format)
elif isinstance(ar,str) and ar:
return self.format_datetime(arrow.get(ar,self.input_format).replace(tzinfo=self.tz))
else:
return ""