Skip to main content

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}")

🧠 功能列表

函数名 说明
get_ymd() 获取当前的年月日,返回 (年, 月, 日)
get_hms() 获取当前的时分秒,返回 (时, 分, 秒)
get_now() 获取完整的日期时间对象
is_leap_year(year) 判断某年是否为闰年
get_days_in_month(year, month) 判断某年某月有多少天
format_date(fmt) 任意格式输出当前日期
validate_date(year, month, day) 检查一个日期是否合法
get_weekday() 返回今天是星期几(0=周一,6=周日)
is_weekend() 判断今天是否为周末(周六或周日)
days_until_weekend() 计算距离下一个周末还有几天
is_daytime() 判断当前时间是白天还是黑夜
get_period_of_day() 返回当前时间段(早晨、上午、中午、下午、傍晚、深夜)
secret_message() 打印作者的一句话(彩蛋)
date_to_timestamp() 将日期转换为 Unix 时间戳
is_past_date() 判断目标日期是否已经过去
time_difference() 计算两个时间之间的差

⚠️ 报错处理

如果你输入了错误的月份或日期,报错也不会让你头大

import ticktock_utils

try:
    ticktock_utils.validate_date(2026, 2, 29)  # 2026年是平年,28天
except ticktock_utils.MonthError:
    print("月份输入有误!")
except ticktock_utils.DayError:
    print("日期超出当月天数了!")

更新日志🗓

更新了days_between计算两个日期相隔时间,format_duration把秒数转换成比如x年x月x日x时x分x秒这样的格式

📜 License

MIT

Release files for ticktock-utils 0.8.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ticktock-utils 0.8.0
File Size Uploaded
ticktock_utils-0.8.0.tar.gz 10.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ticktock-utils 0.8.0
File Interpreter ABI Platform
ticktock_utils-0.8.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.7 kB

Release files / ticktock_utils-0.8.0.tar.gz

Download URL ticktock_utils-0.8.0.tar.gz
Size 10.1 kB
Tags Source
SHA-256 checksum
How to use checksums
b700f24d9a84f983d3f0fbf3e516983987f3f26f18dcd8ad5627366b91035645
BLAKE2b-256 checksum
How to use checksums
ec4bb951ca02c4c0ce91bd793be4fc577cb9be270f49891c8512a3a615f673f7
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-0.8.0-py3-none-any.whl

Download URL ticktock_utils-0.8.0-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bb090bc9fb8f1a1c30e4a724d17dd144c9c56ef742d77522ceca76e1f6824d34
BLAKE2b-256 checksum
How to use checksums
4404bc5a74fd1e783fea98c5e8efbd0c5613241e51aa39bc09b83bfbd12e20a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release history Release notifications | RSS feed

This release

0.8.0 This release

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page