Get the current time in the UTC timezone. Args: tz_aware: Use this flag to control whether the returned datetime is timezone-aware or timezone-naive. If a datetime is provided, the returned datetime will be timezone-aware if and only if the input date
(tz_aware: Union[bool, datetime] = False)
| 19 | |
| 20 | |
| 21 | def utc_now(tz_aware: Union[bool, datetime] = False) -> datetime: |
| 22 | """Get the current time in the UTC timezone. |
| 23 | |
| 24 | Args: |
| 25 | tz_aware: Use this flag to control whether the returned datetime is |
| 26 | timezone-aware or timezone-naive. If a datetime is provided, the |
| 27 | returned datetime will be timezone-aware if and only if the input |
| 28 | datetime is also timezone-aware. |
| 29 | |
| 30 | Returns: |
| 31 | The current UTC time. If tz_aware is a datetime, the returned datetime |
| 32 | will be timezone-aware only if the input datetime is also timezone-aware. |
| 33 | If tz_aware is a boolean, the returned datetime will be timezone-aware |
| 34 | if True, and timezone-naive if False. |
| 35 | """ |
| 36 | now = datetime.now(timezone.utc) |
| 37 | if ( |
| 38 | isinstance(tz_aware, bool) |
| 39 | and tz_aware is False |
| 40 | or isinstance(tz_aware, datetime) |
| 41 | and tz_aware.tzinfo is None |
| 42 | ): |
| 43 | return now.replace(tzinfo=None) |
| 44 | |
| 45 | return now |
| 46 | |
| 47 | |
| 48 | def utc_now_tz_aware() -> datetime: |