How do I find "number of seconds since the beginning of the day UTC timezone" in Python? I looked at the docs and didn't understand how to get this using datetime.timedelta
.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Here's one way to do it.
EDIT As suggested, if you want microsecond precision, or potentially crossing a 24-hour period (i.e. delta.days > 0), use
total_seconds()
or the formula given by @unutbu.The number of seconds in a
datetime.timedelta
,x
, is given by timedelta.total_seconds:This function was introduced in Python2.7. For older versions of python, you just have to compute it yourself:
total_seconds = x.days*24*60*60 + x.seconds + x.microseconds/1e6
.for localtime, we can use
time.localtime()
instead oftime.gmtime()