opendate 0.1.13__py3-none-any.whl → 0.1.20__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of opendate might be problematic. Click here for more details.

date/extras.py CHANGED
@@ -1,6 +1,15 @@
1
- from collections import namedtuple
1
+ """Legacy compatibility functions for OpenDate.
2
2
 
3
- from date import NYSE, DateTime, Entity
3
+ This module contains functions that exist primarily for backward compatibility
4
+ with older codebases. These functions provide alternative interfaces to
5
+ functionality that may be available through other means in the core Date,
6
+ DateTime, and Interval classes.
7
+
8
+ New code should prefer using the built-in methods on Date, DateTime, and
9
+ Interval objects where applicable.
10
+ """
11
+
12
+ from date import NYSE, Date, DateTime, Entity, Interval
4
13
 
5
14
  __all__ = [
6
15
  'is_within_business_hours',
@@ -10,27 +19,7 @@ __all__ = [
10
19
 
11
20
 
12
21
  def is_within_business_hours(entity: Entity = NYSE) -> bool:
13
- """Return whether the current native datetime is between
14
- open and close of business hours.
15
-
16
- >>> from unittest.mock import patch
17
- >>> tz = NYSE.tz
18
-
19
- >>> with patch('date.DateTime.now') as mock:
20
- ... mock.return_value = DateTime(2000, 5, 1, 12, 30, 0, 0, tzinfo=tz)
21
- ... is_within_business_hours()
22
- True
23
-
24
- >>> with patch('date.DateTime.now') as mock:
25
- ... mock.return_value = DateTime(2000, 7, 2, 12, 15, 0, 0, tzinfo=tz) # Sunday
26
- ... is_within_business_hours()
27
- False
28
-
29
- >>> with patch('date.DateTime.now') as mock:
30
- ... mock.return_value = DateTime(2000, 11, 1, 1, 15, 0, 0, tzinfo=tz)
31
- ... is_within_business_hours()
32
- False
33
-
22
+ """Return whether the current native datetime is between open and close of business hours.
34
23
  """
35
24
  this = DateTime.now()
36
25
  this_entity = DateTime.now(tz=entity.tz).entity(entity)
@@ -44,47 +33,26 @@ def is_business_day(entity: Entity = NYSE) -> bool:
44
33
  return DateTime.now(tz=entity.tz).entity(entity).is_business_day()
45
34
 
46
35
 
47
- Range = namedtuple('Range', ['start', 'end'])
48
-
36
+ def overlap_days(
37
+ interval_one: Interval | tuple[Date | DateTime, Date | DateTime],
38
+ interval_two: Interval | tuple[Date | DateTime, Date | DateTime],
39
+ days: bool = False,
40
+ ) -> bool | int:
41
+ """Calculate how much two date intervals overlap.
49
42
 
50
- def overlap_days(range_one, range_two, days=False):
51
- """Test by how much two date ranges overlap
52
- if `days=True`, we return an actual day count,
53
- otherwise we just return if it overlaps True/False
54
- poached from Raymond Hettinger http://stackoverflow.com/a/9044111
43
+ When days=False, returns True/False indicating whether intervals overlap.
44
+ When days=True, returns the actual day count (negative if non-overlapping).
55
45
 
56
- >>> from date import Date
57
- >>> date1 = Date(2016, 3, 1)
58
- >>> date2 = Date(2016, 3, 2)
59
- >>> date3 = Date(2016, 3, 29)
60
- >>> date4 = Date(2016, 3, 30)
61
-
62
- >>> assert overlap_days((date1, date3), (date2, date4))
63
- >>> assert overlap_days((date2, date4), (date1, date3))
64
- >>> assert not overlap_days((date1, date2), (date3, date4))
65
-
66
- >>> assert overlap_days((date1, date4), (date1, date4))
67
- >>> assert overlap_days((date1, date4), (date2, date3))
68
- >>> overlap_days((date1, date4), (date1, date4), True)
69
- 30
70
-
71
- >>> assert overlap_days((date2, date3), (date1, date4))
72
- >>> overlap_days((date2, date3), (date1, date4), True)
73
- 28
74
-
75
- >>> assert not overlap_days((date3, date4), (date1, date2))
76
- >>> overlap_days((date3, date4), (date1, date2), True)
77
- -26
46
+ Algorithm adapted from Raymond Hettinger: http://stackoverflow.com/a/9044111
78
47
  """
79
- r1 = Range(*range_one)
80
- r2 = Range(*range_two)
81
- latest_start = max(r1.start, r2.start)
82
- earliest_end = min(r1.end, r2.end)
48
+ if not isinstance(interval_one, Interval):
49
+ interval_one = Interval(*interval_one)
50
+ if not isinstance(interval_two, Interval):
51
+ interval_two = Interval(*interval_two)
52
+
53
+ latest_start = max(interval_one._start, interval_two._start)
54
+ earliest_end = min(interval_one._end, interval_two._end)
83
55
  overlap = (earliest_end - latest_start).days + 1
84
56
  if days:
85
57
  return overlap
86
58
  return overlap >= 0
87
-
88
-
89
- if __name__ == '__main__':
90
- __import__('doctest').testmod(optionflags=4 | 8 | 32)