Why are my two date fields not identical when copied?
I am using django and have two models with model.DateTimeField (). Sometimes I need a copy of the date - but look at this:
>>>myobject.date = datetime.datetime.now()
>>>print myobject.date
>>>2010-04-27 12:10:43.526277
>>>other_object.date_copy = myobject.date
>>>print other_object.date_copy
>>>2010-04-27 12:10:43
Why are these two dates not identical, and how can I make an exact copy of myobject.date?
Edit:
I made a mistake, simplifying the code I was using. The following code will reproduce the oddity:
>>>myobject.date = datetime.datetime.now()
>>>print myobject.date
>>>2010-04-27 12:10:43.526277
>>>myobject.save()
>>>myobject_retrieved_from_db = Myobject.objects.get(id=myobject.id)
>>>other_object.date_copy = myobject_retrieved_from_db.date
>>>print other_object.date_copy
>>>2010-04-27 12:10:43
As Petriborg suggested, the time difference is caused by persisting the database:
>>>print myobject_retrieved_from_db.date
>>>2010-04-27 12:10:43
Mystery solved.
+2
a source to share
1 answer
What version of python are you using?
Seems to work for me ...
In [3]: s = datetime.datetime.now()
In [4]: x = s
In [5]: print s
------> print(s)
2010-04-27 06:37:02.303067
In [6]: print x
------> print(x)
2010-04-27 06:37:02.303067
Do you store datetime in a third party framework like sqldb via django? The temporary structure is usually {int seconds; int fractional_seconds; } or as long milliseconds, it may happen that the second part will be reset either by structure or by casting down ...
+3
a source to share