Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get the visitor's current timezone then convert timezone.now() to string of the local time in Django 1.4?
    text
    copied!<p>I understand that the best practice now with Django 1.4 is to store all <code>datetime</code> in UTC and I agree with that. I also understand that all timezone conversation should be done in the template level like this:</p> <pre><code>{% load tz %} {% timezone "Europe/Paris" %} Paris time: {{ value }} {% endtimezone %} </code></pre> <p>However, I need to convert the UTC time to the <code>request</code>'s local time all in Python. I can't use the template tags since I am returning the string in JSON using Ajax (more specifically <a href="http://www.dajaxproject.com/" rel="noreferrer">Dajaxice</a>).</p> <p>Currently this is my code <code>ajax.py</code>:</p> <pre><code># checked is from the checkbox's this.value (Javascript). datetime = timezone.now() if checked else None $ order_pk is sent to the Ajax function. order = Order.objects.get(pk=order_pk) order.time = datetime order.save() return simplejson.dumps({ 'error': False, 'datetime': dateformat.format(datetime, 'F j, Y, P') if checked else 'None' }) </code></pre> <p>So even if the current time is <code>April 14, 2012, 5:52 p.m.</code> in EST time (my local timezone), the JSON response will return <code>April 14, 2012, 9:52 p.m</code>, because that is the UTC time.</p> <p>Also I noticed that Django stores a template variable called <code>TIME_ZONE</code> for each request (not actually part of the <code>request</code> variable), so since my is <code>America/New_York</code>, I'm assuming that Django can figure out each visitor's own local timezone (based on HTTP header)?</p> <p>Anyway, so my question is two-fold:</p> <ol> <li>How do I get the visitor's local timezone in my <code>ajax.py</code>? (Probably pass it as a string argument like <code>{{ TIME_ZONE }}</code>)</li> <li>With the visitor's local timezone, how to convert the UTC <code>timezone.now()</code> to the local timezone and output as a string using Django's <code>dateformat</code>?</li> </ol> <p><strong>EDIT:</strong> for @agf</p> <p><code>timezone.now()</code> gives the UTC time when <code>USE_TZ = True</code>:</p> <pre><code># From django.utils.timezone def now(): """ Returns an aware or naive datetime.datetime, depending on settings.USE_TZ. """ if settings.USE_TZ: # timeit shows that datetime.now(tz=utc) is 24% slower return datetime.utcnow().replace(tzinfo=utc) else: return datetime.now() </code></pre> <p>Is there anyway to convert a <code>datetime</code> to something other than UTC? For example, can I do something like <code>current_time = timezone.now()</code>, then <code>current_time.replace(tzinfo=est)</code> (EST = Eastern Standard Time)?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload