Skip to content

Commit 7043c67

Browse files
committed
model: set UTC timezone to DateTimes read rom JSON too
I came a cross this difference during testing of SAP#95 I decided to set tzinfo of JSON DateTimes to simplify my tests and it also make sense to me to have the values configured the same way.
1 parent ae17331 commit 7043c67

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99
### Added
1010
- support for Edm.Float - Jakub Filak
1111

12+
### Changed
13+
- both Literal and JSON DateTimes has Timezone set to UTC - Jakub Filak
14+
1215
### Fixed
1316
- removed superfluous debug print when parsing FunctionImports from metadata - Jakub Filak
1417

pyodata/v2/model.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ def from_literal(self, value):
426426
except ValueError:
427427
raise PyODataModelError('Cannot decode datetime from value {}.'.format(value))
428428

429-
return value
429+
return value.replace(tzinfo=datetime.timezone.utc)
430430

431431

432432
class EdmStringTypTraits(TypTraits):

tests/test_model_v2.py

+8
Original file line numberDiff line numberDiff line change
@@ -468,19 +468,22 @@ def test_traits_datetime():
468468
assert testdate.minute == 33
469469
assert testdate.second == 6
470470
assert testdate.microsecond == 654321
471+
assert testdate.tzinfo == timezone.utc
471472

472473
# parsing without miliseconds
473474
testdate = typ.traits.from_literal("datetime'1976-11-23T03:33:06'")
474475
assert testdate.year == 1976
475476
assert testdate.second == 6
476477
assert testdate.microsecond == 0
478+
assert testdate.tzinfo == timezone.utc
477479

478480
# parsing without seconds and miliseconds
479481
testdate = typ.traits.from_literal("datetime'1976-11-23T03:33'")
480482
assert testdate.year == 1976
481483
assert testdate.minute == 33
482484
assert testdate.second == 0
483485
assert testdate.microsecond == 0
486+
assert testdate.tzinfo == timezone.utc
484487

485488
# parsing invalid value
486489
with pytest.raises(PyODataModelError) as e_info:
@@ -502,19 +505,22 @@ def test_traits_datetime():
502505
assert testdate.minute == 33
503506
assert testdate.second == 6
504507
assert testdate.microsecond == 10000
508+
assert testdate.tzinfo == timezone.utc
505509

506510
# parsing without miliseconds
507511
testdate = typ.traits.from_json("/Date(217567986000)/")
508512
assert testdate.year == 1976
509513
assert testdate.second == 6
510514
assert testdate.microsecond == 0
515+
assert testdate.tzinfo == timezone.utc
511516

512517
# parsing without seconds and miliseconds
513518
testdate = typ.traits.from_json("/Date(217567980000)/")
514519
assert testdate.year == 1976
515520
assert testdate.minute == 33
516521
assert testdate.second == 0
517522
assert testdate.microsecond == 0
523+
assert testdate.tzinfo == timezone.utc
518524

519525
# parsing the lowest value
520526
with pytest.raises(OverflowError):
@@ -528,6 +534,7 @@ def test_traits_datetime():
528534
assert testdate.minute == 0
529535
assert testdate.second == 0
530536
assert testdate.microsecond == 0
537+
assert testdate.tzinfo == timezone.utc
531538

532539
# parsing the highest value
533540
with pytest.raises(OverflowError):
@@ -541,6 +548,7 @@ def test_traits_datetime():
541548
assert testdate.minute == 59
542549
assert testdate.second == 59
543550
assert testdate.microsecond == 999000
551+
assert testdate.tzinfo == timezone.utc
544552

545553
# parsing invalid value
546554
with pytest.raises(PyODataModelError) as e_info:

0 commit comments

Comments
 (0)