From ba216250ecd40b7cf641733ffb10c2ed83f5cab3 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 4 Dec 2015 19:30:04 +0100 Subject: [PATCH 1/2] utils: add method to convert datetime to UTC and to xs:datetime strings (using UTC timezone) (#9141) --- src/authentic2/utils.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/authentic2/utils.py b/src/authentic2/utils.py index 25162b4..df3145d 100644 --- a/src/authentic2/utils.py +++ b/src/authentic2/utils.py @@ -21,6 +21,7 @@ from django.contrib.auth import (REDIRECT_FIELD_NAME, login as auth_login, from django import forms from django.forms.util import ErrorList from django.forms.utils import to_current_timezone +from django.utils import timezone from django.utils import html, http from django.utils.translation import ugettext as _ from django.shortcuts import resolve_url @@ -616,3 +617,11 @@ def switch_back(request): else: messages.warning(request, _('No user to switch back to')) return continue_to_next_url(request) + +def datetime_to_utc(dt): + if timezone.is_naive(dt): + dt = timezone.make_aware(dt) + return dt.astimezone(timezone.utc) + +def datetime_to_xs_datetime(dt): + return datetime_to_utc(dt).isoformat().split('.')[0] + 'Z' -- 2.1.4