Skip to content

Commit d218e80

Browse files
mamiksikfilak-sap
authored andcommitted
Fix timezone bug when converting datetime object to literal
Edm.DateTime should not contain timezone information as it did with previous implementation. Detail reasoning behind these changes can be found in commit #adf376f3436b2fdaebb6fc6da65c35664f8b9b3c Previous output of to_literal: '2019-10-09T12:14:25.216435+00:00' New output of to_literal: '2019-10-09T12:15:39.744821' Documentation also provide extensive explanation of differences between DateTime and DateTimeOffset: https://www.odata.org/documentation/odata-version-2-0/overview/
1 parent adf376f commit d218e80

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

pyodata/v2/model.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,8 @@ def to_literal(self, value):
377377
raise PyODataModelError(
378378
'Cannot convert value of type {} to literal. Datetime format is required.'.format(type(value)))
379379

380-
return super(EdmDateTimeTypTraits, self).to_literal(value.isoformat())
380+
# Sets timezone to none to avoid including timezone information in the literal form.
381+
return super(EdmDateTimeTypTraits, self).to_literal(value.replace(tzinfo=None).isoformat())
381382

382383
def to_json(self, value):
383384
if isinstance(value, str):

tests/test_model_v2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Tests for OData Model module"""
22
# pylint: disable=line-too-long,too-many-locals,too-many-statements,invalid-name, too-many-lines, no-name-in-module, expression-not-assigned, pointless-statement
33

4-
from datetime import datetime
4+
from datetime import datetime, timezone
55
from unittest.mock import patch
66
import pytest
77
from pyodata.v2.model import Schema, Typ, StructTypeProperty, Types, EntityType, EdmStructTypeSerializer, \
@@ -416,7 +416,7 @@ def test_traits_datetime():
416416

417417
# 1. direction Python -> OData
418418

419-
testdate = datetime(2005, 1, 28, 18, 30, 44, 123456)
419+
testdate = datetime(2005, 1, 28, 18, 30, 44, 123456, tzinfo=timezone.utc)
420420
assert typ.traits.to_literal(testdate) == "datetime'2005-01-28T18:30:44.123456'"
421421

422422
# without miliseconds part

0 commit comments

Comments
 (0)