"""Offline Ruby 3.x semantic verification lab for Chapter 24 (date-time)."""
import datetime as dt
import json
import shutil
VERSION = "CPython 3.12.14 offline Ruby 3.x semantic verifier (MRI ruby absent)"
def case_time_components_and_to_a():
# Ruby Time#to_a order: [sec, min, hour, day, month, year, wday(Sun=0), yday, isdst, zone]
tz_cst = dt.timezone(dt.timedelta(hours=8), name="CST")
t = dt.datetime(2015, 9, 17, 15, 24, 44, 921519, tzinfo=tz_cst)
ruby_wday = (t.weekday() + 1) % 7 # Python Mon=0..Sun=6 -> Ruby Sun=0..Sat=6
yday = t.timetuple().tm_yday
to_a = [t.second, t.minute, t.hour, t.day, t.month, t.year, ruby_wday, yday, False, t.tzname()]
expected = {
"ruby_absent": True,
"inspect": "2015-09-17 15:24:44 +0800",
"year": 2015,
"month": 9,
"day": 17,
"wday": 4,
"yday": 260,
"usec": 921519,
"to_a": [44, 24, 15, 17, 9, 2015, 4, 260, False, "CST"],
}
actual = {
"ruby_absent": shutil.which("ruby") is None,
"inspect": t.strftime("%Y-%m-%d %H:%M:%S %z"),
"year": t.year,
"month": t.month,
"day": t.day,
"wday": ruby_wday,
"yday": yday,
"usec": t.microsecond,
"to_a": to_a,
}
return {
"name": "time_components_and_to_a",
"summary": "核验 Ruby Time 组件读取(年/月/日/星期/年日/微秒)与 to_a 10 元素数组顺序,以及本机无 MRI Ruby 探测",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_utc_local_and_epoch():
tz_cst = dt.timezone(dt.timedelta(hours=8), name="CST")
t_utc = dt.datetime(2008, 7, 8, 9, 10, 11, tzinfo=dt.timezone.utc)
t_local = t_utc.astimezone(tz_cst)
epoch_i = int(t_utc.timestamp())
rebuilt = dt.datetime.fromtimestamp(epoch_i, tz=dt.timezone.utc)
expected = {
"utc_str": "2008-07-08 09:10:11 UTC",
"local_str": "2008-07-08 17:10:11 +0800",
"utc_offset_sec": 0,
"cst_offset_sec": 28800,
"is_utc": True,
"epoch_roundtrip": True,
}
actual = {
"utc_str": t_utc.strftime("%Y-%m-%d %H:%M:%S UTC"),
"local_str": t_local.strftime("%Y-%m-%d %H:%M:%S %z"),
"utc_offset_sec": int(t_utc.utcoffset().total_seconds()),
"cst_offset_sec": int(t_local.utcoffset().total_seconds()),
"is_utc": t_utc.utcoffset() == dt.timedelta(0),
"epoch_roundtrip": rebuilt == t_utc,
}
return {
"name": "utc_local_and_epoch",
"summary": "核验 Time.utc/Time.gm/Time.local 时区偏移、getlocal/getutc 转换与纪元秒数 Time.at 往返一致",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_strftime_directives():
tz_cst = dt.timezone(dt.timedelta(hours=8), name="CST")
t = dt.datetime(2015, 9, 17, 15, 26, 42, tzinfo=tz_cst)
ruby_wday = str((t.weekday() + 1) % 7)
directives = {
"%Y-%m-%d %H:%M:%S": t.strftime("%Y-%m-%d %H:%M:%S"),
"%a": t.strftime("%a"),
"%A": t.strftime("%A"),
"%b": t.strftime("%b"),
"%B": t.strftime("%B"),
"%I %p": t.strftime("%I %p"),
"%j": t.strftime("%j"),
"%U": t.strftime("%U"),
"%W": t.strftime("%W"),
"%w": ruby_wday,
"%y": t.strftime("%y"),
"%Z": t.tzname(),
"%%": t.strftime("%%"),
}
expected = {
"%Y-%m-%d %H:%M:%S": "2015-09-17 15:26:42",
"%a": "Thu",
"%A": "Thursday",
"%b": "Sep",
"%B": "September",
"%I %p": "03 PM",
"%j": "260",
"%U": "37",
"%W": "37",
"%w": "4",
"%y": "15",
"%Z": "CST",
"%%": "%",
}
return {
"name": "strftime_directives",
"summary": "核验 Time#strftime 全部核心格式化指令输出(日期、12/24 小时制、周数、百分号转义)",
"expected": expected,
"actual": directives,
"passed": directives == expected,
}
def case_time_arithmetic_and_offset():
tz_cst = dt.timezone(dt.timedelta(hours=8), name="CST")
now = dt.datetime(2015, 9, 17, 15, 27, 8, tzinfo=tz_cst)
past = now - dt.timedelta(seconds=10)
future = now + dt.timedelta(seconds=10)
diff = (future - now).total_seconds()
expected = {
"past": "2015-09-17 15:26:58 +0800",
"future": "2015-09-17 15:27:18 +0800",
"diff_seconds": 10.0,
}
actual = {
"past": past.strftime("%Y-%m-%d %H:%M:%S %z"),
"future": future.strftime("%Y-%m-%d %H:%M:%S %z"),
"diff_seconds": diff,
}
return {
"name": "time_arithmetic_and_offset",
"summary": "核验 Time 加减秒数返回新 Time、两 Time 相减返回浮点秒数差值的语义",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_time_components_and_to_a(),
case_utc_local_and_epoch(),
case_strftime_directives(),
case_time_arithmetic_and_offset(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装 MRI ruby 解释器)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装 MRI ruby 解释器)。
入口文件:projects/ruby-date-time-lab/explore-date-time.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-date-time.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装 MRI ruby 解释器)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联45章) · 含当前章节或实例 · mbskycom202612a2434.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2446.zip