Serialize a datetime object to ISO format string using the user's timezone. This ensures the frontend receives dates with the correct offset for display.
(self, dt: datetime | None)
| 193 | return None |
| 194 | |
| 195 | def serialize_datetime(self, dt: datetime | None) -> str | None: |
| 196 | """ |
| 197 | Serialize a datetime object to ISO format string using the user's timezone. |
| 198 | This ensures the frontend receives dates with the correct offset for display. |
| 199 | """ |
| 200 | if dt is None: |
| 201 | return None |
| 202 | |
| 203 | # At this point, dt is definitely not None |
| 204 | assert dt is not None |
| 205 | |
| 206 | try: |
| 207 | # Ensure datetime is timezone aware (if not, assume the user's timezone) |
| 208 | if dt.tzinfo is None: |
| 209 | dt = self.localize_naive_datetime(dt) |
| 210 | |
| 211 | local_dt = dt.astimezone(self.get_tzinfo()) |
| 212 | return local_dt.isoformat() |
| 213 | except Exception as e: |
| 214 | PrintStyle.error(f"Error serializing datetime: {e}") |
| 215 | return None |
no test coverage detected