from datetime import datetime


def datetime2str(date_time: datetime, use_time: bool = False) -> str:
    """Convert a datetime object to a DICOM compliant date(time) string.

    Parameters
    ----------
    date_time : datetime
        The datetime object to convert ot a DICOM string.
    use_time : bool
        If False, the default, don't add the time component in the
        return value. Note that this has no impact if the datetime
        component has an existing time component.

    Returns
    -------
    str
        The datetime object as a DICOM string.
    """
    omit_time = (
        date_time.hour == 0
        and date_time.minute == 0
        and date_time.second == 0
        and date_time.microsecond == 0
    )

    if use_time or not omit_time:
        if date_time.tzinfo is not None:
            return date_time.strftime("%Y%m%d%H%M%S.%f%z")
        return date_time.strftime("%Y%m%d%H%M%S.%f")
    return date_time.strftime("%Y%m%d")