Storing UTC Dates

267 views
Skip to first unread message

Heston James - Cold Beans

unread,
Aug 28, 2008, 7:21:43 AM8/28/08
to sqlal...@googlegroups.com

Hello Guys,

 

This might seem like a bit of a naive question but I’m looking for your advice. Being from the UK we operate on Daylight Savings Time which gives us a one hour offset on times for a few months of the year.

 

I currently have a DateTime column which is declared like so:

 

    created = Column(DateTime, default=func.now())

    modified = Column(DateTime, default=func.now(), onupdate=func.now())

 

Which generally works very well, when I create a record it inserts the current locale time into the column, however, it stores the datetime with DST applied too it. As I use the datetime at a later point for posting over web services I really need to store the UTC version of now() in the database, without DST applied to it.

 

How can I modify the above column definition to do this? Can I simply use something instead of func.now()? I was given the advise to use func.now() by someone but not really sure what it returns, is it a datetime.datetime object? Or a time tuple?

 

Or is there a parameter I can pass to Column() or DateTime() which will ensure it uses the UTC format of the date when creating and modifying records?

 

Many thanks guys,

 

Heston

Werner F. Bruhin

unread,
Aug 28, 2008, 8:09:30 AM8/28/08
to sqlal...@googlegroups.com
Heston,

IIUC func.now is a database function.

You should be able to use datetime instead i.e.:

created = Column(DateTime, default=datetime.datetime.utcnow)

modified = Column(DateTime, default=datetime.datetime.utcnow,
onupdate=datetime.datetime.utcnow)


Werner

Heston James - Cold Beans

unread,
Aug 28, 2008, 8:47:44 AM8/28/08
to sqlal...@googlegroups.com
Hi Werner,

> IIUC func.now is a database function.

Ah, ok, that makes fair sense.

> You should be able to use datetime instead i.e.:
>
> created = Column(DateTime, default=datetime.datetime.utcnow)
>
> modified = Column(DateTime, default=datetime.datetime.utcnow,
> onupdate=datetime.datetime.utcnow)

Yes, this worked just great Werner, I've used that and it seems to have done
the job! I hoped it would be that simple :-)

Thanks again,

Heston

Reply all
Reply to author
Forward
0 new messages