Allow reading files with invalid dates
Notably, KLayout writes invalid dates when told to not write the date
This commit is contained in:
parent
97527a5948
commit
46b3349935
@ -3,12 +3,16 @@ Functionality for encoding/decoding basic datatypes
|
||||
"""
|
||||
from typing import Sequence, IO
|
||||
import struct
|
||||
import logging
|
||||
from datetime import datetime
|
||||
|
||||
import numpy
|
||||
from numpy.typing import NDArray
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class KlamathError(Exception):
|
||||
pass
|
||||
|
||||
@ -70,7 +74,12 @@ def parse_datetime(data: bytes) -> list[datetime]:
|
||||
dts = []
|
||||
for ii in range(0, len(data), 12):
|
||||
year, *date_parts = parse_int2(data[ii:ii + 12])
|
||||
dts.append(datetime(year + 1900, *date_parts))
|
||||
try:
|
||||
dt = datetime(year + 1900, *date_parts)
|
||||
except ValueError as err:
|
||||
dt = datetime(1900, 1, 1, 0, 0, 0)
|
||||
logger.warning(f'Invalid date {[year] + date_parts}, setting {dt} instead')
|
||||
dts.append(dt)
|
||||
return dts
|
||||
|
||||
|
||||
|
@ -2,11 +2,12 @@ import struct
|
||||
|
||||
import pytest # type: ignore
|
||||
import numpy
|
||||
from datetime import datetime
|
||||
from numpy.testing import assert_array_equal
|
||||
|
||||
from .basic import parse_bitarray, parse_int2, parse_int4, parse_real8, parse_ascii
|
||||
from .basic import pack_bitarray, pack_int2, pack_int4, pack_real8, pack_ascii
|
||||
from .basic import decode_real8, encode_real8
|
||||
from .basic import decode_real8, encode_real8, parse_datetime
|
||||
|
||||
from .basic import KlamathError
|
||||
|
||||
@ -117,3 +118,14 @@ def test_pack_real8():
|
||||
def test_pack_ascii():
|
||||
assert pack_ascii(b'4321') == b'4321'
|
||||
assert pack_ascii(b'321') == b'321\0'
|
||||
|
||||
|
||||
def test_invalid_date():
|
||||
default = [datetime(1900, 1, 1, 0, 0, 0)]
|
||||
assert parse_datetime(pack_int2((0, 0, 0, 0, 0, 0))) == default
|
||||
assert parse_datetime(pack_int2((0, 1, 32, 0, 0, 0))) == default
|
||||
assert parse_datetime(pack_int2((0, 2, 30, 0, 0, 0))) == default
|
||||
assert parse_datetime(pack_int2((0, 1, 1, 24, 0, 0))) == default
|
||||
assert parse_datetime(pack_int2((0, 1, 1, 25, 0, 0))) == default
|
||||
assert parse_datetime(pack_int2((0, 1, 1, 0, 61, 0))) == default
|
||||
assert parse_datetime(pack_int2((0, 1, 1, 0, 0, 61))) == default
|
||||
|
Loading…
Reference in New Issue
Block a user