Differences between revisions 1 and 2
Revision 1 as of 2022-05-02 10:58:23
Size: 794
Editor: PieterSmit
Comment:
Revision 2 as of 2023-01-24 01:07:45
Size: 820
Editor: PieterSmit
Comment:
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
 * Links: [[Linux/Bash]]

Python/DateTime

  • Links: Linux/Bash

  • Python has the concept of time with our without timezone(offset-naive), it is mostly better to work with time that contains a timeszone(offset-aware).

How to generate offest-aware timestamp

  • Add timezone to datetime.now. (timezone-aware)
    • Wrong  t = datetime.datetime.now() 

    • Better  t = datetime.datetime.now( datetime.timezone.utc ) 

Calculate seconds from time difference

  • e.g.

    td = t1 - t2
    td_seconds = td.days * 24 * 60 * 60 + td.seconds + td.microseconds / 1000000

time string with timezone

import datetime
tz = datetime.timezone.utc
ft = "%Y-%m-%dT%H:%M:%S%z"
t = datetime.datetime.now(tz=tz).strftime(ft)
print(f"== Summary {t} ==")

== Summary 2022-05-19T22:48:17+0000 ==

Python/DateTime (last edited 2023-01-24 01:09:28 by PieterSmit)