Skip to content

DateTime

Base classes

Name Children Inherits
QDateTime
PySide6.QtCore
QDateTime(self, /) -> None

⋔ Inheritance diagram

graph TD
  259099885["core.DateTime"]
  1312842061["QtCore.QDateTime"]
  37984305["Shiboken.Object"]
  2751680898["builtins.object"]
  1312842061 --> 259099885
  37984305 --> 1312842061
  2751680898 --> 37984305

🛈 DocStrings

Bases: QDateTime

DateTime funcitons.

Source code in prettyqt/core/_datetime.py
class DateTime(core.QDateTime):
    """DateTime funcitons."""

    def __repr__(self):
        super_repr = super().__repr__()
        template = str(super_repr).split("(")[1]
        return f"{type(self).__name__}({template}"

    def __str__(self):
        return self.toString("yyyy-MM-dd hh:mm:ss.zzzzzz")

    def __reduce__(self):
        return type(self), (self.date(), self.time(), self.get_timezone())

    def __format__(self, format_spec: str):
        if format_spec in constants.DATE_FORMAT:
            return self.toString(constants.DATE_FORMAT[format_spec])
        return self.toString(format_spec)

    @classmethod
    def from_seconds(cls, seconds: float) -> Self:
        new = cls()
        new.setMSecsSinceEpoch(int(seconds * 1000))
        return new

    def get_value(self) -> dt.datetime:
        return self.toPython()  # type: ignore

    def get_date(self) -> core.Date:
        return core.Date(self.date())

    def get_time(self) -> core.Time:
        return core.Time(self.time())

    def get_timezone(self) -> core.TimeZone:
        return core.TimeZone(self.timeZone())

    def set_timezone(self, zone: str | core.QTimeZone):
        if isinstance(zone, str):
            self.setTimeZone(core.TimeZone(zone))
        else:
            self.setTimeZone(zone)

    def set_time_spec(self, spec: constants.TimeSpecStr | constants.TimeSpec):
        """Set the time specification.

        Args:
            spec: time specification to use
        """
        time_spec = constants.TIME_SPEC.get_enum_value(spec)
        if time_spec == constants.TimeSpec.UTC:
            self.setTimeZone(core.QTimeZone(core.QTimeZone.Initialization.UTC))
        elif time_spec == constants.TimeSpec.LocalTime:
            self.setTimeZone(core.QTimeZone(core.QTimeZone.Initialization.LocalTime))
        else:
            self.setTimeSpec(time_spec)

    def get_time_spec(self) -> constants.TimeSpecStr:
        """Return current time specification.

        Returns:
            time specification
        """
        return constants.TIME_SPEC.inverse[self.timeSpec()]

    def to_format(self, fmt: constants.DateFormatStr):
        return self.toString(constants.DATE_FORMAT[fmt])

get_time_spec() -> constants.TimeSpecStr

Return current time specification.

Source code in prettyqt/core/_datetime.py
def get_time_spec(self) -> constants.TimeSpecStr:
    """Return current time specification.

    Returns:
        time specification
    """
    return constants.TIME_SPEC.inverse[self.timeSpec()]

set_time_spec(spec: constants.TimeSpecStr | constants.TimeSpec)

Set the time specification.

Parameters:

Name Type Description Default
spec TimeSpecStr | TimeSpec

time specification to use

required
Source code in prettyqt/core/_datetime.py
def set_time_spec(self, spec: constants.TimeSpecStr | constants.TimeSpec):
    """Set the time specification.

    Args:
        spec: time specification to use
    """
    time_spec = constants.TIME_SPEC.get_enum_value(spec)
    if time_spec == constants.TimeSpec.UTC:
        self.setTimeZone(core.QTimeZone(core.QTimeZone.Initialization.UTC))
    elif time_spec == constants.TimeSpec.LocalTime:
        self.setTimeZone(core.QTimeZone(core.QTimeZone.Initialization.LocalTime))
    else:
        self.setTimeSpec(time_spec)

:fa-brands-github: Show source on GitHub