Files
marqdo-std-system/modules/datetime.mq.md
cflmy 053ddb0e59 migrate: own clock/datetime/env/process/random sources and baseline assertions
Move the official stdlib system modules to their unique source authority without
changing public std: paths or host authorization. Carry the pre-migration product
assertions into component cases captured from the baseline Bundle; the process
case resolves the granted program and expected stdout from the verified CLI.

Refs cflmy/marqdo#9, cflmy/marqdo-std-system#1
2026-11-26 23:30:25 +08:00

341 lines
4.8 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# @exec datetime
## 失败
`params(消息)`
`return {ok: false, code: "datetime.invalid", message: 消息}`
## 闰年
`params(年)`
`return 年 % 4 == 0 and (年 % 100 != 0 or 年 % 400 == 0)`
## 每月天数
`params(年, 月)`
`if 月 == 2`
`if 闰年(年)`
`return 29`
`end`
`return 28`
`else`
`if 月 == 4 or 月 == 6 or 月 == 9 or 月 == 11`
`return 30`
`end`
`end`
`return 31`
## 补零2
`params(值)`
`if 值 < 10`
`return "0" + 文本(值)`
`end`
`return 文本(值)`
## 补零3
`params(值)`
`if 值 < 10`
`return "00" + 文本(值)`
`else`
`if 值 < 100`
`return "0" + 文本(值)`
`end`
`end`
`return 文本(值)`
## 补零4
`params(值)`
`if 值 < 10`
`return "000" + 文本(值)`
`else`
`if 值 < 100`
`return "00" + 文本(值)`
`else`
`if 值 < 1000`
`return "0" + 文本(值)`
`end`
`end`
`end`
`return 文本(值)`
## 月份
`params(mp)`
`if mp < 10`
`return mp + 3`
`end`
`return mp - 9`
## 年份
`params(年, 月)`
`if 月 <= 2`
`return 年 + 1`
`end`
`return 年`
## 日期部件
`params(天)`
Howard Hinnant 的 civil-from-days 算法,输入为 1970-01-01 起的天数。
**z = 天 + 719468**
**era = z // 146097**
**doe = z - era * 146097**
**yoe = (doe - doe // 1460 + doe // 36524 - doe // 146096) // 365**
**年 = yoe + era * 400**
**doy = doe - (365 * yoe + yoe // 4 - yoe // 100)**
**mp = (5 * doy + 2) // 153**
**日 = doy - (153 * mp + 2) // 5 + 1**
**月 = 月份(mp)**
`return {年: 年份(年, 月), 月: 月, 日: 日}`
## 日期序号
`params(年, 月, 日)`
**y = 年**
`if 月 <= 2`
**y = y - 1**
`else`
`if true`
**忽略 = 0**
`end`
`end`
**era = y // 400**
**yoe = y - era * 400**
**m = 月**
`if 月 > 2`
**m = 月 - 3**
`else`
`if true`
**m = 月 + 9**
`end`
`end`
**doy = (153 * m + 2) // 5 + 日 - 1**
**doe = yoe * 365 + yoe // 4 - yoe // 100 + doy**
`return era * 146097 + doe - 719468`
## 是数字串
`params(文本)`
**点 = text.codepoints(文本)**
**结果 = 长度(点) > 0**
**下标 = 0**
`while 下标 < 长度(点) and 结果`
`if 点[下标] < 48 or 点[下标] > 57`
**结果 = false**
`else`
`if true`
**忽略 = 0**
`end`
`end`
**下标 = 下标 + 1**
`end`
`return 结果`
## 片段
`params(点, 起点, 终点)`
**结果 = ""**
**下标 = 起点**
`while 下标 < 终点`
**结果 = 结果 + text.from_codepoint(点[下标])**
**下标 = 下标 + 1**
`end`
`return 结果`
## 格式化
`params(毫秒)`
输出 UTC ISO-8601:YYYY-MM-DDTHH:MM:SSZ;毫秒非零时追加
.mmm。负数毫秒按 1970 前的 UTC 时间处理。
**秒 = 毫秒 // 1000**
**毫 = 毫秒 % 1000**
**天 = 秒 // 86400**
**日内 = 秒 % 86400**
**时 = 日内 // 3600**
**分 = 日内 % 3600 // 60**
**秒数 = 日内 % 60**
**日期 = 日期部件(天)**
**基 = 补零4(日期.年) + "-" + 补零2(日期.月) + "-" + 补零2(日期.日) + "T" + 补零2(时) + ":" + 补零2(分) + ":" + 补零2(秒数)**
`if 毫 == 0`
`return 基 + "Z"`
`end`
`return 基 + "." + 补零3(毫) + "Z"`
## 解析
`params(文本)`
解析 YYYY-MM-DDTHH:MM:SSZ 或带 .mmm 的 UTC 文本,返回 Unix 毫秒。
范围、闰年和格式错误都是可恢复 datetime.invalid。
**点 = text.codepoints(文本)**
`if 长度(点) < 20 or 长度(点) > 24`
`return 失败("长度不是 20–24 个码点")`
`else`
`if 点[4] != 45 or 点[7] != 45 or 点[10] != 84 or 点[13] != 58 or 点[16] != 58`
`return 失败("分隔符不符合 YYYY-MM-DDTHH:MM:SS")`
`end`
`end`
**年文本 = 片段(点, 0, 4)**
**月文本 = 片段(点, 5, 7)**
**日文本 = 片段(点, 8, 10)**
**时文本 = 片段(点, 11, 13)**
**分文本 = 片段(点, 14, 16)**
**秒文本 = 片段(点, 17, 19)**
`if not 是数字串(年文本) or not 是数字串(月文本) or not 是数字串(日文本) or not 是数字串(时文本) or not 是数字串(分文本) or not 是数字串(秒文本)`
`return 失败("日期时间分量必须是数字")`
`end`
**年 = 整数(年文本)**
**月 = 整数(月文本)**
**日 = 整数(日文本)**
**时 = 整数(时文本)**
**分 = 整数(分文本)**
**秒 = 整数(秒文本)**
**毫文本 = "0"**
**结束 = 20**
`if 长度(点) >= 24 and 点[19] == 46 and 点[23] == 90`
**毫文本 = 片段(点, 20, 23)**
**结束 = 24**
`else`
`if 长度(点) != 结束 or 点[结束 - 1] != 90`
`return 失败("必须以 Z 结尾")`
`else`
`if not 是数字串(毫文本)`
`return 失败("毫秒必须是数字")`
`end`
`end`
`end`
**毫 = 整数(毫文本)**
`if 年 < 1970 or 月 < 1 or 月 > 12 or 日 < 1 or 日 > 每月天数(年, 月)`
`return 失败("日期超出范围")`
`else`
`if 时 < 0 or 时 > 23 or 分 < 0 or 分 > 59 or 秒 < 0 or 秒 > 59`
`return 失败("时间超出范围")`
`end`
`end`
**毫秒 = 日期序号(年, 月, 日) * 86400000 + 时 * 3600000 + 分 * 60000 + 秒 * 1000 + 毫**
`return {ok: true, value: 毫秒}`