Skip to content

_timestamp

_convert_to_db_timestamp(dt)

This function takes a timezone-aware datetime and converts it to UTC. If using SQLite, it also removes the timezone information in order to make the datetime comparable with datetimes in the database.

Source code in fractal_server/app/routes/aux/_timestamp.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def _convert_to_db_timestamp(dt: datetime) -> datetime:
    """
    This function takes a timezone-aware datetime and converts it to UTC.
    If using SQLite, it also removes the timezone information in order to make
    the datetime comparable with datetimes in the database.
    """
    if dt.tzinfo is None:
        raise HTTPException(
            status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
            detail=f"The timestamp provided has no timezone information: {dt}",
        )
    _dt = dt.astimezone(timezone.utc)
    if Inject(get_settings).DB_ENGINE == "sqlite":
        return _dt.replace(tzinfo=None)
    return _dt