Skip to content

Commit

Permalink
Add timezone support when rendering datetimes.
Browse files Browse the repository at this point in the history
Fixes #42.
  • Loading branch information
jcass77 committed Nov 17, 2018
1 parent 555fa7d commit 98de582
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 8 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ pip install django-apscheduler
Usage
-----

* Add ``django_apscheduler`` to ``INSTALLED_APPS`` in your Django project settings:
* Add ``django_apscheduler`` to ``INSTALLED_APPS`` in your Django project settings, You can also specify a different
format for displaying runtime timestamps in the Django admin site using ``APSCHEDULER_DATETIME_FORMAT``:
```python

INSTALLED_APPS = (
...
django_apscheduler,
)

APSCHEDULER_DATETIME_FORMAT = "N j, Y, f:s a" # Default

```

* Run migrations:
Expand Down
5 changes: 3 additions & 2 deletions django_apscheduler/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.utils.timezone import now

from django_apscheduler.models import DjangoJob, DjangoJobExecution
from django_apscheduler import util


def execute_now(ma, r, qs):
Expand Down Expand Up @@ -32,7 +33,7 @@ def get_queryset(self, request):
return super(DjangoJobAdmin, self).get_queryset(request)

def next_run_time_sec(self, obj):
return obj.next_run_time.strftime("%Y-%m-%d %H:%M:%S")
return util.localize(obj.next_run_time)

def average_duration(self, obj):
return self._durations.get(obj.id) or 0
Expand All @@ -45,7 +46,7 @@ class DjangoJobExecutionAdmin(admin.ModelAdmin):
list_filter = ["job__name", "run_time", "status"]

def run_time_sec(self, obj):
return obj.run_time.strftime("%Y-%m-%d %H:%M:%S")
return util.localize(obj.run_time)

def get_queryset(self, request):
return super(DjangoJobExecutionAdmin, self).get_queryset(
Expand Down
22 changes: 17 additions & 5 deletions django_apscheduler/util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.conf import settings
from django.utils.timezone import is_aware, is_naive, make_aware, make_naive
from django.utils import formats
from django.utils import timezone


def serialize_dt(dt):
Expand All @@ -8,12 +9,23 @@ def serialize_dt(dt):
:param dt:
:return:
"""
if not settings.USE_TZ and dt and is_aware(dt):
return make_naive(dt)
if not settings.USE_TZ and dt and timezone.is_aware(dt):
return timezone.make_naive(dt)
return dt


def deserialize_dt(dt):
if not settings.USE_TZ and dt and is_naive(dt):
return make_aware(dt)
if not settings.USE_TZ and dt and timezone.is_naive(dt):
return timezone.make_aware(dt)
return dt


def get_format():
return formats.get_format(getattr(settings, "APSCHEDULER_DATETIME_FORMAT", "N j, Y, f:s a"))


def localize(dt):
if settings.USE_TZ and dt and timezone.is_aware(dt):
dt = timezone.localtime(dt)

return formats.date_format(dt, get_format())
23 changes: 23 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest
from django.conf import settings


@pytest.fixture
def use_seconds_format():
settings.APSCHEDULER_DATETIME_FORMAT = "N j, Y, f:s"
return settings.APSCHEDULER_DATETIME_FORMAT


@pytest.fixture
def use_tz():
settings.APSCHEDULER_DATETIME_FORMAT = "H" # Only interested in hour
settings.USE_TZ = True

return settings.APSCHEDULER_DATETIME_FORMAT


@pytest.fixture
def use_hour_format():
settings.APSCHEDULER_DATETIME_FORMAT = "H" # Only interested in hour

return settings.APSCHEDULER_DATETIME_FORMAT
34 changes: 34 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from datetime import datetime

from django.utils import timezone

from django_apscheduler import util


def test_get_format_default():
assert util.get_format() == "N j, Y, f:s a"


def test_get_format_from_settings(use_seconds_format):
assert util.get_format() == use_seconds_format


def test_localize_naive(use_hour_format):

dt = datetime.utcnow()
dt_hour = dt.strftime("%H")

localized_dt_hour = util.localize(dt)

assert localized_dt_hour == dt_hour


def test_localize_aware(use_tz):
utc_dt = timezone.now()

local_dt = timezone.localtime(utc_dt)
local_dt_hour = local_dt.strftime("%H")

localized_dt_hour = util.localize(utc_dt)

assert localized_dt_hour == local_dt_hour

0 comments on commit 98de582

Please sign in to comment.