List Info

Thread: DateTimeField auto_now / auto_now_add and UTC




DateTimeField auto_now / auto_now_add and UTC
user name
2007-10-24 10:16:58
I'm looking to store UTC date/times so I don't have to worry
about
time zones until I localize the display.  From looking at
the code in
DateTimeField I think I've answered my own question, but I
just want
to ask here for the sake of conversation. Is there a way to
get the
auto_now/auto_now_add feature of DateTimeField to use UTC?

        if self.auto_now or (self.auto_now_add and add):
            value = datetime.datetime.now()
            setattr(model_instance, self.attname, value)
            return value

I'm thinking my use case isn't all that unique, so if
there's not a
way to do this, I'm also wondering if this is a reasonable
enhancement
request?  As a workaround, I can simply avoid using this
feature, but
it is pretty handy.

Thanks

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: DateTimeField auto_now / auto_now_add and UTC
country flaguser name
United States
2007-10-24 10:33:14
http://www.cnd-industry.
com/


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: DateTimeField auto_now / auto_now_add and UTC
country flaguser name
Poland
2007-10-24 10:35:00
Joe Holloway napisał(a):
> I'm looking to store UTC date/times so I don't have to
worry about
> time zones until I localize the display.  From looking
at the code in
> DateTimeField I think I've answered my own question,
but I just want
> to ask here for the sake of conversation. Is there a
way to get the
> auto_now/auto_now_add feature of DateTimeField to use
UTC?
> 
>         if self.auto_now or (self.auto_now_add and
add):
>             value = datetime.datetime.now()
>             setattr(model_instance, self.attname,
value)
>             return value
> 
> I'm thinking my use case isn't all that unique, so if
there's not a
> way to do this, I'm also wondering if this is a
reasonable enhancement
> request?  As a workaround, I can simply avoid using
this feature, but
> it is pretty handy.

I'd not rely on auto_now and auto_now_add but write UTC
datetime in
overriden save() method for now.

-- 
Jarek Zgoda
Skype: jzgoda | GTalk: zgodajabber.aster.pl | voice:
+48228430101

"We read Knuth so you don't have to." (Tim
Peters)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: DateTimeField auto_now / auto_now_add and UTC
country flaguser name
United States
2007-10-24 15:55:28
> Is there a way to get the
> auto_now/auto_now_add feature of DateTimeField to use
UTC?

Those methods suck and hopefully will be removed. Use a
callable
default instead of auto_now_add (or overridden save instead
of
auto_now, like Jarek gave):

from datetime import datetime
class YourModel(models.Model):
    created = models.DateTimeField(default=datetime.utcnow)


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )