Skip to content

Commit

Permalink
Merge pull request #177 from chinapnr/jiachunying_20181225
Browse files Browse the repository at this point in the history
Jiachunying 20181225
  • Loading branch information
mindjun committed Dec 26, 2018
2 parents 32d1eaa + 8141b1a commit c63e1ec
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 18 deletions.
22 changes: 22 additions & 0 deletions appveyor.yml
@@ -0,0 +1,22 @@
build: false

environment:
matrix:
- PYTHON: "C:/Python34"
- PYTHON: "C:/Python35"
- PYTHON: "C:/Python36"
- PYTHON: "C:/Python37"

init:
- "ECHO %PYTHON%"
- ps: "ls C:/Python*"

install:
- "curl -fsS -o C:/get-pip.py https://bootstrap.pypa.io/get-pip.py"
- "%PYTHON%/python.exe C:/get-pip.py"
- "%PYTHON%/Scripts/pip.exe install -U setuptools"
- "%PYTHON%/python.exe setup.py develop"
- "%PYTHON%/Scripts/pip.exe install -U -r requirements.txt"

test_script:
- "%PYTHON%/Scripts/py.test.exe -sv test/"
5 changes: 5 additions & 0 deletions docs/change_log.rst
@@ -1,5 +1,10 @@
更新记录
===========================
2018.12.25 v1.1.5
---------------------------
* `#164 <https://github.com/chinapnr/fishbase/issues/164>`_, common, add function :meth:`fish_date.GetRandomTime.gen_date_by_range`, doc and unittest;
* `#142 <https://github.com/chinapnr/fishbase/issues/142>`_, common, edit function :meth:`fish_date.GetRandomTime.gen_date_by_year`, doc and unittest;

2018.12.14 v1.1.4
---------------------------
* `#142 <https://github.com/chinapnr/fishbase/issues/142>`_, common, add function :meth:`fish_date.GetRandomTime.random_date_str`, doc and unittest;
Expand Down
2 changes: 1 addition & 1 deletion fishbase/__init__.py
Expand Up @@ -25,5 +25,5 @@
from .fish_project import *
from .fish_data import *

__version__ = '1.1.4' # type: str
__version__ = '1.1.5' # type: str

72 changes: 58 additions & 14 deletions fishbase/fish_date.py
Expand Up @@ -109,6 +109,7 @@ def get_years(months=0, refer=None):
return ''.join(['%04d' % y, '%02d' % m])


# v1.1.15 edit by Jia Chunying #142 #164
# v1.1.14 edit by Hu Jun #142
# v1.0.16 edit by Hu Jun #87
class GetRandomTime(object):
Expand Down Expand Up @@ -188,7 +189,7 @@ def date_time_this_year():
return this_year_start + timedelta(seconds=random_seconds)

@staticmethod
def random_date_str(year):
def gen_date_by_year(year):
"""
获取当前年的随机时间字符串
Expand All @@ -200,14 +201,14 @@ def random_date_str(year):
举例如下::
print('--- GetRandomTime.random_date_str demo ---')
print(GetRandomTime.random_date_str(2010))
print('--- GetRandomTime.gen_date_by_year demo ---')
print(GetRandomTime.gen_date_by_year("2010"))
print('---')
执行结果::
--- GetRandomTime.date_time_this_year demo demo ---
20101008
--- GetRandomTime.gen_date_by_year demo ---
20100505
---
"""
if isinstance(year, int) and len(str(year)) != 4:
Expand All @@ -216,23 +217,66 @@ def random_date_str(year):
if isinstance(year, str) and len(year) != 4:
raise ValueError("year should be string year like '2018', but we got {}, {}".
format(year, type(year)))
if isinstance(year, int):
year = str(year)

date_str = GetRandomTime.gen_date_by_range(year + "-01-01", year + "-12-31", "%Y%m%d")

return date_str

@staticmethod
def gen_date_by_range(begin_date, end_date, date_format="%Y-%m-%d"):
"""
指定一个日期范围,随机生成区间内的某一个日期,该区间为闭区间
:param:
* begin_date: (string) 范围的起始日期,字符串 yyyy-MM-dd eg. 2018-01-01
* end_date: (string) 范围的结束日期,字符串 yyyy-MM-dd eg. 2018-12-31
* date_format: 返回的日期格式,字符串:默认格式yyyyMMdd default: "%Y%m%d"
:return:
* date_str
日期区间内的一个指定格式的合法的随机日期
举例如下::
# 设置开始日期时间元组(1980-01-01 00:00:00)
start_time_tuple = (year, 1, 1, 0, 0, 0, 0, 0, 0)
# 设置结束日期时间元组(1980-12-31 23:59:59)
end_time_tuple = (year, 12, 31, 23, 59, 59, 0, 0, 0)
print('--- GetRandomTime.gen_date_by_range demo ---')
print(GetRandomTime.gen_date_by_range("2010-01-01","2010-12-31"))
print('---')
# 生成开始时间戳
start_timestamp = time.mktime(start_time_tuple)
# 生成结束时间戳
end_timestamp = time.mktime(end_time_tuple)
执行结果::
--- GetRandomTime.gen_date_by_range demo ---
20100124
---
"""
# 设置开始日期
begin_date_info = begin_date.split("-")
begin_date_info = [int(x) for x in begin_date_info]
begin_date_info.extend([0, 0, 0, 0, 0, 0])
begin_date_tuple = tuple(begin_date_info)
# 设置结束日期
end_date_info = end_date.split("-")
end_date_info = [int(x) for x in end_date_info]
end_date_info.extend([23, 59, 59, 59, 0, 0])
end_date_tuple = tuple(end_date_info)

try:
# 生成开始时间戳
start_timestamp = time.mktime(begin_date_tuple)
# 生成结束时间戳
end_timestamp = time.mktime(end_date_tuple)
except TypeError as e:
raise TypeError(e, "begin_date/end_date format error")

# 在开始和结束时间戳中随机取出一个
rand_timedelta = random.randint(start_timestamp, end_timestamp)
# 将时间戳生成时间元组
date_tuple = time.localtime(rand_timedelta)

# 将时间元组转成格式化字符串
date_str = time.strftime("%Y%m%d", date_tuple)
date_str = time.strftime(date_format, date_tuple)
return date_str


Expand Down
1 change: 1 addition & 0 deletions requirements.txt
@@ -1,5 +1,6 @@
python-dateutil
coverage
pytest
pytest-cov
coveralls
pyyaml
6 changes: 3 additions & 3 deletions test/test_date.py
Expand Up @@ -54,17 +54,17 @@ def test_date_time_this_year_01(self):

# 测试 GetRandomTime() tc
def test_random_date_str_01(self):
date_str = GetRandomTime.random_date_str(2018)
date_str = GetRandomTime.gen_date_by_year(2018)

assert date_str[:4] == '2018'

# 测试 GetRandomTime() tc
def test_random_date_str_02(self):
with pytest.raises(ValueError):
GetRandomTime.random_date_str('201812')
GetRandomTime.gen_date_by_year('201812')

with pytest.raises(ValueError):
GetRandomTime.random_date_str(18)
GetRandomTime.gen_date_by_year(18)

# 测试 get_time_interval() tc
def test_get_time_interval_01(self):
Expand Down

0 comments on commit c63e1ec

Please sign in to comment.