# Copyright 2023 Flower Labs GmbH. All Rights Reserved.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.# =============================================================================="""Flower date utils."""importdatetime
[docs]defnow()->datetime.datetime:"""Construct a datetime from time.time() with time zone set to UTC."""returndatetime.datetime.now(tz=datetime.timezone.utc)
defformat_timedelta(td:datetime.timedelta)->str:"""Format a timedelta as a string."""days=td.dayshours,remainder=divmod(td.seconds,3600)minutes,seconds=divmod(remainder,60)ifdays>0:returnf"{days}d {hours:02}:{minutes:02}:{seconds:02}"returnf"{hours:02}:{minutes:02}:{seconds:02}"defisoformat8601_utc(dt:datetime.datetime)->str:"""Return the datetime formatted as an ISO 8601 string with a trailing 'Z'."""ifdt.tzinfo!=datetime.timezone.utc:raiseValueError("Expected datetime with timezone set to UTC")returndt.isoformat(timespec="seconds").replace("+00:00","Z")