From d469630759e4616308fe1b087d0f5743cf644d38 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 4 Dec 2015 17:48:26 +0100 Subject: [PATCH 3/4] misc: add helper method to parse xsd:datetime value in the UTC timezone (#8887) --- tests/test_misc.py | 9 ++++++++- wcs/qommon/misc.py | 5 +++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/test_misc.py b/tests/test_misc.py index f01a922..53a38f6 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -8,7 +8,7 @@ from quixote import cleanup import wcs.api # workaround against circular dependencies :/ from wcs.qommon.form import FileSizeWidget from wcs.qommon.humantime import humanduration2seconds, seconds2humanduration -from wcs.qommon.misc import simplify, json_loads +from wcs.qommon.misc import simplify, json_loads, parse_isotime from wcs.admin.settings import FileTypesDirectory def setup_module(module): @@ -95,3 +95,10 @@ def test_json_str_decoder(): assert type(json_loads(json_str)['lst'][0]['a']) is str assert type(json_loads(json_str)['bla']) is str assert json_loads(json_str)['bla'] == u'éléphant'.encode('utf-8') + +def test_parse_isotime(): + assert 1420107019 == parse_isotime('2015-01-01T10:10:19Z') + with pytest.raises(ValueError): + parse_isotime('2015-01-01T10:10:19') + with pytest.raises(ValueError): + parse_isotime('2015-01-0110:10:19Z') diff --git a/wcs/qommon/misc.py b/wcs/qommon/misc.py index 06ec38c..780b263 100644 --- a/wcs/qommon/misc.py +++ b/wcs/qommon/misc.py @@ -15,6 +15,7 @@ # along with this program; if not, see . import datetime +import calendar import re import os import time @@ -455,3 +456,7 @@ def json_response(data): json_str = '%s(%s);' % (get_request().form[variable], json_str) break return json_str + +def parse_isotime(s): + t = time.strptime(s, '%Y-%m-%dT%H:%M:%SZ') + return calendar.timegm(t) -- 2.1.4