-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from chinapnr/hujun_20180620
Hujun 20180620
- Loading branch information
Showing
8 changed files
with
217 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
``fish_date`` 基本函数包 | ||
============================= | ||
|
||
.. autosummary:: | ||
fish_date.get_date_range | ||
fish_date.get_years | ||
|
||
.. automodule:: fish_date | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ API 函数列表 | |
fish_system | ||
fish_file | ||
fish_csv | ||
fish_date | ||
fish_logger | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,105 @@ | ||
# 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): | ||
# 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) 该月份的最后一天 | ||
today = datetime.today() | ||
this_year = datetime.today().year | ||
this_month = datetime.today().month | ||
举例如下:: | ||
first_day = last_day = today | ||
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 == 'last month': | ||
first_day = ((today - relativedelta(months=1)).replace(day=1)).date() | ||
last_day = date(this_year, this_month, 1) - relativedelta(days=1) | ||
执行结果:: | ||
# 本月 | ||
if date_kind == 'this month': | ||
first_day = today.replace(day=1).date() | ||
--- get_years demo --- | ||
('2018-06-1', '2018-06-30') | ||
('2018/02/1', '2018/02/28') | ||
--- | ||
next_month = this_month + 1 | ||
if next_month == 13: | ||
next_month = 1 | ||
|
||
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): | ||
""" | ||
获取基准时月份增量的年月 | ||
:param: | ||
* months: (int) 月份增量,正数为往后年月,整数为往前年月 | ||
* refer: (datetime obj) datetime 对象,或者有month和year属性的实例,默认为当前时间 | ||
:return: | ||
* result: (string) 年月字符串 | ||
举例如下:: | ||
print('--- get_years demo ---') | ||
print(get_years(-5)) | ||
print(get_years(7, datetime.now())) | ||
print('---') | ||
执行结果:: | ||
--- get_years demo --- | ||
201801 | ||
201901 | ||
--- | ||
""" | ||
if refer is None: | ||
refer = datetime.now() | ||
# 计算当前总月份数 | ||
try: | ||
months_count = refer.year * 12 + refer.month | ||
except Exception: | ||
raise TypeError('refer except {}, got an {}'.format(type(datetime.now()), type(refer))) | ||
|
||
# 计算结果总月分数 | ||
months_count += months | ||
|
||
y, m = divmod(months_count, 12) | ||
|
||
# 将m的值转换为1-12 | ||
if m == 0: | ||
y -= 1 | ||
m = 12 | ||
|
||
return ''.join(['%04d' % y, '%02d' % m]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# coding=utf-8 | ||
# fish_date.py 单元测试 | ||
# 2018.6.11 create by Hu Jun | ||
|
||
import pytest | ||
import sys | ||
sys.path.append('../fishbase') | ||
from fishbase.fish_date import * | ||
import datetime | ||
|
||
|
||
# 2018.6.11 v1.0.14 #37 create by Hu Jun | ||
class TestFishDate(object): | ||
# test get_years() tc | ||
def test_get_years_01(self): | ||
this_month = datetime.date(day=1, month=6, year=2018) | ||
|
||
assert get_years(7, this_month) == '201901' | ||
assert get_years(-5, this_month) == '201801' | ||
assert get_years(-6, this_month) == '201712' | ||
|
||
this_month1 = datetime.datetime.now() | ||
|
||
y = this_month1.year | ||
m = this_month1.month + 1 | ||
if m == 12: | ||
y += 1 | ||
m = 1 | ||
|
||
assert get_years(1) == ''.join(['%04d' % y, '%02d' % m]) | ||
|
||
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') |