@@ -591,9 +591,12 @@ def __init__(self, start='09:00', end='17:00', offset=timedelta(0)):
591
591
raise ValueError ('number of starting time and ending time'
592
592
'must be the same' )
593
593
num_openings = len (start )
594
- openings = sorted (zip (start , end ))
595
- start = tuple (x for x , _ in openings )
596
- end = tuple (x for _ , x in openings )
594
+
595
+ # sort starting and ending time by starting time
596
+ index = np .argsort (start )
597
+ start = tuple (np .array (start )[index ])
598
+ end = tuple (np .array (end )[index ])
599
+
597
600
total_secs = 0
598
601
for i in range (num_openings ):
599
602
total_secs += self ._get_business_hours_by_sec (start [i ], end [i ])
@@ -627,7 +630,7 @@ def next_bday(self):
627
630
def _get_daytime_flag (self , start , end ):
628
631
return start < end
629
632
630
- def _next_opening_time (self , other ):
633
+ def _next_opening_time (self , other , dir = 1 ):
631
634
"""
632
635
If n is positive, return tomorrow's business day opening time.
633
636
Otherwise yesterday's business day's opening time.
@@ -638,23 +641,23 @@ def _next_opening_time(self, other):
638
641
earliest_start = self .start [0 ]
639
642
latest_start = self .start [- 1 ]
640
643
if not self .next_bday .onOffset (other ):
641
- other = other + self .next_bday
642
- if self .n >= 0 :
644
+ other = other + dir * self .next_bday
645
+ if self .n * dir >= 0 :
643
646
return datetime (other .year , other .month , other .day ,
644
647
earliest_start .hour , earliest_start .minute )
645
648
else :
646
649
return datetime (other .year , other .month , other .day ,
647
650
latest_start .hour , latest_start .minute )
648
651
else :
649
- if self .n >= 0 and latest_start < other .time ():
650
- other = other + self .next_bday
652
+ if self .n * dir >= 0 and latest_start < other .time ():
653
+ other = other + dir * self .next_bday
651
654
return datetime (other .year , other .month , other .day ,
652
655
earliest_start .hour , earliest_start .minute )
653
- elif self .n < 0 and other .time () < earliest_start :
654
- other = other + self .next_bday
656
+ elif self .n * dir < 0 and other .time () < earliest_start :
657
+ other = other + dir * self .next_bday
655
658
return datetime (other .year , other .month , other .day ,
656
659
latest_start .hour , latest_start .minute )
657
- if self .n >= 0 :
660
+ if self .n * dir >= 0 :
658
661
for st in self .start :
659
662
if other .time () <= st :
660
663
return datetime (other .year , other .month , other .day ,
@@ -670,35 +673,7 @@ def _prev_opening_time(self, other):
670
673
If n is positive, return yesterday's business day opening time.
671
674
Otherwise yesterday business day's opening time.
672
675
"""
673
- earliest_start = self .start [0 ]
674
- latest_start = self .start [- 1 ]
675
- if not self .next_bday .onOffset (other ):
676
- other = other - self .next_bday
677
- if self .n < 0 :
678
- return datetime (other .year , other .month , other .day ,
679
- earliest_start .hour , earliest_start .minute )
680
- else :
681
- return datetime (other .year , other .month , other .day ,
682
- latest_start .hour , latest_start .minute )
683
- else :
684
- if self .n < 0 and latest_start < other .time ():
685
- other = other - self .next_bday
686
- return datetime (other .year , other .month , other .day ,
687
- earliest_start .hour , earliest_start .minute )
688
- elif self .n >= 0 and other .time () < earliest_start :
689
- other = other - self .next_bday
690
- return datetime (other .year , other .month , other .day ,
691
- latest_start .hour , latest_start .minute )
692
- if self .n < 0 :
693
- for st in self .start :
694
- if other .time () <= st :
695
- return datetime (other .year , other .month , other .day ,
696
- st .hour , st .minute )
697
- else :
698
- for st in reversed (self .start ):
699
- if other .time () >= st :
700
- return datetime (other .year , other .month , other .day ,
701
- st .hour , st .minute )
676
+ return self ._next_opening_time (other , dir = - 1 )
702
677
703
678
def _get_business_hours_by_sec (self , start , end ):
704
679
"""
@@ -862,10 +837,10 @@ def _onOffset(self, dt):
862
837
863
838
def _repr_attrs (self ):
864
839
out = super ()._repr_attrs ()
865
- hours = ',' .join ('{}-{}' .format (st .strftime ('%H:%M' ), en .strftime ('%H:%M' ))
840
+ hours = ',' .join ('{}-{}' .format (
841
+ st .strftime ('%H:%M' ), en .strftime ('%H:%M' ))
866
842
for st , en in zip (self .start , self .end ))
867
- attrs = ['{prefix}={hours}' .format (prefix = self ._prefix ,
868
- hours = hours )]
843
+ attrs = ['{prefix}={hours}' .format (prefix = self ._prefix , hours = hours )]
869
844
out += ': ' + ', ' .join (attrs )
870
845
return out
871
846
0 commit comments