title: Serialize date to url safe string in Python
body: <p>I'd like to serialize a Date in python to a URL Safe string.</p>
  
  <p>I remember in C++, I used to just take the integer representing the number of seconds since January 1st 1970 (or something like that).  Then I could turn it into a Base64 url-safe string.  C++ dates were designed to be able to pass these integers around easily.</p>
  
  <p>Ideally in Python, I'd like to get a byte array representing the date, then pass that to base64.urlsafe_b64encode().  Then when I wanted to de-serialize, I could de-encode the bytes and pass it back into a datetime object.  I do not see how to do this in Python though.  </p>
  
  <p>I believe I could use datetime.isoformat(), but the string produced by that seems to be unnecessarily long and I do not need it to be human readable.  I could also write custom functions to do the translation, but I'd like to use official library code if possible.</p>
  
  <p>Am I missing something?  is there an "easy" way to do this that I am not seeing?</p>
  
  <p>Thanks! </p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Alright, so this is what I settled on.  It is a variant of what @bgporter suggested below.  My goal was to turn the datetime information into a url-safe string without taking up too much unnecessary space so I modified the code such that the bytes from the "int" timestamp are being directly base64 url-encoded as opposed to translated into a string of digits (which don't need to be base64 url-encoded).  The resulting timestamp is about 8 characters and looks like this: <code>a7NaTw==</code>:</p>
  
  <p>Encode timestamp (url-safe Base64 string):</p>
  
  <pre><code>url_safe_timestamp = base64.urlsafe_b64encode(struct.pack('L', int(time.time())))
  </code></pre>
  
  <p>Decode timestamp (Date object):</p>
  
  <pre><code>decoded_timestamp = datetime.datetime.fromtimestamp(float(struct.unpack('L', base64.urlsafe_b64decode(url_safe_timestamp))[0]))
  </code></pre>