Skip to content

Other

rigour.time

datetime_iso(dt)

Convert a datetime object or string to an ISO 8601 formatted string. If the input is None, it returns None. If the input is a string, it is returned as is. Otherwise, the datetime object is converted to a string in the format 'YYYY-MM-DDTHH:MM:SS'.

Source code in rigour/time.py
def datetime_iso(dt: Optional[Union[str, datetime]]) -> Optional[str]:
    """Convert a datetime object or string to an ISO 8601 formatted string. If the input
    is None, it returns None. If the input is a string, it is returned as is. Otherwise,
    the datetime object is converted to a string in the format 'YYYY-MM-DDTHH:MM:SS'."""
    if dt is None:
        return dt
    try:
        return dt.isoformat(sep="T", timespec="seconds")  # type: ignore
    except AttributeError:
        outvalue = str(dt)
        return outvalue.replace(" ", "T")

iso_datetime(value) cached

Parse datetime from standardized date string. This expects an ISO 8601 formatted string, e.g. '2023-10-01T12:00:00'. Any additional characters after the seconds will be ignored. The string is converted to a datetime object with UTC timezone. This is not designed to parse all possible ISO 8601 formats, but rather a specific convention used in the context of the FollowTheMoney ecosystem.

Source code in rigour/time.py
@lru_cache(maxsize=1000)
def iso_datetime(value: Optional[str]) -> Optional[datetime]:
    """Parse datetime from standardized date string. This expects an ISO 8601 formatted
    string, e.g. '2023-10-01T12:00:00'. Any additional characters after the seconds will
    be ignored. The string is converted to a datetime object with UTC timezone. This is
    not designed to parse all possible ISO 8601 formats, but rather a specific convention
    used in the context of the FollowTheMoney ecosystem."""
    if value is None or len(value) == 0:
        return None
    value = value[:19].replace(" ", "T")
    dt = datetime.strptime(value, "%Y-%m-%dT%H:%M:%S")
    return dt.replace(tzinfo=timezone.utc)

naive_now()

Return the current datetime as a naive datetime.

Source code in rigour/time.py
def naive_now() -> datetime:
    """Return the current datetime as a naive datetime."""
    return datetime.now(timezone.utc).replace(tzinfo=None)

utc_date()

Return the current date in UTC.

Source code in rigour/time.py
def utc_date() -> date:
    """Return the current date in UTC."""
    return utc_now().date()

utc_now()

Return the current datetime in UTC.

Source code in rigour/time.py
6
7
8
def utc_now() -> datetime:
    """Return the current datetime in UTC."""
    return datetime.now(timezone.utc)

rigour.boolean

bool_text(value)

Convert a boolean value to a string representation. If the input is None, it returns None. If the input is True, it returns 't'. If the input is False, it returns 'f'.

Source code in rigour/boolean.py
def bool_text(value: Optional[bool]) -> Optional[str]:
    """Convert a boolean value to a string representation. If the input is None, it returns
    None. If the input is True, it returns 't'. If the input is False, it returns 'f'."""
    if value is None:
        return None
    return "t" if value else "f"

text_bool(text) cached

Convert a string representation of a boolean value to a boolean. If the input is None or an empty string, it returns None.

Source code in rigour/boolean.py
@cache
def text_bool(text: Optional[str]) -> Optional[bool]:
    """Convert a string representation of a boolean value to a boolean. If the input is None
    or an empty string, it returns None."""
    if text is None or len(text) == 0:
        return None
    text = text.lower()
    return text.startswith("t") or text.startswith("y") or text.startswith("1")

rigour.env

env_float(name, default)

Ensure the env returns an float.

Source code in rigour/env.py
def env_float(name: str, default: float) -> float:
    """Ensure the env returns an float."""
    try:
        return float(env.get(name, default))
    except (ValueError, TypeError):
        return default

env_int(name, default)

Ensure the env returns an int.

Source code in rigour/env.py
def env_int(name: str, default: int) -> int:
    """Ensure the env returns an int."""
    try:
        return int(env.get(name, default))
    except (ValueError, TypeError):
        return default

env_str(name, default)

Ensure the env returns a string even on Windows (#100).

Source code in rigour/env.py
6
7
8
9
def env_str(name: str, default: str) -> str:
    """Ensure the env returns a string even on Windows (#100)."""
    value = stringify(env.get(name))
    return default if value is None else value