formatting dates for DateTimeField()

274 views
Skip to first unread message

Gonzillaaa

unread,
Sep 8, 2009, 5:26:55 AM9/8/09
to Django users
Hello I'm importing data into django and using its admin interface to
browse and search for data.

Each piece of data is timestamped by me at log time. The docs say data
on a DateTimeField() should be a valid datetime.datetime object but I
am unclear of format (tuple, string etc..)

My model looks like this:

class InfrarredTimes(models.Model):
loc_id = models.ForeignKey(Location)
node_id = models.IntegerField(db_index=True)
pin_id = models.IntegerField(db_index=True)
time_start = models.DateTimeField()
time_end = models.DateTimeField()
time_difference = models.IntegerField(db_index=True)


so to log into that I'm doing this:


def log_status_change( self, node_id, pin_id, status ):
'''logs a change of status for a given node into the
database'''
if __debug__: print "Loggin status change node_id %s, pin_id
%s, status %s" % ( node_id, pin_id, status )
loc = self.get_node_location( node_id )
time_start = self.nodes[node_id]['reading_time_start'][pin_id]
time_end = time.localtime()
#time difference expressed in seconds
time_difference = int( time.mktime( time_end ) - time.mktime
( time_start ) )

print " loc_id %s, node_id %s, pin_id %s, time_start %s,
time_end %s time_difference %s " % ( loc, node_id, pin_id,
time.strftime( '%Y-%m-%d %H:%M:%S', time_start ), time.strftime( '%Y-
%m-%d %H:%M:%S', time_end ), time_difference )

data = InfrarredTimes( loc_id = loc, node_id = node_id, pin_id
= pin_id, \
time_start = time.strftime( '%Y-%m-%d
%H:%M:%S', time_start ), \
time_end = time.strftime( '%Y-%m-%d %H:
%M:%S', time_end ), \
time_difference = time_difference )
data.save()
return True

this is not working at the moment. any pointers much appreciated.


Daniel Roseman

unread,
Sep 8, 2009, 6:18:37 AM9/8/09
to Django users
On Sep 8, 10:26 am, Gonzillaaa <gonzill...@gmail.com> wrote:
> Hello I'm importing data into django and using its admin interface to
> browse and search for data.
>
> Each piece of data is timestamped by me at log time. The docs say data
> on a DateTimeField() should be a valid datetime.datetime object but I
> am unclear of format (tuple, string etc..)

Your question doesn't make sense. As you say, the docs say it should
be a datetime.datetime object. What does format have to do with it?

>        data = InfrarredTimes( loc_id = loc, node_id = node_id, pin_id
> = pin_id, \
>                                time_start = time.strftime( '%Y-%m-%d
> %H:%M:%S', time_start ), \
>                                time_end = time.strftime( '%Y-%m-%d %H:
> %M:%S', time_end ), \
>                                time_difference = time_difference )
>        data.save()
>        return True
>
> this is not working at the moment. any pointers much appreciated.


But you've ignored the bit that you quoted above! Why are you using
time instead of datetime? Why are you converting the time into a
string instead of a datetime object?

time_start = datetime.datetime(2009, 9, 8, 11, 15)
--
DR.

Gonzalo

unread,
Sep 8, 2009, 10:49:25 AM9/8/09
to Django users
Hi Daniel, thanks for your reply apologies for the muddled question.

What I need to do is the following:

I receive a piece of data from the serial port, determine node and
timestamp it.
then when I receive the next piece of date form the same node I
timestamp it and determine the time difference form the previous
reading.

Before using Django I was timestamping data with:

time_start = time.strftime( '%Y-%m-%d %H:%M:%S' )

and computing time difference with:

time_difference = int( time.mktime( time_end ) - time.mktime
( time_start ) )

that didn't work with django because of the format required by
DateTimeField()

now I'm doing:

>>> time_start = datetime.datetime.now()
>>> time_start
datetime.datetime(2009, 9, 8, 15, 46, 9, 544688)
>>> time_end = datetime.datetime.now()
>>> time_end
datetime.datetime(2009, 9, 8, 15, 46, 32, 353081)
>>> time_diff = time_start - time_end
>>> time_diff
datetime.timedelta(-1, 86377, 191607)
>>> print int(time_diff)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string or a number, not
'datetime.timedelta'

how would you compute the time_diff between the two datetime.datetime
objects?

Thank you,

BJ Swope

unread,
Sep 8, 2009, 2:17:05 PM9/8/09
to django...@googlegroups.com

Stop calling int on the return value


>>> import datetime
>>> time_start = datetime.datetime.now()
>>> time_end = datetime.datetime.now()
>>> time_diff = time_end - time_start
>>> time_diff
datetime.timedelta(0, 9, 126708)
>>> print time_diff
0:00:09.126708


--
We are all slave to our own paradigm. -- Joshua Williams

If the letters PhD appear after a person's name, that person will
remain outdoors even after it's started raining. -- Jeff Kay

Gonzalo

unread,
Sep 9, 2009, 4:21:32 AM9/9/09
to Django users
How would you store that into the database, what type of field on the
model I mean? should I store the number of seconds + number of days in
seconds? or the datetime.timedelta() object?

Thanks

Daniel Roseman

unread,
Sep 9, 2009, 5:33:02 AM9/9/09
to Django users
On Sep 9, 9:21 am, Gonzalo <gonzill...@gmail.com> wrote:
> How would you store that into the database, what type of field on the
> model I mean? should I store the number of seconds + number of days in
> seconds? or the datetime.timedelta() object?
>
> Thanks

You can't store a timedelta object in a database. And annoyingly,
there's no simple method to convert a timedelta to seconds. You have
to do it manually:
time_diff_seconds = time_diff.days * 86400 + time_diff.seconds

So now you have a value in seconds you can store in an IntegerField.
--
DR.

Gonzalo

unread,
Sep 9, 2009, 5:47:07 AM9/9/09
to Django users
I thought that was the case, Thanks for your reply.

G.
Reply all
Reply to author
Forward
0 new messages