Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22462

Grzegorz Śliwiński: Creating a timestamp in python - difference between calendar.timegm and time.mktime

$
0
0

If think most of python programmers realise that there are two ways of getting timestamp with only the help of stdlib libraries. It's time.mktime and weirdly placed calendar.timegm.

There are two things people are probably not aware. First one can be attributed due to the fact that their docs are not being read properly, or more often skipped is that time.mktime function treats timetuple as if it were representing date and time in machine local time. This means even if you extract timetuple from your datetime object using utctimetuple method resulting timestamp will still be a given date in your system local time zone. Might not be a problem at all on production, but will be a much bigger issue on your machine, when you pull some data to analyse. calendar.timegm on the other hand, treats all input data as if it were passed in utc.

Speaking of which, none of those two functions accepts a parameter that would tell which timezone the timetuple is in. The only difference is that time.mktime returns a float and calendar.timegm returns an integer.

Second things - timegm function is faster than mktime! See the short benchmarks below:

Read more… (1 min remaining to read)


Viewing all articles
Browse latest Browse all 22462

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>