;idZddlZddlZddlZddlmZejdejjZ dZ dZ ej dej ZdZd d ZGd d ejZdS) zHelpers for :mod:`datetime`.N) timestamp_pb2z%Y-%m-%dT%H:%M:%S.%fZz%Y-%m-%dT%H:%M:%Sah (?P \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2} # YYYY-MM-DDTHH:MM:SS ) ( # Optional decimal part \. # decimal point (?P\d{1,9}) # nanoseconds, maybe truncated )? Z # Zulu c<ttj|zS)aConvert timestamp in microseconds since the unix epoch to datetime. Args: value (float): The timestamp to convert, in microseconds. Returns: datetime.datetime: The datetime object equivalent to the timestamp in UTC. ) microseconds) _UTC_EPOCHdatetime timedelta)values JC:\PYTHON\MyICR_Workspace\venv\Lib\site-packages\proto/datetime_helpers.py_from_microsecondsr ,s *>>> >>Tc|s2|j+|d|z }|tS)aConvert a datetime to an RFC3339 timestamp string. Args: value (datetime.datetime): The datetime object to be converted to a string. ignore_zone (bool): If True, then the timezone (if any) of the datetime object is ignored and the datetime is treated as UTC. Returns: str: The RFC3339 formatted string representing the datetime. Ntzinfo)rreplace utcoffsetstrftime_RFC3339_MICROS)r ignore_zones r _to_rfc3339r9sG ?5<3 T **U__->->> >>/ * **r czeZdZdZdZdZfdZedZdZ e dZ dZ e d Z xZS) DatetimeWithNanosecondszuTrack nanosecond in addition to normal datetime attrs. Nanosecond can be passed only as a keyword argument. ) _nanosecondc|dd}|dkrd|vrtd|dz|d<tjj|g|Ri|}|pd|_|S)N nanosecondr microsecond1Specify only one of 'microsecond' or 'nanosecond')pop TypeErrorr__new__r)clsargskwnanosinsts r r zDatetimeWithNanoseconds.__new__Usz|Q'' 199"" STTT % B}  (:t:::r:: :A r cd|v}d|v}|dd}|j}|r|rtd|r|dz|d<tj|i|}|r|jdz|_n|r||_n||_|S)a@Return a date with the same value, except for those parameters given new values by whichever keyword arguments are specified. For example, if d == date(2002, 12, 31), then d.replace(day=26) == date(2002, 12, 26). NOTE: nanosecond and microsecond are mutually exclusive arguments. rrrrr)rrrsuperrrr) selfr"r# ms_provided ns_provided provided_ns prev_nanosr% __class__s r rzDatetimeWithNanoseconds.replace`s$r) "b( ff\1-- _  Q; QOPP P  4 +t 3B} uww+++  *#/$6D    **D   *D  r c$|jp |jdzS)z Read-only: nanosecond precision.r)rr)r(s r rz"DatetimeWithNanoseconds.nanoseconds:4#3d#::r c|jdkrt|St|jddd}d|t|S)zReturn an RFC3339-compliant timestamp. Returns: (str): Timestamp string according to RFC3339 spec. r 0z{}.{}Z)rrstrrjustrstripformatr_RFC3339_NO_FRACTION)r(r$s r rfc3339zDatetimeWithNanoseconds.rfc3339so  q t$$ $D$%%++As33::3??t}}-ABBEJJJr c t|}|-td|tjt j|dt}|d}|d}n'dt|z }t|d|zz}||j |j |j |j|j|j|t jjS) akParse RFC3339-compliant timestamp, preserving nanoseconds. Args: stamp (str): RFC3339 stamp, with up to nanosecond precision Returns: :class:`DatetimeWithNanoseconds`: an instance matching the timestamp string Raises: ValueError: if `stamp` does not match the expected format Nz)Timestamp: {}, does not match pattern: {} no_fractionr$rr0 rr)_RFC3339_NANOSmatch ValueErrorr5patternrstrptimegroupr6lenintyearmonthdayhourminutesecondtimezoneutc)r!stamp with_nanosbarefractionr$scales r from_rfc3339z$DatetimeWithNanoseconds.from_rfc3339s$))%00  ;BB>1   ))   ] + +-A  ##G,,  EEH %EMMRY/Es I J H I K K$(    r c|j|n$|tjj}|t z }t |}|jp |j dz}tj ||S)zReturn a timestamp message. Returns: (:class:`~google.protobuf.timestamp_pb2.Timestamp`): Timestamp message Nrr)secondsr$) rrrrJrKrrC total_secondsrrr Timestamp)r(r%deltarSr$s r timestamp_pbz$DatetimeWithNanoseconds.timestamp_pbs{& DX%6%:;; z!e))++,, ;D$4t$;&weDDDDr c t|jdz}t|}||j|j|j|j|j|j|j tj j S)a&Parse RFC3339-compliant timestamp, preserving nanoseconds. Args: stamp (:class:`~google.protobuf.timestamp_pb2.Timestamp`): timestamp message Returns: :class:`DatetimeWithNanoseconds`: an instance matching the timestamp message g.Ar;) rCrSr rDrErFrGrHrIr$rrJrK)r!rLrrNs r from_timestamp_pbz)DatetimeWithNanoseconds.from_timestamp_pbsj5=3.// !,//s I J H I K K{$(    r )__name__ __module__ __qualname____doc__ __slots__r rpropertyrr7 classmethodrQrWrY __classcell__)r-s@r rrLs !I     D;;X; K K K& & [& PEEE   [     r r)T)r]calendarrregoogle.protobufr fromtimestamprJrKrrr6compileVERBOSEr<r rrr r ris#" ))))))  , ,Q0A0E F F )* J   ? ? ?++++&U U U U U h/U U U U U r