Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're going to store it as a <code>datetime</code> (which I agree is a good idea), I'd extend the <code>DateTimeProperty</code> - then you get various bits of parsing and validation for free.</p> <p>Also, storing as <code>timedelta</code> as a <code>datetime</code> can be much easier than the other methods given here, by storing it as a datetime some distance from a reference datetime, such that the difference represents the timedelta. This is really easy thanks to the operator overloading the datetime module gives us.</p> <pre><code>from datetime import datetime, timedelta from google.appengine.ext import db class TimeDeltaProperty(db.DateTimeProperty): # Use a reference datetime half way between the min and max possible # datetimes, so that we can support both +ve and -ve timedeltas ref_datetime = (datetime.max - datetime.min) / 2 + datetime.min def get_value_for_datastore(self, model_instance): # Get the timedelta instance assigned to this property td = super(TimeDeltaProperty, self).get_value_for_datastore(model_instance) if td is not None: # datetime + timedelta = datetime return self.ref_datetime + td def make_value_from_datastore(self, dt): if dt is not None: # datetime - datetime = timedelta return dt - self.ref_datetime </code></pre> <p>And here's an equivalent implementation for the <a href="https://developers.google.com/appengine/docs/python/ndb/" rel="nofollow">NDB API</a>, if you're that way inclined:</p> <pre><code>from datetime import datetime, timedelta from google.appengine.ext import ndb class TimeDeltaProperty(ndb.DateTimeProperty): # Use a reference datetime half way between the min and max possible # datetimes, so that we can support both +ve and -ve timedeltas ref_datetime = (datetime.max - datetime.min) / 2 + datetime.min def _validate(self, value): if not isinstance(value, timedelta): raise TypeError('expected a datetime.timedelta, got %r' % value) def _to_base_type(self, value): # datetime + timedelta = datetime return self.ref_datetime + td def _from_base_type(self, value): # datetime - datetime = timedelta return dt - self.ref_datetime </code></pre> <h2>Accuracy</h2> <p>A <code>timedelta</code> in Python can handle deltas of roughly +/-2.7 million years. However, a <code>datetime</code> only covers a range of about 10,000 years. To store a greater timedelta in a datetime, you'll have to do some shifting and sacrifice some accuracy.</p> <p>The approach above limits timedeltas to half this range - about +/-5000 years, because of the choice of reference datetime.</p> <p>If you know your timedelta will always be positive, you can use <code>ref_datetime = datetime.min</code> (or if you know it'll always be negative you can use <code>ref_datetime = datetime.max</code>) to get the full range of about 10,000 years.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload