from dataclasses import dataclass
from datetime import datetime, UTC
from typing import Optional
from adaptix.conversion import get_converter, coercer
@dataclass
class X:
a: Optional[datetime]
@dataclass
class Y:
a: Optional[datetime]
x_to_y = get_converter(
X, Y, recipe=[coercer(datetime, datetime, lambda x: x.replace(tzinfo=UTC))]
)
print(x_to_y(X(datetime.now())))
This code prints Y(a=datetime.datetime(2024, 6, 1, 19, 33, 17, 289641)) with no timezone
This code prints
Y(a=datetime.datetime(2024, 6, 1, 19, 33, 17, 289641))
with no timezone