Code Monkey home page Code Monkey logo

zhdate's Introduction

ZhDate **农历日期处理对象

VersionBadge pythonversion LicenseBadge

不用网络接口直接本地计算**农历,支持农历阳历互转

感谢

EillesWan

更新

  • 修正农历初一前一天错误
  • 修正f 字符串输出方式

安装方法

通过 pip 直接安装

pip install zhdate

或从git拉取

git clone https://github.com/CutePandaSh/zhdate.git
cd zhdate
python setup.py install

更新

pip install zhdate --upgrade

使用方法

见如下代码案例:

from zhdate import ZhDate

date1 = ZhDate(2010, 1, 1) # 新建农历 2010年正月初一 的日期对象
print(date1)  # 直接返回农历日期字符串
dt_date1 = date1.to_datetime() # 农历转换成阳历日期 datetime 类型

dt_date2 = datetime(2010, 2, 6)
date2 = ZhDate.from_datetime(dt_date2) # 从阳历日期转换成农历日期对象

date3 = ZhDate(2020, 4, 30, 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_zhdate4 = ZhDate(2019, 1, 1) + 30 # 加整数返回相隔天数以后的新农历对象

# 中文输出
new_zhdate5 = ZhDate(2019, 1, 1)
print(new_zhdate5.chinese())

# 当天的农历日期
ZhDate.today()

zhdate's People

Contributors

cutepandash avatar eilleswan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

zhdate's Issues

部分日期带有时间部分时农历转换会出现异常

from datetime import datetime
from zhdate import ZhDate

dt_date = datetime(2014,1,30,0,0,1)
date = ZhDate.from_datetime(dt_date) #会抛出异常
date = ZhDate.from_datetime(datetime.combine(dt_date.date(),datetime.min.time())) #必须处理掉时间部分才能正常使用

已发现存在bug的时间:
公历01/30/14
公历02/07/16
公历01/27/17
公历02/15/18
公历01/24/20

期待修正。

__format__ not defined?

请问,方便把__format__() 写一下吗?

print(f"{zh_obj:>16}", flush=True)

这样的语法就会报错

TypeError: unsupported format string passed to ZhDate.format

非常感激

建议 chinese 返回一个对象, 以便用于数据处理

from pydantic import BaseModel

class ZhModel(BaseModel):
    zh_year: str
    zh_month: str
    zh_day: str
    year_tiandi: str
    shengxiao: str

    def __str__(self):
        return f"{self.zh_year}{self.zh_month}{self.zh_day} {self.year_tiandi} ({self.shengxiao}年)"


class ZhDate1():
    def chinese(self):
        ......

        return ZhModel(
            zh_year=zh_year,
            zh_month=zh_month,
            zh_day=zh_day,
            year_tiandi=year_tiandi,
            shengxiao=shengxiao[(self.lunar_year - 1900) % 12],
        )

无法转换2024年2月9日

❯ python3                 
Python 3.11.6 (main, Nov 14 2023, 09:36:21) [GCC 13.2.1 20230801] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from zhdate import ZhDate
>>> ZhDate.today()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../venv/lib/python3.11/site-packages/zhdate/__init__.py", line 164, in today
    return ZhDate.from_datetime(datetime.now())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../venv/lib/python3.11/site-packages/zhdate/__init__.py", line 160, in from_datetime
    return ZhDate(lunar_year, lunar_month, lunar_day, leap_month)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../venv/lib/python3.11/site-packages/zhdate/__init__.py", line 111, in __init__
    raise TypeError('农历日期不支持,超出农历1900年1月1日至2100年12月29日,或日期不存在')
TypeError: 农历日期不支持,超出农历1900年1月1日至2100年12月29日,或日期不存在
❯ pip install --upgrade zhdate
Requirement already satisfied: zhdate in ./venv/lib/python3.11/site-packages (0.1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.