Monday, August 31, 2015

Re: TIME ZONE DJANGO SAVE DATETIMEFIELD

I understand what you say, when I started working with Django I thought exactly the same, it is more convenient that the date is saved with a time zone and then the conversions are made, and also knew that the gap was precisely because those 4:30 in my timezone, but the problem is not to show the data to the user, but as I make my queries so that the filtered data is correct, since my dates are obvious questions, I'll put my concern examples:

The date does not place the customer, we do not control them. Each time an invoice is added, the date placed in my controller using:
   >> factura = Factura()
   >> factura.fecha = timezone.now ()
   >> factura.save ()

For example I consult my templates from sales day, and was shown to the customer in my template:
     1. Add invoice number 6 on August 31, 2015 at 18:48
     2. Add Invoice Number 7 on August 31, 2015 at 18:48
     3. Add Invoice Number 8 on August 31, 2015 at 19:32

But Database I remain as follows:
     1. Invoice number 6 Date: 08/31/2015 23:18:07
     2. Invoice number 6 Date: 08/31/2015 23:31:25
     3. Invoice number 6 Date: 01/09/2015 00:02:17


So if I want to consult the invoices (today August 31, 2015):

 >> Factura.objects.filter (fecha__startswith = timezone.now (). Date ())
 
only leave me the invoice number 6 and number 7 because the number 8 corresponds to another day.

Is there any way of making the request and that django conversion made directly (with some function)? or I have to do the conversion?

I hope I was more explicit. And much I appreciate your time.

El lunes, 31 de agosto de 2015, 18:41:00 (UTC-4:30), James Schneider escribió:


On Aug 31, 2015 2:58 PM, "Serena" <sereni...@gmail.com> wrote:
>
> No doubt that django is acting correctly. I understand what you tell me about it, then I know I can do conversions to the date shown in the templates have my time zone, but it is not what I want. Because when I do queries on bills for closing Z does not work, even when I have to close and tell me which corresponds to another day. by the same gap that you commented.
> If you do the cierreZ day: 2015/08/30 19:00 (client time), with the gap that I have tells me the date is 2015/08/31 1:00 (server time), that is already being done the next day, which is incorrect.
>

Is it possible that is correct? If you are saying that 2015/08/30 19:00 [server timezone] is the same date/time as 2015/08/31 1:00 [other timezone], then what you are seeing appears to be correct. Without actual offsets to work with and what you were expecting, it is a bit difficult to say for sure.

Can you answer the following?

  • How are you controlling the user timezone for display purposes?
  • Are the users entering naive date/times (with no timezone specifier) in forms, and is the server [possibly incorrectly] assuming that the data entered is in the servers' timezone, when the user intended that the date/time is their own local time? Or is the timezone selector in your form being ignored?
  • Have you validated that the server is storing the correct date/times in the database, either as the default timezone or (preferably) as the correct UTC? For example, 2015/08/30 19:00 in America/Caracas -4:30 is actually stored in the database as 2015/08/30 23:30 UTC. 
  • When you are displaying a bill, what time was entered by the user (including TZ), what time was stored in the database, what time is being displayed, and what were you expecting to be displayed?

At this point it is a matter of troubleshooting what you are putting in vs. what you are getting out of the system, and figuring out where the breakdown is. Dates/times are not easy to deal with, and all of us have probably run into a similar issue at some point.

> Ah, excuse my English.
>

No worries, you're doing pretty well.

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2d5ae246-3739-4922-a228-7ab6e5d219a1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment