-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
增加自动化测试 #9
Open
lihuanshuai
wants to merge
7
commits into
CutePandaSh:dev
Choose a base branch
from
lihuanshuai:master
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
增加自动化测试 #9
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5b663a6
add flake8 and pytest workflow
lihuanshuai a10154d
bump version
lihuanshuai c63a8a1
Merge branch 'dev' into master
lihuanshuai 66c87b7
update workflow
lihuanshuai ee48c6f
fix
lihuanshuai 91f1eca
fix
lihuanshuai 8b153fe
add test
lihuanshuai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[flake8] | ||
max-line-length = 100 |
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,36 @@ | ||
# This workflow will install Python dependencies, run tests and lint with a single version of Python | ||
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions | ||
|
||
name: Python application | ||
|
||
on: | ||
push: | ||
branches: [ master ] | ||
pull_request: | ||
branches: [ master ] | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Set up Python 3.7 | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: 3.7 | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi | ||
- name: Install dependencies for testing | ||
run: | | ||
if [ -f requirements-test.txt ]; then pip install -r requirements-test.txt; fi | ||
- name: Lint with flake8 | ||
run: | | ||
# stop the build if there are Python syntax errors or undefined names | ||
flake8 . --count --show-source --statistics | ||
- name: Test with pytest | ||
run: | | ||
python -m pytest |
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,3 @@ | ||
flake8==3.9.0 | ||
importlib-metadata==4.13.0 | ||
pytest==6.0.1 |
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,28 +1,30 @@ | ||
# -*- coding=utf-8 -*- | ||
|
||
from zhdate import ZhDate | ||
from datetime import datetime | ||
|
||
if __name__ == '__main__': | ||
date1 = ZhDate(2010, 1, 1) # 新建农历 2010年正月初一 的日期对象 | ||
date1 = ZhDate(2010, 1, 1) # 新建农历 2010年正月初一 的日期对象 | ||
# print(date1) # 直接返回农历日期字符串 | ||
dt_date1 = date1.to_datetime() # 农历转换成阳历日期 datetime 类型 | ||
dt_date1 = date1.to_datetime() # 农历转换成阳历日期 datetime 类型 | ||
|
||
dt_date2 = datetime(2010, 2, 6) | ||
date2 = ZhDate.from_datetime(dt_date2) # 从阳历日期转换成农历日期对象 | ||
dt_date2 = datetime(2010, 2, 6) | ||
date2 = ZhDate.from_datetime(dt_date2) # 从阳历日期转换成农历日期对象 | ||
|
||
date3 = ZhDate(2020, 4, 29, leap_month=True) # 新建农历 2020年闰4月30日 | ||
date3 = ZhDate(2020, 4, 29, leap_month=True) # 新建农历 2020年闰4月30日 | ||
# print(date3.to_datetime()) | ||
|
||
# 支持比较 | ||
if ZhDate(2019, 1, 1) == ZhDate.from_datetime(datetime(2019, 2, 5)): | ||
pass | ||
|
||
# 减法支持 | ||
new_zhdate = ZhDate(2019, 1, 1) - 30 #减整数,得到差额天数的新农历对象 | ||
new_zhdate2 = ZhDate(2019, 1, 1) - ZhDate(2018, 1, 1) #两个zhdate对象相减得到两个农历日期的差额 | ||
new_zhdate3 = ZhDate(2019, 1, 1) - datetime(2019, 1, 1) # 减去阳历日期,得到农历日期和阳历日期之间的天数差额 | ||
new_zhdate = ZhDate(2019, 1, 1) - 30 # 减整数,得到差额天数的新农历对象 | ||
delta1 = ZhDate(2019, 1, 1) - ZhDate(2018, 1, 1) # 两个zhdate对象相减得到两个农历日期的差额 | ||
delta2 = ZhDate(2019, 1, 1) - datetime(2019, 1, 1) # 减去阳历日期,得到农历日期和阳历日期之间的天数差额 | ||
|
||
# 加法支持 | ||
new_zhdate4 = ZhDate(2019, 1, 1) + 30 # 加整数返回相隔天数以后的新农历对象 | ||
new_zhdate4 = ZhDate(2019, 1, 1) + 30 # 加整数返回相隔天数以后的新农历对象 | ||
|
||
print(ZhDate(1900, 9, 1, False).chinese()) | ||
print(ZhDate.today().chinese()) |
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 |
---|---|---|
@@ -1,13 +1,14 @@ | ||
''' | ||
-*- coding: utf-8 -*- | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
File: zhdate.py | ||
File Created: Sunday, 17th February 2019 4:58:02 pm | ||
Author: Wang, Yi ([email protected]) | ||
''' | ||
''' | ||
----- | ||
changed: Saturday, 21st January 2023 | ||
by: Eilles Wan ([email protected]) | ||
''' | ||
""" | ||
|
||
from datetime import datetime, timedelta | ||
from itertools import accumulate | ||
from .constants import CHINESENEWYEAR, CHINESEYEARCODE | ||
|
@@ -55,17 +56,19 @@ def from_datetime(dt): | |
ZhDate -- 生成的农历日期对象 | ||
""" | ||
lunar_year = dt.year | ||
# 如果还没有到农历正月初一 农历年份减去1 | ||
lunar_year -= (datetime.strptime(CHINESENEWYEAR[lunar_year-1900], '%Y%m%d') - dt).total_seconds() > 0 | ||
# 当时农历新年时的日期对象 | ||
newyear_dt = datetime.strptime(CHINESENEWYEAR[lunar_year-1900], '%Y%m%d') | ||
# 如果还没有到农历正月初一 农历年份减去1 | ||
lunar_year -= (newyear_dt - dt).total_seconds() > 0 | ||
# 查询日期距离当年的春节差了多久 | ||
days_passed = (dt - newyear_dt).days | ||
# 被查询日期的年份码 | ||
year_code = CHINESEYEARCODE[lunar_year - 1900] | ||
# 取得本年的月份列表 | ||
month_days = ZhDate.decode(year_code) | ||
|
||
month = 0 | ||
lunar_day = 0 | ||
for pos, days in enumerate(accumulate(month_days)): | ||
if days_passed + 1 <= days: | ||
month = pos + 1 | ||
|
@@ -94,13 +97,13 @@ def __days_passed(self): | |
int -- 差值天数 | ||
""" | ||
month_days = ZhDate.decode(self.year_code) | ||
#当前农历年的闰月,为0表示无润叶 | ||
month_leap = self.year_code & 0xf | ||
# 当前农历年的闰月,为0表示无润叶 | ||
month_leap = self.year_code & 0xf | ||
|
||
#当年无闰月,或者有闰月但是当前月小于闰月 | ||
# 当年无闰月,或者有闰月但是当前月小于闰月 | ||
if (month_leap == 0) or (self.lunar_month < month_leap): | ||
days_passed_month = sum(month_days[:self.lunar_month - 1]) | ||
#当前不是闰月,并且当前月份和闰月相同 | ||
# 当前不是闰月,并且当前月份和闰月相同 | ||
elif (not self.leap_month) and (self.lunar_month == month_leap): | ||
days_passed_month = sum(month_days[:self.lunar_month - 1]) | ||
else: | ||
|
@@ -157,7 +160,11 @@ def __str__(self): | |
Returns: | ||
str -- 标准格式农历日期字符串 | ||
""" | ||
return "农历{}年{}{}月{}日".format(self.lunar_year,"闰" if self.leap_month else "",self.lunar_month,self.lunar_day) | ||
return "农历{}年{}{}月{}日".format( | ||
self.lunar_year, "闰" if self.leap_month else "", | ||
self.lunar_month, | ||
self.lunar_day | ||
) | ||
|
||
def __repr__(self): | ||
return self.__str__() | ||
|
@@ -194,7 +201,7 @@ def __sub__(self, another): | |
def __tiandi(anum): | ||
tian = '甲乙丙丁戊己庚辛壬癸' | ||
di = '子丑寅卯辰巳午未申酉戌亥' | ||
return '{}{}'.format(tian[anum % 10],di[anum % 12]) | ||
return '{}{}'.format(tian[anum % 10], di[anum % 12]) | ||
|
||
@staticmethod | ||
def validate(year, month, day, leap): | ||
|
@@ -217,15 +224,15 @@ def validate(year, month, day, leap): | |
|
||
# 有闰月标志 | ||
if leap: | ||
if (year_code & 0xf) != month: # 年度闰月和校验闰月不一致的话,返回校验失败 | ||
if (year_code & 0xf) != month: # 年度闰月和校验闰月不一致的话,返回校验失败 | ||
return False | ||
elif day == 30: # 如果日期是30的话,直接返回年度代码首位是否为1,即闰月是否为大月 | ||
return (year_code >> 16) == 1 | ||
else: # 年度闰月和当前月份相同,日期不为30的情况,返回通过 | ||
else: # 年度闰月和当前月份相同,日期不为30的情况,返回通过 | ||
return True | ||
elif day <= 29: # 非闰月,并且日期小于等于29,返回通过 | ||
elif day <= 29: # 非闰月,并且日期小于等于29,返回通过 | ||
return True | ||
else: # 非闰月日期为30,返回年度代码中的月份位是否为1,即是否为大月 | ||
else: # 非闰月日期为30,返回年度代码中的月份位是否为1,即是否为大月 | ||
return ((year_code >> (12 - month) + 4) & 1) == 1 | ||
|
||
@staticmethod | ||
|
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,9 +1,10 @@ | ||
''' | ||
-*- coding: utf-8 -*- | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
File: constant.py | ||
File Created: Saturday, 21st January 2023 01:42 am | ||
Author: Eilles Wan ([email protected]) | ||
''' | ||
""" | ||
|
||
CHINESEYEARCODE = [ | ||
19416, | ||
|
@@ -41,7 +42,7 @@ | |
最后四位:表示闰月的月份,0表示当年无闰月 | ||
|
||
前四位和最后四位应该结合使用,如果最后四位为0,则不考虑前四位 | ||
例: | ||
例: | ||
1901年代码为 19168,转成二进制为 0b100101011100000, 最后四位为0,当年无闰月,月份数据为 010010101110 分别代表12月的大小情况 | ||
1903年代码为 21717,转成二进制为 0b101010011010101,最后四位为5,当年为闰五月,首四位为0,闰月为29天,月份数据为 010101001101 分别代表12月的大小情况 | ||
|
||
|
@@ -93,4 +94,3 @@ | |
''' | ||
从1900年,至2100年每年的农历春节的公历日期 | ||
''' | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里不能这样写,你这个写法会导致newyear_dt是在lunar_year确定之前就已经确定了。
必须要先判断是否是农历正月初一之前,确定 lunar_year是否需要减1,之后,再获取newyear_dt的值
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已修复并补充了测试