ticktock_utils
一个超级简单、轻量级的 Python 时间工具库。支持拿到当前时间、判断闰年、验证日期,还带贴心报错!
🚀 快速开始(Quick Start)
安装很简单:
pip install ticktock_utils
然后你就可以用了:
import ticktock_utils
# 拿到今天
year, month, day = ticktock_utils.get_ymd()
print(f"今天是 {year} 年 {month} 月 {day} 日")
# 拿到现在
hour, minute, second = ticktock_utils.get_hms()
print(f"现在是 {hour}:{minute}:{second}")
🧠 功能列表
| 功能 | 效果 |
|---|---|
| 语言检测 _detect_language() | 依次查看 TICKTOCK_LANG 环境变量、LANG 和系统 locale,返回 True 表示英文、False 表示中文;这些都没有取到值时默认中文 |
| 全局语言开关 error_is_english / easter_egg_is_english | 模块加载时只检测一次语言,把结果存成全局变量;后续报错文案和彩蛋文案都按它选择中英文 |
| 节日日期缓存 _get_festival_dates(year) | 把春节、元宵、端午、七夕、中元、中秋、重阳、腊八这 8 个农历节日换算成当年的公历 (月, 日),结果由 lru_cache 缓存 |
| 异常类 TimeError / MonthError / DayError | 三级自定义异常:月份不在 1–12 时抛出 MonthError,日期超出当月天数时抛出 DayError |
| 传统节日判断 get_festival() | 把今天转成农历后比对节日表;如果今天是腊月 29 日或 30 日、且次日为正月初一,就判为除夕;没有命中任何节日则返回“现在不是传统节日” |
| 取年月日 get_ymd() | 返回当前的 (年, 月, 日) 元组 |
| 取时分秒 get_hms() | 返回当前的 (时, 分, 秒) 元组 |
| 取当前时间 get_now() | 返回完整的 datetime 对象 |
| 闰年判断 is_leap_year(year) | 按 4、100、400 三条规则返回 True 或 False |
| 当月天数 get_days_in_month(year, month) | 返回 28、29、30 或 31;月份不在 1–12 时按语言抛出 MonthError |
| 日期格式化 format_date(fmt) | 按指定格式输出当前日期,默认格式为 %Y-%m-%d,例如 2026-11-26 |
| 日期合法性校验 validate_date(y, m, d) | 合法返回 True;月份或日期越界时按语言抛出对应异常 |
| 星期几 get_weekday() | 返回 0–6,0 表示周一、6 表示周日 |
| 是否周末 is_weekend() | 周六、周日返回 True |
| 距周末天数 days_until_weekend() | 按 (5 - 今天) % 7 计算,工作日返回距周六的天数,周六返回 0 |
| 当前时段 get_period_of_day() | 按小时返回 早晨、上午、中午、下午、傍晚、深夜 六档之一 |
| 是否过去日期 is_past_date(str) | 传入的 ISO 日期早于今天则返回 True |
| 时间差 time_difference(start, end) | 两个 ISO 时间相减,返回“X天X小时X分钟”,秒数被舍去 |
| 转时间戳 date_to_timestamp(str) | 把 ISO 日期字符串转成整数型 Unix 时间戳 |
| 本月第几周 get_week_of_month() | 1–28 号按 (日-1)//7+1 算出 1–4 周;29 号及以后直接返回“不在计算内”的提示 |
| 休息提醒 rest_reminder(interval, max_times) | 循环休眠,每到时间点打印提醒并弹出 tkinter 消息框;默认每 20 分钟提醒一次、共 5 次 |
| 距整点 countdown_to_next_hour() | 返回“距离下一个整点还有 X 分钟” |
| 日期相差天数 days_between(d1, d2) | 返回 d2−d1 的天数,结果可以为负 |
| 时长可读化 format_duration(seconds) | 把秒数转成“1天2小时3分4秒”这样的文字,为 0 时返回“0秒” |
| 同一天判断 is_same_day(d1, d2) | 两个日期是同一天则返回 True |
| 装哥语录 zhuangge_quote() | 从中英文各 10 条语录中随机返回一条(彩蛋) |
| 相对时间 time_ago(dt_str) | 把过去的时间点转成“X秒前、分钟前、小时前、天前、个月前、年前” |
| 日期倒计时 countdown_to_date(target) | 目标在未来时返回“还有 X 天”,是当天时返回“就是今天”,已经过去时返回“已经过去 X 天” |
| 季节判断 get_season(date_str) | 按 3/20、6/21、9/23、12/21 分界,返回春、夏、秋、冬,默认取今天 |
| 同一周判断 is_same_week(d1, d2) | 按 ISO 周(周一开始)比较,同年同周则返回 True |
| 是否工作日 is_workday() | 周一到周五返回 True |
| 距工作日 days_until_workday() | 工作日返回 0,周六返回 2,周日返回 1 |
| 本周起止 get_week_range(date_str) | 返回所在周的周一和周日两个日期字符串 |
| 下个工作日 next_workday() | 返回下一个工作日的 YYYY-MM-DD,如果今天就是工作日则返回今天 |
| 距指定星期 days_until_specific_weekday(n) | 传入 0–6 后按 (目标-今天)%7 算出天数,越界抛出 ValueError |
| 距月末 days_until_month_end() | 返回距本月最后一天还有几天,最后一天返回 0 |
| 距年末 days_until_year_end() | 返回距 12 月 31 日还有几天 |
| 距下个节日 days_until_next_festival() | 只取今年和明年的节日日期,找出最近的一个并返回相隔天数;没有候选时返回 365 |
| 作者留言 secret_message() | 打印作者留言,周末和工作日各有一套鼓励文案,署名“来自一位四年级的小程序员” |
| 农历转公历 lunar_to_solar(y, m, d) | 把农历日期换算成公历,返回 (年, 月, 日) |
| 生肖 get_zodiac(year) | 按 (year-4) % 12 返回鼠到猪中的一个生肖 |
| 干支常量表 TIANGAN / DIZHI / SHENGXIAO | 10 个天干、12 个地支、12 个生肖三张固定表 |
| 干支 get_ganzhi(year) | 返回两位干支字符串,例如“甲辰” |
| 完整干支 get_ganzhi_full(year) | 返回包含天干、地支、干支、生肖四项的字典 |
| 公历转农历 solar_to_lunar(y, m, d) | 把公历日期换算成农历,返回 (年, 月, 日) |
| 按日期算干支 get_ganzhi_from_date(date) | 先把公历日期转成农历年,再返回该年的干支字典 |
| 节气表 SOLAR_TERMS / SOLAR_TERMS_DICT | 24 节气按公历月排成固定表,另建 (月, 日) 到节气名的哈希表,用于 O(1) 查询 |
| 当日节气 get_solar_term(date) | 直接查哈希表,当天是节气则返回名字,否则返回 None |
| 下个节气 next_solar_term(date) | 在今后两年节气中找最近的一个,返回 (节气名, 还有几天) |
| 全部节气 get_solar_terms() | 返回 24 个 (月, 日, 节气名) 元组 |
| 星座 get_zodiac_sign(date) | 把月日拼成数值后用 bisect 二分定位,返回 12 星座之一 |
| 同月判断 is_same_month(d1, d2) | 同年同月则返回 True |
| 距下月 days_until_next_month() | 返回距下月 1 号还有几天 |
| 博物馆彩蛋 fullstack_museum() | 打印作者另一个作品的链接,并附一句“记得每天保持微笑” |
⚠️ 报错处理
如果你输入了错误的月份或日期,报错也不会让你头大:
import ticktock_utils
try:
ticktock_utils.validate_date(2026, 2, 29) # 2026年是平年,28天
except ticktock_utils.MonthError:
print("月份输入有误!")
except ticktock_utils.DayError:
print("日期超出当月天数了!")
📜 License
MIT
📝 更新日志 (Changelog)
把文档补了一下
Metadata
Release files for ticktock-utils 1.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ticktock_utils-1.5.0.tar.gz | 20.6 kB | Without provenance | Details for ticktock_utils-1.5.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ticktock_utils-1.5.0-py3-none-any.whl | Python 3 | none | any | Details for ticktock_utils-1.5.0-py3-none-any.whl |
Total release size: 36.0 kB
Release files / ticktock_utils-1.5.0.tar.gz
| Download URL | ticktock_utils-1.5.0.tar.gz |
|---|---|
| Size | 20.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fd78de0aa507c63bdd76b7ff91de8bb711ab7f802ec9493d1303813b5d2c4edc
|
|
BLAKE2b-256 checksum How to use checksums |
9cd8b21004bb36330e9cbe68124c85246122d5aa754eb930b6cc7364ffad6675
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|
Release files / ticktock_utils-1.5.0-py3-none-any.whl
| Download URL | ticktock_utils-1.5.0-py3-none-any.whl |
|---|---|
| Size | 15.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c1e5d5a296bb30651eac7b92497917e547cd051bf5d2a8bb1a97053a2b861d1f
|
|
BLAKE2b-256 checksum How to use checksums |
766e7c8de18cd64544f7032f05ac5232a98277b56dd2ffb159902d3f38beb33b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|
Security for ticktock-utils 1.5.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.