From 3d07ac0c49265bde725b91376ca727d16e65aa76 Mon Sep 17 00:00:00 2001 From: Rishi Gupta Date: Sat, 25 Feb 2017 19:58:25 -0800 Subject: [PATCH] Change timezone-naive datetimes to use timezone.now() where safe to do so. Change timezone-naive datetimes to use timezone.now() in cases where there is no change in behavior. --- .../zulip_zephyr_mirror/check_user_zephyr_mirror_liveness | 7 ++++--- zerver/lib/logging_util.py | 6 ++++-- zerver/management/commands/enqueue_digest_emails.py | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/puppet/zulip_ops/files/nagios_plugins/zulip_zephyr_mirror/check_user_zephyr_mirror_liveness b/puppet/zulip_ops/files/nagios_plugins/zulip_zephyr_mirror/check_user_zephyr_mirror_liveness index c63db9fa99..b9507fdf01 100755 --- a/puppet/zulip_ops/files/nagios_plugins/zulip_zephyr_mirror/check_user_zephyr_mirror_liveness +++ b/puppet/zulip_ops/files/nagios_plugins/zulip_zephyr_mirror/check_user_zephyr_mirror_liveness @@ -18,6 +18,7 @@ sys.path.append('/home/zulip/deployments/current') import scripts.lib.setup_path_on_import import django +from django.utils import timezone os.environ['DJANGO_SETTINGS_MODULE'] = "zproject.settings" sys.path.append('/home/zulip/deployments/current') @@ -50,13 +51,13 @@ def report(state, short_msg, too_old=None): exit(states[state]) -now = datetime.datetime.utcnow() +now = timezone.now() all_users = UserActivity.objects.filter(query__in=["get_events_backend", "/api/v1/events"], client__name="zephyr_mirror") -new_inactive_users = [user for user in all_users if user.last_visit.replace(tzinfo=None) < +new_inactive_users = [user for user in all_users if user.last_visit < now - datetime.timedelta(minutes=10)] -old_inactive_users = [user for user in new_inactive_users if user.last_visit.replace(tzinfo=None) < +old_inactive_users = [user for user in new_inactive_users if user.last_visit < now - datetime.timedelta(minutes=60)] recently_inactive_users = set(new_inactive_users) - set(old_inactive_users) diff --git a/zerver/lib/logging_util.py b/zerver/lib/logging_util.py index ef6861fc7c..c9f7adc1b0 100644 --- a/zerver/lib/logging_util.py +++ b/zerver/lib/logging_util.py @@ -1,5 +1,7 @@ from __future__ import absolute_import +from django.utils import timezone + import hashlib import logging import traceback @@ -38,10 +40,10 @@ class _RateLimitFilter(object): if not duplicate: cache.set(key, 1, rate) else: - min_date = datetime.now() - timedelta(seconds=rate) + min_date = timezone.now() - timedelta(seconds=rate) duplicate = (self.last_error >= min_date) if not duplicate: - self.last_error = datetime.now() + self.last_error = timezone.now() return not duplicate diff --git a/zerver/management/commands/enqueue_digest_emails.py b/zerver/management/commands/enqueue_digest_emails.py index 018b01e0ca..1c045a35e2 100644 --- a/zerver/management/commands/enqueue_digest_emails.py +++ b/zerver/management/commands/enqueue_digest_emails.py @@ -94,7 +94,7 @@ in a while. # To be really conservative while we don't have user timezones or # special-casing for companies with non-standard workweeks, only # try to send mail on Tuesdays, Wednesdays, and Thursdays. - if datetime.datetime.utcnow().weekday() not in VALID_DIGEST_DAYS: + if timezone.now().weekday() not in VALID_DIGEST_DAYS: return deployment_realms = realms_for_this_deployment()