title: Write and read Datetime to binary format in Python
body: <p>I want to store a list of datetimes in a binary file in Python.</p>
  
  <p>EDIT: by "binary" I mean the best digital representation for each datatype. The application for this is to save GPS trackpoints composed by (unix-timestamp, latitude, longitude, elevation), so the whole structure is little-endian "Long, float, float, float", with four bytes to each value.</p>
  
  <p>NOTE: I don't use "unix-timestamp" due to any affection to the Unix platform, but only as an unequivocal way to represent the value of a datetime.</p>
  
  <p>Currently, I am doing like the code below, but besides some timezone confusion that I'm still working out (my timezone is -3), I believe converting to <code>int</code> and back might not be the right way, since <code>datetime</code> and <code>datetime64</code> are native types in python/numpy, if I'm not mistaken. Thus, a <code>datetime64</code> would need eight bytes instead of the four I am using for the (long)unix-timestamp.</p>
  
  <pre><code>import datetime
  import calendar
  import struct
  
  now = datetime.datetime.now()
  print now
  
  stamp = calendar.timegm(now.utctimetuple())
  print stamp
  
  binarydatetime = struct.pack('&lt;L', stamp)
  recoverstamp = struct.unpack('&lt;L', binarydatetime)[0]
  print recoverstamp
  
  recovernow = datetime.datetime.fromtimestamp(recoverstamp)
  print recovernow
  </code></pre>
  
  <p>So the main question is: "is this the pythonic way to converting naive datetime to binary and back?"</p>
  
  <p>And the aditional question is: "if everything in this code is supposed to be naive, why do I have a timezone offset?"</p>
  
  <p>Thanks for reading!</p>