Reputation: 753
I have a question concerning django.utils.timezone
package.
I am at UTC+7 timezone. Thus, if it is 16:00 local time,
timezone.now()
returns 9:00. That is just fine.
However when I do the following:
current_tz = pytz.timezone('Europe/Moscow') # UTC + 4
timezone.activate(current_tz)
I expect timezone.now()
to return 12:00 at 16:00 localtime, but the output remains the same "9:00".
Does timezone.activate() affects on anything at all?
Upvotes: 9
Views: 4979
Reputation: 36270
This is what works for me, and I stumbled on this question when trying to find out how to change the timezone in the admin view in the change list. Here is my full example:
from django.contrib import admin
from django.utils import timezone
from audience.models import ClientTrending
TRENDING_TZ = 'Canada/Eastern'
class ClientTrendingAdmin(admin.ModelAdmin):
"""Admin Interface configuration for model"""
def get_changelist_instance(self, request):
timezone.activate(TRENDING_TZ)
return super().get_changelist_instance(request)
admin.site.register(ClientTrending, ClientTrendingAdmin)
You can also pass the timezone string name value as I am using above.
Upvotes: 0
Reputation: 151
timezone.now()
explicitly returns UTC time.
After timezone.activate, then timezone.localtime(timezone.now())
returns the output you want.
Upvotes: 13