Differences between revisions 2 and 3
Revision 2 as of 2023-01-24 01:07:45
Size: 820
Editor: PieterSmit
Comment:
Revision 3 as of 2023-01-24 01:09:28
Size: 821
Editor: PieterSmit
Comment:
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
 * 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).  * Python has the concept of time with  or  without timezone(offset-naive), it is mostly better to work with time that contains a timeszone(offset-aware).

Python/DateTime

  • Links: Linux/Bash

  • Python has the concept of time with or 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)