⇤ ← Revision 1 as of 2022-05-02 10:58:23
794
Comment:
|
820
|
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 ==