python's time module seems a little haphazard. For example, here is a list of methods in there, from the docstring:
time() -- return current time in seconds since the Epoch as a float clock() -- return CPU time since process start as a float sleep() -- delay for a number of seconds given as a float gmtime() -- convert seconds since Epoch to UTC tuple localtime() -- convert seconds since Epoch to local time tuple asctime() -- convert time tuple to string ctime() -- convert time in seconds to string mktime() -- convert local time tuple to seconds since Epoch strftime() -- convert time tuple to string according to format specification strptime() -- parse string to time tuple according to format specification tzset() -- change the local timezone
Looking at localtime() and its inverse mktime(), why is there no inverse for gmtime() ?
Bonus questions: what would you name the method ? How would you implement it ?
Python time gmtime() Method Pythom time method gmtime() converts a time expressed in seconds since the epoch to a struct_time in UTC in which the dst flag is always zero. If secs is not provided or None, the current time as returned by time() is used.
strftime() function converts a tuple or struct_time representing a time as returned by gmtime() or localtime() to a string as specified by the format argument.
There is actually an inverse function, but for some bizarre reason, it's in the calendar module: calendar.timegm(). I listed the functions in this answer.
I always thought the time and datetime modules were a little incoherent. Anyways, here's the inverse of mktime
import time def mkgmtime(t): """Convert UTC tuple to seconds since Epoch""" return time.mktime(t)-time.timezone
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With