From 173b26f4048d458e56d37794278f5c636bf2bdcd Mon Sep 17 00:00:00 2001 From: yihuang Date: Fri, 20 Sep 2019 01:40:18 +0800 Subject: [PATCH] fix timezone --- admin_tools_stats/modules.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/admin_tools_stats/modules.py b/admin_tools_stats/modules.py index 8cfc218e..d5558029 100644 --- a/admin_tools_stats/modules.py +++ b/admin_tools_stats/modules.py @@ -20,16 +20,14 @@ from django.contrib import messages from django.core.exceptions import FieldError from django.utils.safestring import mark_safe +from django.utils import timezone from qsstats import QuerySetStats from cache_utils.decorators import cached from admin_tools.dashboard import modules from admin_tools_stats.models import DashboardStats -from datetime import datetime, timedelta - +from datetime import timedelta import time -from django.utils.timezone import now - class DashboardChart(modules.DashboardModule): """Dashboard module with user registration charts. @@ -124,7 +122,7 @@ def get_registrations(self, interval, days, graph_key, select_box_value): stats = QuerySetStats(model_name.objects.filter(**kwargs).distinct(), conf_data.date_field_name, aggregate) # stats = QuerySetStats(User.objects.filter(is_active=True), 'date_joined') - today = now() + today = timezone.now() if days == 24: begin = today - timedelta(hours=days - 1) return stats.time_series(begin, today + timedelta(hours=1), interval) @@ -136,7 +134,7 @@ def get_registrations(self, interval, days, graph_key, select_box_value): User = get_user_model() stats = QuerySetStats( User.objects.filter(is_active=True), 'date_joined') - today = now() + today = timezone.now() if days == 24: begin = today - timedelta(hours=days - 1) return stats.time_series(begin, today + timedelta(hours=1), interval) @@ -161,8 +159,9 @@ def prepare_template_data(self, data, graph_key, select_box_value, other_select_ xdata = [] ydata = [] + current_tz = timezone.get_current_timezone() for data_date in self.data: - start_time = int(time.mktime(data_date[0].timetuple()) * 1000) + start_time = int(time.mktime(data_date[0].astimezone(current_tz).timetuple()) * 1000) xdata.append(start_time) ydata.append(data_date[1])