When syncing between iOS and my Python GAE backend, I would like to utilise the timestamp for a clean solution.
According to my research this is the best way to create a reliable timestamp:
calendar.timegm((datetime.datetime.now()).utctimetuple())
where I get an integer like this: 1382375236
When on the backend, I would like to additionally save the last_updated
datetime derived from the timestamp. That is human readable and good for a quick check.
def before_put(self):
self.last_updated = datetime.utcfromtimestamp(self.timestamp)
However this fails with an error:
TypeError: a float is required
What is the best way of solving this in an accurate way?
UPDATE:
I also found this suggestion here:
The solution would be dividing it by 1e3
.
In my case this gives me a strange date:
>>> datetime.datetime.utcfromtimestamp(1382375236 / 1e3)
datetime.datetime(1970, 1, 16, 23, 59, 35, 236000)
UPDATE 2
The entire model is:
class Record(ndb.Model):
user = ndb.KeyProperty(kind=User)
record_date = ndb.DateProperty(required=True)
rating = ndb.IntegerProperty(required=True)
notes = ndb.TextProperty()
last_updated = ndb.DateTimeProperty(required=True)
timestamp = ndb.IntegerProperty(required=True)
def __repr__(self):
return '<record_date %r>' % self.record_date
def before_put(self):
self.last_updated = datetime.utcfromtimestamp(self.timestamp)
def after_put(self):
pass
def put(self, **kwargs):
self.before_put()
super(Record, self).put(**kwargs)
self.after_put()