Skip to content

Commit

Permalink
[Enhancement] Add Between
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-tessier committed Feb 20, 2019
1 parent 274dde4 commit 60b99dc
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 14 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ if HolidayJp(datetime.date.today()).is_holiday:
print('False')
else:
print('True')
# Between return holidays between 2 dates in text
holidays = HolidayJp.between('2009-01-01', '2009-01-31')
new_year_day = holidays[0]
self.assertEqual(datetime.date(year=2009, month=1, day=1), new_year_day.date_obj)
self.assertEqual('元日', new_year_day.name)
# Or date object
holidays = HolidayJp.between(datetime.date(year=2008, month=12, day=23), datetime.date(year=2009, month=1, day=12))
```

check the unit test holiday_jp/test.py
For more usage check the unit test holiday_jp/test.py
2 changes: 1 addition & 1 deletion holiday_jp/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .holiday_jp import HolidayJp
from .holiday_jp import HolidayJp # noqa
64 changes: 53 additions & 11 deletions holiday_jp/holiday_jp.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,12 @@ class HolidayJp(object):
HOLIDAY_DATASET = HolidayDataset.HOLIDAYS

def __init__(self, date):
"""init the date and fill the property."""
# if date is string support the following format otherwise must be a date or datetime object
if isinstance(date, str):
# normalize
date = zenhan.z2h(date)
try:
self.date_obj = datetime.datetime.strptime(date, '%Y-%m-%d')
except Exception as e:
raise e
else:
self.date_obj = date
"""init the date and fill the property.
date: string with the following format yyyy-mm-dd
or date object
"""
self.date_obj = HolidayJp._format_date(date)

date_str = self.date_obj.strftime('%Y-%m-%d')
if self.HOLIDAY_DATASET.get(date_str):
Expand All @@ -47,3 +42,50 @@ def __init__(self, date):
# Monday is 0 and Sunday is 6.
if self.date_obj.weekday() < 5:
self.is_business_day = True

def _format_date(date):
"""Format the date to date object."""
date_obj = None
# if date is string support the following format otherwise must be a date or datetime object
if isinstance(date, str):
# normalize
date = zenhan.z2h(date)
try:
date_obj = datetime.datetime.strptime(date, '%Y-%m-%d').date()
except Exception as e:
raise e
else:
date_obj = date

return date_obj

def between(start, last):
"""Return a tuple of HolidayJp objects between 2 dates.
start, last: string with the following format yyyy-mm-dd
or date object
"""
from dateutil import relativedelta

start = HolidayJp._format_date(start)
last = HolidayJp._format_date(last)

result = []

if last < start:
raise ValueError('last must be greater than start.')

# include the limit
start_date_str = start.strftime('%Y-%m-%d')
if HolidayJp.HOLIDAY_DATASET.get(start_date_str):
result.append(HolidayJp(start))
# go day by day
next_day = start + relativedelta.relativedelta(days=1)

while next_day <= last:
next_day_str = next_day.strftime('%Y-%m-%d')
if HolidayJp.HOLIDAY_DATASET.get(next_day_str):
result.append(HolidayJp(next_day))
next_day += relativedelta.relativedelta(days=1)

return result
20 changes: 19 additions & 1 deletion holiday_jp/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_init(self):
"""Check that the calss init correctly."""
HolidayJp('1990-01-01') # support string
HolidayJp(datetime.date(year=1990, month=1, day=1)) # date
with self.assertRaises(ValueError) as context:
with self.assertRaises(ValueError):
HolidayJp('Banana') # but not banana

def test_is_holiday(self):
Expand Down Expand Up @@ -67,3 +67,21 @@ class MyHolidayJp(HolidayJp):

new_holiday_date = MyHolidayJp('2018-10-01')
self.assertEqual(new_holiday_date.is_holiday, True)

def test_between(self):
"""Check that I can retrieved holiday date between 2 dates."""
holidays = HolidayJp.between('2009-01-01', '2009-01-31')
new_year_day = holidays[0]
self.assertEqual(datetime.date(year=2009, month=1, day=1), new_year_day.date_obj)
self.assertEqual('元日', new_year_day.name)
self.assertEqual("New Year's Day", new_year_day.name_en)
self.assertEqual('木', new_year_day.week)

self.assertEqual(datetime.date(year=2009, month=1, day=12), holidays[1].date_obj)
self.assertEqual('成人の日', holidays[1].name)

holidays = HolidayJp.between(datetime.date(year=2008, month=12, day=23), datetime.date(year=2009, month=1, day=12))

self.assertEqual(datetime.date(year=2008, month=12, day=23), holidays[0].date_obj)
self.assertEqual(datetime.date(year=2009, month=1, day=1), holidays[1].date_obj)
self.assertEqual(datetime.date(year=2009, month=1, day=12), holidays[2].date_obj)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
zenhan==0.5.2
python-dateutil==2.7.5

0 comments on commit 60b99dc

Please sign in to comment.