1

I feel like I'm missing something obvious:

I implemented timezones in my Django app using the SO solution here and it seems to be working (i.e. when the user changes to their timezone, Django's {% get_current_timezone as TIME_ZONE %} and timezone.get_current_timezone.now() reflect that change)

However, I have a Django model with both a naive DateField and TimeField input. I have defined a property to return the formatted string with the date and time as follows:

# imports
from django.db import models
import pytz
from django.utils import timezone
from django.template.defaultfilters import date as format_date
from django.template.defaultfilters import time as format_time


class MyModel(models.Model):

    date = models.DateField(default=timezone.now, null=True, blank=True)
    time = models.TimeField(default=timezone.now, null=True, blank=True)
   ...

@property
    def datetime(self):
        date_str = format_date(self.date, "j N Y")
        time_str = format_time(self.time, 'g:i a') 

        return f"{date_str} at {time_str}"

How would I make the dates and times returned in the string changed to suit the timezone? (It can be assumed that they currently refer to UTC.) I'm currently trying with the time field. I tried both of these functions to make the time aware:

  • pytz.utc.localize(self.time)
  • self.time.replace(tzinfo=pytz.UTC)

But whenever I try to get the subsequent time for the zone through the statement:

local_time = self.time.replace(tzinfo=pytz.UTC)

I get the error:

'datetime.time' object has no attribute 'astimezone'

Edit: I think the function that I should have been using was

local_time = aware_time.astimezone(timezone.get_current_timezone())

but the same error applies.

Hermit
  • 35
  • 3

1 Answers1

1

If you want to manipulate time zones you need to be working with datetime objects. Create one with datetime.combine:

from datetime import datetime
from datetime.timezone import utc

dt = datetime.combine(self.date, self.time, utc)

At that point you have an aware datetime object and can convert it to another timezone, print it out, etc.

Kevin Christopher Henry
  • 46,175
  • 7
  • 116
  • 102
  • This did help my original problem so thanks! However for some reason converting between UTC and timezones for form inputs is adding 24 minutes to the inputted time (the test timezone was St. Lucia). – Hermit Jan 13 '23 at 21:47
  • @Hermit: That's strange. You'll have to include the full code for what you're doing, along with expected and actual output. You can edit the question above, but it probably qualifies as a separate question. – Kevin Christopher Henry Jan 14 '23 at 03:14
  • Ok it was my bad: I didn't notice there was a `localize` parameter for the form fields themselves and was trying to force the conversion on the naive datetimes the form inputted by using `timezone.get_current_timezone()`. I'm not sure why that results in a 24 minute offset specifically but setting `localize=True` in the form fields and then combining the aware `date` and `time` into a `datetime` works fine. – Hermit Jan 14 '23 at 12:54