Skip to content

Commit

Permalink
2018.6.15 v1.0.14 update function fish_date.get_date_range() doc and …
Browse files Browse the repository at this point in the history
…unittest from issue #61
  • Loading branch information
jun.hu committed Jun 15, 2018
1 parent f7795d2 commit a262c3c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 21 deletions.
1 change: 1 addition & 0 deletions docs/change_log.rst
Expand Up @@ -10,6 +10,7 @@
* 38, common, edit function :meth:`fish_common.find_files()`, optimize, doc and unittest;
* 37, date, edit function :meth:`fish_date.get_years()`, optimize, doc and unittest;
* 27, common, edit function :meth:`fish_common.hmac_sha256()`, optimize, doc and unittest;
* 61, date, edit function :meth:`fish_date.get_date_range()`, optimize, doc and unittest;

2018.6.6 v1.0.13
---------------------------
Expand Down
1 change: 1 addition & 0 deletions docs/fish_date.rst
Expand Up @@ -2,6 +2,7 @@
=============================

.. autosummary::
fish_date.get_date_range
fish_date.get_years

.. automodule:: fish_date
Expand Down
66 changes: 45 additions & 21 deletions fishbase/fish_date.py
@@ -1,43 +1,67 @@
# coding=utf-8

from datetime import datetime, date
from dateutil.relativedelta import relativedelta
from datetime import datetime
import calendar


# 2016.4.26
# 输入: date_kind, eg 'last month', 'this month'
# 输出: tuple, type datetime.date eg '2016-03-01' '2016-03-31'
def get_date_range(date_kind):

today = datetime.today()
this_year = datetime.today().year
this_month = datetime.today().month
# v1.0.14 #61, edit by Hu Jun
def get_date_range(dates, separator='-'):
"""
获取某个月的日期范围,返回该月第一天和最后一天的字符串表示
:param:
* dates: (string 或者 datetime obj) 月份信息
* separator: (string) 分隔符,默认为 '-'
:return:
* first_day: (string) 该月份的第一天
* last_day: (string) 该月份的最后一天
first_day = last_day = today
举例如下::
# 上个月
if date_kind == 'last month':
first_day = ((today - relativedelta(months=1)).replace(day=1)).date()
last_day = date(this_year, this_month, 1) - relativedelta(days=1)
print('--- get_date_range demo ---')
now_time = datetime.now()
print(get_date_range(now_time))
print(get_date_range('201802',separator='/'))
print('---')
# 本月
if date_kind == 'this month':
first_day = today.replace(day=1).date()
执行结果::
next_month = this_month + 1
if next_month == 13:
next_month = 1
--- get_years demo ---
('2018-06-1', '2018-06-30')
('2018/02/1', '2018/02/28')
---
this_month_last_day = date(this_year, next_month, 1) - relativedelta(days=1)
last_day = this_month_last_day
"""
if isinstance(dates, str) and dates.isdigit():
y = dates[:4]
m = dates[4:]
if (len(y) != 4) or (not 1 < int(m) < 12):
raise (ValueError("date must be a date string like '201806', but get {}".format(dates)))
elif hasattr(dates, 'year') and hasattr(dates, 'month'):
y = str(dates.year)
m = str(dates.month)
else:
raise (TypeError("date except a years string like '201806' or a object has 'year' "
"and 'month' attribute, but get a {}".format(type(dates))))

# set month to length 2 if month less than 10
m = '0'+m if len(m) != 2 else m

mr = calendar.monthrange(int(y), int(m))

first_day = separator.join([y, m, '1'])
last_day = separator.join([y, m, str(mr[1])])

return first_day, last_day


# v1.0.14 #37, edit by Hu Jun
def get_years(months=0, refer=None):
"""
get_years,获取基准时月份增量的年月
获取基准时月份增量的年月
:param:
* months: (int) 月份增量,正数为往后年月,整数为往前年月
Expand Down
12 changes: 12 additions & 0 deletions test/test_date.py
Expand Up @@ -31,3 +31,15 @@ def test_get_years_01(self):

with pytest.raises(TypeError):
get_years(-5, 8)

def test_get_date_range_01(self):
this_month = datetime.date(day=1, month=2, year=2018)

assert get_date_range(this_month) == ('2018-02-1', '2018-02-28')
assert get_date_range('201802', separator='/') == ('2018/02/1', '2018/02/28')

with pytest.raises(ValueError):
get_date_range('2016798')

with pytest.raises(TypeError):
get_date_range('asdafsd')

0 comments on commit a262c3c

Please sign in to comment.