0

I'm using Django 1.11 and pytz==2018.3.

I have a chat model with a timezone field that won't display in local time.

class Message(models.Model):
    sender    = models.ForeignKey(User, on_delete=models.CASCADE, related_name='sender')        
    receiver  = models.ForeignKey(User, on_delete=models.CASCADE, related_name='receiver')        
    message   = models.CharField(max_length=1200)
    timestamp = models.DateTimeField(auto_now_add=True)
    is_read   = models.BooleanField(default=False)

When I print it normally like this I get:

<div class="textcontainer lighter">
    {{ message }}
    <span class="time-right">{{ message.timestamp|date:"SHORT_DATETIME_FORMAT" }}</span>
</div>

output:

July 8, 2018, 2:55 a.m

However, this is UTC time (6 hours ahead. I want it to say a different time instead).

I've tried the following:

{{ message.timestamp|localtime }}

But this doesn't do anything.

I've also tried including these two as well:

{% load tz %}
{% get_current_timezone as TIME_ZONE %}

In my settings.py file I have:

USE_L10N = True
USE_TZ = True

Thoughts? Thanks in advance!

Micah Pearce
  • 1,805
  • 3
  • 28
  • 61
  • Local time here is the time shown in the timezone setup by you in settings.py file or by default it's UTC. What I understand from your question is that you are looking for the time in the user's timezone. – Ankit Jaiswal Jul 10 '18 at 05:03

2 Answers2

0

Local time here means the time in a timezone that has been setup by you in your settings.py file, by default it's UTC. Something like this is defined in your settings.py:

TIME_ZONE = 'America/Chicago'  # or 
TIME_ZONE =  'Asia/Kolkata'

and all the time when displayed in local time will now be in this timezone only.

I believe what you are looking for is the time of the users' machine and for that there is nothing built-in in Django (as per my knowledge). You would need to write your own logic to fetch the users' timezone may be based on their IP address or handle it on client side in Javascript which gets you the time on the users' machine. Another option can be to ask users' timezone at the time of signup and save it in DB to use later for timezone conversions.

Here is a django plugin django-easy-timezones which can help you. It basically uses a middleware to get the IP address information of the connected user and then maps it with a country using ip geolocation database to fetch the timezone of the end user.

Ankit Jaiswal
  • 22,859
  • 5
  • 41
  • 64
  • this looks really nice, but I couldn't get past a "GEOIP_DATABASE setting is defined, but file does not exist." error. I tried putting the GEOIP_Database in multiple spots in my settings.py file and in the django project, but it never seemed to recognize it. – Micah Pearce Jul 11 '18 at 00:38
  • @MicahPearce I have not used it myself but shared here because it looked promising. Can you add the relevant code in question that you tried but did not work? I'm sure there must be some small glitch. – Ankit Jaiswal Jul 11 '18 at 03:59
0

Have you try this?

TIME_ZONE = 'UTC'

Here is the list

joe
  • 8,383
  • 13
  • 61
  • 109