Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## Unreleased
* Convert timezone-aware datetimes to UTC before serializing them with a `Z` suffix

## 4.48.0
* Add `Transaction.reverse()` to automatically void or fully refund a transaction
* Add `previous_mastercard_transaction_link_id` to `external_vault` on `Transaction` and `CreditCardVerification.create`
Expand Down
2 changes: 2 additions & 0 deletions braintree/util/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def __generate_boolean(self, value):
return str(value).lower()

def __generate_datetime(self, value):
if isinstance(value, datetime.datetime) and value.utcoffset() is not None:
value = value.astimezone(datetime.timezone.utc)
return value.strftime("%Y-%m-%dT%H:%M:%SZ")

def __generate_dict(self, dictionary):
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/test_generator_timezones.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import unittest
from datetime import date, datetime, timedelta, timezone

from braintree.util.xml_util import XmlUtil


class TestGeneratorTimezones(unittest.TestCase):
def test_positive_offset_preserves_instant_across_date_boundary(self):
value = datetime(2026, 1, 2, 3, 4, 5, tzinfo=timezone(timedelta(hours=5, minutes=30)))
xml = XmlUtil.xml_from_dict({"created_at": value})
self.assertEqual('<created_at type="datetime">2026-01-01T21:34:05Z</created_at>', xml)
self.assertEqual({"created_at": datetime(2026, 1, 1, 21, 34, 5)}, XmlUtil.dict_from_xml(xml))

def test_negative_offset_preserves_instant(self):
value = datetime(2026, 1, 2, 3, 4, 5, tzinfo=timezone(timedelta(hours=-7)))
xml = XmlUtil.xml_from_dict({"created_at": value})
self.assertEqual('<created_at type="datetime">2026-01-02T10:04:05Z</created_at>', xml)
self.assertEqual({"created_at": datetime(2026, 1, 2, 10, 4, 5)}, XmlUtil.dict_from_xml(xml))

def test_utc_keeps_existing_output(self):
value = datetime(2026, 1, 2, 3, 4, 5, tzinfo=timezone.utc)
self.assertEqual('<value type="datetime">2026-01-02T03:04:05Z</value>', XmlUtil.xml_from_dict({"value": value}))

def test_naive_datetime_keeps_existing_output(self):
value = datetime(2026, 1, 2, 3, 4, 5)
self.assertEqual('<value type="datetime">2026-01-02T03:04:05Z</value>', XmlUtil.xml_from_dict({"value": value}))

def test_date_keeps_existing_output(self):
self.assertEqual('<value type="datetime">2026-01-02T00:00:00Z</value>', XmlUtil.xml_from_dict({"value": date(2026, 1, 2)}))