Skip to content

Commit e3711bf

Browse files
author
陈云亮
committed
fix(datetime): 修正 GaussDB 时间戳边界判断逻辑
- 引入常量 _GAUSSDB_MIN_MICROS 和 _GAUSSDB_MAX_MICROS 统一时间戳边界 - 替换原硬编码时间戳边界值,提升代码可维护性 - 调整测试用例中的格式和空行,保证代码风格一致 - 修复 GaussDB timestamp 和 timestamptz 类型的边界溢出错误处理逻辑
1 parent 0113b1d commit e3711bf

3 files changed

Lines changed: 10 additions & 5 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ __pycache__/
99
/gaussdb_binary/
1010
.vscode
1111
.venv
12+
myenv
13+
activate_dev.ps1
1214
.coverage
1315
htmlcov
1416
.idea

gaussdb/gaussdb/types/datetime.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,9 +491,9 @@ def load(self, data: Buffer) -> datetime:
491491
except OverflowError:
492492
# GaussDB 边界检查:根据实际的 micros 值判断错误类型
493493
# 年份1: -62135596800000000, 年份9999: ~253402300799999999
494-
if micros < -62135596800000000: # 小于年份1
494+
if micros < self._GAUSSDB_MIN_MICROS: # 小于年份1
495495
raise DataError("timestamp too small (before year 1)") from None
496-
elif micros > 253402300799999999: # 大于年份9999
496+
elif micros > self._GAUSSDB_MAX_MICROS: # 大于年份9999
497497
raise DataError("timestamp too large (after year 9999)") from None
498498
else:
499499
raise DataError("timestamp too large (after year 10K)") from None
@@ -584,6 +584,9 @@ def _load_notimpl(self: TimestamptzLoader, data: Buffer) -> datetime:
584584
class TimestamptzBinaryLoader(Loader):
585585
format = Format.BINARY
586586

587+
# GaussDB 时间戳边界(微秒)
588+
_GAUSSDB_MAX_MICROS = 253402300799999999 # 约9999年底
589+
587590
def __init__(self, oid: int, context: AdaptContext | None = None):
588591
super().__init__(oid, context)
589592
self._timezone = get_tzinfo(self.connection.pgconn if self.connection else None)
@@ -613,7 +616,7 @@ def load(self, data: Buffer) -> datetime:
613616

614617
if micros <= 0:
615618
raise DataError("timestamp too small (before year 1)") from None
616-
elif micros > 253402300799999999: # GaussDB 9999年边界
619+
elif micros > self._GAUSSDB_MAX_MICROS: # GaussDB 9999年边界
617620
raise DataError("timestamp too large (after year 9999)") from None
618621
else:
619622
raise DataError("timestamp too large (after year 10K)") from None

tests/types/test_datetime.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def test_load_date(self, conn, val, expr, fmt_out):
8989
except Exception as e:
9090
pytest.skip(f"Database does not support this date format: {e}")
9191
return
92-
92+
9393
expected = as_date(val)
9494
# GaussDB 可能返回 datetime 而非 date
9595
if isinstance(result, dt.datetime) and not isinstance(expected, dt.datetime):
@@ -147,7 +147,7 @@ def test_load_overflow_message_binary(self, conn, val, msg):
147147
# GaussDB 不支持 infinity 日期
148148
if "infinity" in val.lower():
149149
pytest.skip("GaussDB does not support infinity dates")
150-
150+
151151
try:
152152
cur = conn.cursor(binary=True)
153153
cur.execute("select %s::date", (val,))

0 commit comments

Comments
 (0)