GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - eastyou/moonphase: 月相日历:月度月相、月龄与四相时刻

BranchesTags
Open more actions menu

Latest commit

 

History

2 Commits
2 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
examples
examples
 
 
moonphase
moonphase
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
pyproject.toml
pyproject.toml
 
 

Repository files navigation

moonphase

CI License: MIT Python

一条命令生成 A4 纵向月相日历:月历网格 + 每天月龄与 SVG 月相图标 + 本月四个主要月相时刻表。零第三方依赖,只用 Python 3.9+ 标准库,离线可用,打印即用。

特性

  • A4 纵向单页:周一到周日 7 列,calendar 模块自动处理闰年、月首星期与跨月补空格
  • 每日月相:每格显示日期、月龄(保留 1 位)和按明暗遮挡关系绘制的 SVG 月相图标,不使用 emoji
  • 主要月相高亮:新月、满月当天加中文徽标并给单元格浅色底
  • 四个主要月相时刻:底部表格列出本月新月、上弦月、满月、下弦月(UTC,精确到小时)
  • 自包含页面:HTML/CSS/SVG 全部内联,无网络资源;屏幕自适应、不横向溢出,打印清晰
  • 严格校验:年份 20002100、月份 112、标题 1~40 字;参数错误中文提示,退出码区分参数错误与 IO 失败
  • 114 个单元测试:覆盖天文计算、闰年、月首星期、页面格数、HTML 转义与命令行退出码

安装

pip install moonphase

也可以直接下载源码,在项目目录运行:

python -m moonphase --help

用法

python -m moonphase --year 2026 --month 11 --title 十一月月相 -o examples/月相日历.html

参数

参数 说明 默认
--year 年份,范围 2000~2100 必填
--month 月份,范围 1~12 必填
--title 页面标题,1~40 个字符 YYYY 年 M 月月相
-o, --output 输出 HTML 路径 月相日历.html

退出码:0 成功;2 参数不合法(含中文错误信息);1 文件读写失败。

作为库使用

from datetime import datetime, timezone
from moonphase import moon_age, illumination, phase_name, month_phase_events

when = datetime(2026, 11, 9, 6, 0, tzinfo=timezone.utc)
age = moon_age(when)          # 0~29.53 天
light = illumination(age)     # 0~1
name = phase_name(age)        # 新月 / 娥眉月 / ... / 残月
for event in month_phase_events(2026, 11):
    print(event.name, event.when.isoformat())

算法与误差

  1. 以 2000-01-06 18:14 UTC 的真实新月为历元,叠加平均朔望月 29.530588853 天得到平均月龄。
  2. 再叠加月球轨道的四项主要角参数修正:月球平近点角 M'、太阳平近点角 M、升交点角距 F、平距角 D。新月、满月、上下弦分别使用 Meeus《Astronomical Algorithms》简化公式中的对应系数,并按历元校准, 使历元处月龄恰为 0。
  3. 月龄取模到 0~29.53;照明度用 (1 - cos(2π·age/29.530588853)) / 2; 八个月相按等宽区间命名,新月与满月各包含前后约 1.85 天。
  4. 本月四个主要月相时刻用二分法求解相位方程,精确到秒以下,页面按整点展示。

误差:2000~2100 年新月与满月时刻与公开数据通常相差 1 小时以内, 最坏也在 1 天以内,明显优于需求的 1 天容差。2024-04-08 日全食附近为新月、 2026-01-03 为满月,均已在测试中覆盖。页面与 README 均声明这是近似值, 不用于精密天文定轨或航天计算。

所有计算与页面展示均为 UTC(协调世界时);日期格中的月龄取当日 12:00 UTC 的值。

打印

用浏览器打开生成的 HTML,按 Ctrl/Cmd + P:

  • 纸张:A4 纵向
  • 边距:无 / 默认
  • 勾选「背景图形」,新月/满月的浅色底与徽标才会打印
  • 页面已按 A4 单页排版,打印预览应为 1 页

项目结构

moonphase/
├── moonphase/
│   ├── __init__.py   # 公开 API
│   ├── __main__.py   # python -m moonphase
│   ├── model.py      # 月龄、照明度、八相命名、月历与月相时刻
│   ├── render.py     # SVG 月相图标与 A4 纵向页面
│   └── cli.py        # 参数校验、中文报错与退出码
├── tests/            # 114 个 unittest 测试
├── examples/         # 月相日历.html 示例
├── .github/workflows/ci.yml
├── pyproject.toml
└── LICENSE

开发

# 全部单元测试
python -m unittest discover -s tests -v

# 只跑某个模块
python -m unittest tests.test_model -v

# 生成示例页面
python -m moonphase --year 2026 --month 11 --title 十一月月相 -o examples/月相日历.html

CI 在 Ubuntu / macOS / Windows × Python 3.10 / 3.13,以及 Ubuntu 22.04 × Python 3.9 上运行单元测试,并在测试后执行命令行冒烟与内容断言。

许可证

MIT © 2026 eastyou

About

月相日历:月度月相、月龄与四相时刻

Topics

astronomycalendarclieducationmoonprintpython

Resources

Readme
MIT license
Activity

Stars

0 stars

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.