Represents a time instance as hour, minute, second, microsecond.
| 30 | |
| 31 | |
| 32 | class Time(FormattableMixin, time): |
| 33 | """ |
| 34 | Represents a time instance as hour, minute, second, microsecond. |
| 35 | """ |
| 36 | |
| 37 | @classmethod |
| 38 | def instance( |
| 39 | cls, t: time, tz: str | Timezone | FixedTimezone | datetime.tzinfo | None = UTC |
| 40 | ) -> Self: |
| 41 | tz = t.tzinfo or tz |
| 42 | |
| 43 | if tz is not None: |
| 44 | tz = pendulum._safe_timezone(tz) |
| 45 | |
| 46 | return cls(t.hour, t.minute, t.second, t.microsecond, tzinfo=tz, fold=t.fold) |
| 47 | |
| 48 | # String formatting |
| 49 | def __repr__(self) -> str: |
| 50 | us = "" |
| 51 | if self.microsecond: |
| 52 | us = f", {self.microsecond}" |
| 53 | |
| 54 | tzinfo = "" |
| 55 | if self.tzinfo: |
| 56 | tzinfo = f", tzinfo={self.tzinfo!r}" |
| 57 | |
| 58 | return ( |
| 59 | f"{self.__class__.__name__}" |
| 60 | f"({self.hour}, {self.minute}, {self.second}{us}{tzinfo})" |
| 61 | ) |
| 62 | |
| 63 | # Comparisons |
| 64 | |
| 65 | def closest(self, dt1: Time | time, dt2: Time | time) -> Self: |
| 66 | """ |
| 67 | Get the closest time from the instance. |
| 68 | """ |
| 69 | dt1 = self.__class__(dt1.hour, dt1.minute, dt1.second, dt1.microsecond) |
| 70 | dt2 = self.__class__(dt2.hour, dt2.minute, dt2.second, dt2.microsecond) |
| 71 | |
| 72 | if self.diff(dt1).in_seconds() < self.diff(dt2).in_seconds(): |
| 73 | return dt1 |
| 74 | |
| 75 | return dt2 |
| 76 | |
| 77 | def farthest(self, dt1: Time | time, dt2: Time | time) -> Self: |
| 78 | """ |
| 79 | Get the farthest time from the instance. |
| 80 | """ |
| 81 | dt1 = self.__class__(dt1.hour, dt1.minute, dt1.second, dt1.microsecond) |
| 82 | dt2 = self.__class__(dt2.hour, dt2.minute, dt2.second, dt2.microsecond) |
| 83 | |
| 84 | if self.diff(dt1).in_seconds() > self.diff(dt2).in_seconds(): |
| 85 | return dt1 |
| 86 | |
| 87 | return dt2 |
| 88 | |
| 89 | # ADDITIONS AND SUBSTRACTIONS |
no outgoing calls
searching dependent graphs…