title: Python: Convert UTC time-tuple to UTC timestamp
body: <p>My problem: I need to convert a UTC time-tuple into a UTC timestamp. But I have some confusions.</p>
  <p>First a bit info:</p>
  <ul>
  <li><p><code>time.mktime(tuple)</code>: this function always returns the timestamp in <strong>local</strong> time.</p>
  <blockquote>
  <p>This is the inverse function of localtime(). Its argument is the struct_time or full 9-tuple which expresses the time in <strong>local time, not UTC.</strong></p>
  </blockquote>
  </li>
  <li><p><code>calendar.timegm(tuple)</code>: this returns the <strong>UTC timestamp</strong> from the supplied time tuple</p>
  <blockquote>
  <p>takes a time tuple such as returned by the gmtime() function in the time module, and returns the corresponding Unix timestamp value. In fact, <strong>time.gmtime() and timegm() are each others’ inverse</strong></p>
  </blockquote>
  </li>
  </ul>
  <p>Now let's do a test:</p>
  <pre><code>&gt;&gt;&gt; from datetime import datetime
  &gt;&gt;&gt; import time
  &gt;&gt;&gt; import calendar as cal
  
  &gt;&gt;&gt; utc_now = datetime.utcnow()
  &gt;&gt;&gt; now = datetime.now()
  &gt;&gt;&gt; utc_now
  datetime.datetime(2013, 3, 16, 9, 17, 22, 489225)
  &gt;&gt;&gt; now
  datetime.datetime(2013, 3, 16, 5, 17, 29, 736903)
  
  &gt;&gt;&gt; time.mktime(datetime.timetuple(utc_now)), time.mktime(datetime.timetuple(now))
  (1363439842.0, 1363425449.0)
  &gt;&gt;&gt; cal.timegm(datetime.timetuple(utc_now)), cal.timegm(datetime.timetuple(now))
  (1363425442, 1363411049)
  </code></pre>
  <p>Why are there four different values? And which one is right when I want to convert a UTC time-tuple into a UTC timestamp?</p>
  <p><em><strong>UPDATTE</strong></em></p>
  <hr />
  <p>I think I have found answers to my confusions, so let me explain.</p>
  <p>First, we need to know something important:</p>
  <blockquote>
  <p>There are two kinds of date and time objects: “naive” and “aware”.</p>
  <p>An aware object has sufficient knowledge of applicable algorithmic and political time adjustments, such as time zone and daylight saving time information, to locate itself relative to other aware objects. An aware object is used to represent a specific moment in time that is not open to interpretation [1].</p>
  <p>A naive object does not contain enough information to unambiguously locate itself relative to other date/time objects. <em><strong>Whether a naive object represents Coordinated Universal Time (UTC), local time, or time in some other timezone is purely up to the program, just like it is up to the program whether a particular number represents metres, miles, or mass.</strong></em> Naive objects are easy to understand and to work with, at the cost of ignoring some aspects of reality.</p>
  </blockquote>
  <p>What we get from <code>datetime.utcnow()</code>  or <code>datetime.now()</code> are &quot;naive&quot; objects. This means that the <code>datetime</code> object that is returned does not, in anyway, say anything about your local time or UTC time -- it just represents &quot;some time&quot;. It just encapsulates date &amp; time information (year, month, day, hour,minutes, seconds, etc.). It is YOUR responsibility to associate it with the notion of local or UTC.</p>
  <p>So, remember that a naive datetime object just represents &quot;some time&quot;. The <code>datetime.now()</code> function returns a &quot;some time&quot; that is equal to your current time, and the <code>datetime.utcnow()</code> function returns &quot;some time&quot; that is the current time in Greenwich England (which is what UTC is).</p>
  <p>The &quot;some time&quot; is just a value for date &amp; time. And note that on different locations on earth, the &quot;some time&quot; occurs on different times. For example, if a &quot;some time&quot; value is January 1, 10:30, than it will be the current time in Greenwich England some 5 hours BEFORE it becomes the current time in New York.</p>
  <p>Hence we can see that there is two things: a generic &quot;some time&quot; value, and the notion that that &quot;some time&quot; becomes current time at different locations at different &quot;times&quot;. (no pun here, read on)</p>
  <p>Now, let's first define what is &quot;epoch&quot;. We know that &quot;some time&quot; is just a generic value of time. Then, the epoch is a &quot;some time&quot; that occurred in Greenwich England where the values of the parameters are: <code>January 1 1970, 00:00:00</code>.</p>
  <p>A &quot;timestamp&quot; is no. of seconds that have elapsed since the epoch. This means that the timestamp was <code>0</code> when the time was <code>Jan 1, 1970, 00:00:00</code> in Greenwich England. But the timestamp was approx. (5 * 60 * 60) when time was <code>Jan 1, 1970, 00:00:00</code> in New York.</p>
  <pre><code>&gt;&gt;&gt; tt = datetime.timetuple(datetime(1970, 1, 1, 0, 0, 0))
  &gt;&gt;&gt; cal.timegm(tt)
  0
  </code></pre>
  <p>Thus, we can see that the same &quot;some time&quot; value of <code>Jan 1, 1970, 00:00:00</code> has different timestamps when we change locations. Hence when you talk about timestamp, you need to also say &quot;what location&quot; is the timestamp related to, and how much eastward or westward that location is related to Greenwich England. That location is expressed as a &quot;timezone&quot;.</p>
  <p>Now, every system (computer) has a timezone configured, and all timestamps related to that timezone effectively become the &quot;local&quot;. The UTC is the global reference.</p>
  <p>So, let's say you have an <code>X</code> value for &quot;some time&quot; which converts to:</p>
  <ul>
  <li><code>Y</code> timestamp in your local time</li>
  <li><code>Z</code> timestamp in UTC</li>
  </ul>
  <p>then this means that <code>Y</code> no. of seconds will have to elapse for &quot;some time&quot; to become the current time in your location and <code>Z</code> no of seconds will have to pass in order for current time in Greenwich England to become &quot;some time&quot;.</p>
  <p>Now, finally, let's come back to our functions <code>mktime</code> and <code>timegm</code>. These take a time-tuple, which is just another representation for &quot;some time&quot;. Remember that we're passing them a naive time which does not have any notion of local or UTC.</p>
  <p>Let's say <code>X</code> is a time-tuple representing a naive &quot;some time&quot;. Then</p>
  <ul>
  <li><code>mktime(X)</code> will return the no. of seconds that will have to elapse in order for your local current time to become that &quot;some time&quot;, and</li>
  <li><code>timegm(X)</code> will return the no of seconds that will have to be spent to make the current time of Greenwich England equal to that &quot;some time&quot;.</li>
  </ul>
  <hr />
  <p>In the example above, <code>now</code> and <code>utc_now</code> represent naive &quot;some time&quot;, and when we feed these &quot;some time&quot; values into <code>mktime</code> and <code>timegm</code>, they simply return the no. of seconds that have to pass for the corresponding locations (your location and Greenwich England) to have their current time be that &quot;some time&quot;.</p>
  <hr />
  <p>Finally, back to my problem: I need to convert a UTC time-tuple into a UTC timestamp.</p>
  <p>Firstly, there is no concept of &quot;UTC time-tuple&quot; -- it's just &quot;some time&quot;. If I need to convert it to UTC, I simply use <code>timegm</code>:</p>
  <pre><code>cal.timegm(datetime.timetuple(utc_now))
  </code></pre>
  <p>which will give me the timestamp for the current UTC time (i.e., the current &quot;some time&quot; in Greenwich England).</p>