"""Standalone offline SQLite lab for Chapter 45: Date & Time functions."""
import json
import sqlite3
def run():
conn = sqlite3.connect(":memory:")
cur = conn.cursor()
cases = []
# Fixed reference timestamp for 100% deterministic offline reproducibility
ref = "2013-05-07 11:27:37"
# 1. Core five functions + unixepoch() (added in SQLite 3.38)
d_val = cur.execute("SELECT date(?)", (ref,)).fetchone()[0]
t_val = cur.execute("SELECT time(?)", (ref,)).fetchone()[0]
dt_val = cur.execute("SELECT datetime(?)", (ref,)).fetchone()[0]
jd_val = round(cur.execute("SELECT julianday('2013-05-07 12:00:00')").fetchone()[0], 4)
sf_val = cur.execute("SELECT strftime('%Y/%m/%d %H:%M:%S %w %j %W %%', ?)", (ref,)).fetchone()[0]
ue_val = cur.execute("SELECT unixepoch(?)", (ref,)).fetchone()[0]
actual_core = {
"date": d_val,
"time": t_val,
"datetime": dt_val,
"julianday_noon": jd_val,
"strftime": sf_val,
"unixepoch": ue_val,
}
exp_core = {
"date": "2013-05-07",
"time": "11:27:37",
"datetime": "2013-05-07 11:27:37",
"julianday_noon": 2456420.0,
"strftime": "2013/05/07 11:27:37 2 127 18 %",
"unixepoch": 1367926057,
}
cases.append({
"name": "five_datetime_functions_and_unixepoch",
"summary": "使用固定基准时间 2013-05-07 11:27:37 验证 date、time、datetime、julianday、strftime 与 unixepoch 输出",
"expected": exp_core,
"actual": actual_core,
"passed": actual_core == exp_core,
})
# 2. Time string formats (valid ISO-8601 forms vs unsupported non-ISO strings)
fmt_results = {
"YYYY-MM-DD": cur.execute("SELECT date('2010-12-30')").fetchone()[0],
"YYYY-MM-DD HH:MM": cur.execute("SELECT datetime('2010-12-30 12:10')").fetchone()[0],
"YYYY-MM-DD HH:MM:SS.SSS": cur.execute("SELECT strftime('%Y-%m-%d %H:%M:%f', '2010-12-30 12:10:04.100')").fetchone()[0],
"YYYY-MM-DDTHH:MM": cur.execute("SELECT datetime('2010-12-30T12:10')").fetchone()[0],
"HH:MM": cur.execute("SELECT time('12:10')").fetchone()[0],
"HH:MM:SS": cur.execute("SELECT time('12:10:01')").fetchone()[0],
"non_iso_mmddyyyy_returns_null": cur.execute("SELECT datetime('12-30-2010 12:10')").fetchone()[0],
"non_iso_compact_returns_null": cur.execute("SELECT datetime('20101230 121001')").fetchone()[0],
}
exp_fmts = {
"YYYY-MM-DD": "2010-12-30",
"YYYY-MM-DD HH:MM": "2010-12-30 12:10:00",
"YYYY-MM-DD HH:MM:SS.SSS": "2010-12-30 12:10:04.100",
"YYYY-MM-DDTHH:MM": "2010-12-30 12:10:00",
"HH:MM": "12:10:00",
"HH:MM:SS": "12:10:01",
"non_iso_mmddyyyy_returns_null": None,
"non_iso_compact_returns_null": None,
}
cases.append({
"name": "timestring_format_validation",
"summary": "验证标准 ISO-8601 时间字符串(含 T 分隔符与毫秒)解析结果,并确认非 ISO 写法返回 NULL",
"expected": exp_fmts,
"actual": fmt_results,
"passed": fmt_results == exp_fmts,
})
# 3. Modifiers & classic calculation examples (deterministic using ref date)
month_end = cur.execute(
"SELECT date(?, 'start of month', '+1 month', '-1 day')", (ref,)
).fetchone()[0]
from_unix = cur.execute("SELECT datetime(1092941466, 'unixepoch')").fetchone()[0]
unix_str = cur.execute("SELECT strftime('%s', ?)", (ref,)).fetchone()[0]
days_since_1776 = round(
cur.execute("SELECT julianday('2013-05-07 12:00:00') - julianday('1776-07-04 12:00:00')").fetchone()[0],
1,
)
secs_since_2004 = cur.execute(
"SELECT strftime('%s', ?) - strftime('%s', '2004-01-01 02:34:56')", (ref,)
).fetchone()[0]
first_tue_oct = cur.execute(
"SELECT date(?, 'start of year', '+9 months', 'weekday 2')", (ref,)
).fetchone()[0]
epoch_secs_frac = round(
cur.execute("SELECT (julianday(?) - 2440587.5) * 86400.0", (ref,)).fetchone()[0],
0,
)
tz_roundtrip = cur.execute(
"SELECT datetime(datetime(?, 'localtime'), 'utc')", (ref,)
).fetchone()[0]
actual_mod = {
"month_end": month_end,
"from_unix_1092941466": from_unix,
"strftime_s": unix_str,
"days_1776_to_2013": days_since_1776,
"secs_2004_to_ref": secs_since_2004,
"first_tuesday_oct_2013": first_tue_oct,
"epoch_secs_from_julianday": epoch_secs_frac,
"localtime_utc_roundtrip": tz_roundtrip,
}
exp_mod = {
"month_end": "2013-05-31",
"from_unix_1092941466": "2004-08-19 18:51:06",
"strftime_s": "1367926057",
"days_1776_to_2013": 86504.0,
"secs_2004_to_ref": 295001561,
"first_tuesday_oct_2013": "2013-10-01",
"epoch_secs_from_julianday": 1367926057.0,
"localtime_utc_roundtrip": ref,
}
cases.append({
"name": "modifiers_and_calendar_calculations",
"summary": "验证月末推算、UNIX 时间戳转换、儒略日天数差、当年 10 月首个周二,以及 localtime/utc 互逆转换",
"expected": exp_mod,
"actual": actual_mod,
"passed": actual_mod == exp_mod,
})
conn.close()
return {
"version": f"CPython 3.12.14 / Native SQLite {sqlite3.sqlite_version}",
"cases": cases,
}
if __name__ == "__main__":
print(json.dumps(run(), ensure_ascii=False, indent=2))
本例需在 Windows x64、CPython 3.12.14、内置 sqlite3(原生 SQLite 3.53.1)本地离线环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、内置 sqlite3(原生 SQLite 3.53.1)本地离线环境。
入口文件:projects/sqlite-date-time-lab/explore-date-time.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-date-time.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、内置 sqlite3(原生 SQLite 3.53.1)本地离线环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1688.zip
课程关联工程包(登记关联51章) · 含当前章节或实例 · mbskycom202612a1680.zip