Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use UTC-aware datetime objects #957

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions features/steps/coreprops.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from __future__ import absolute_import

from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone

from behave import given, when, then

Expand Down Expand Up @@ -67,7 +67,7 @@ def step_then_a_core_props_part_with_def_vals_is_added(context):
assert core_props.revision == 1
# core_props.modified only stores time with seconds resolution, so
# comparison needs to be a little loose (within two seconds)
modified_timedelta = datetime.utcnow() - core_props.modified
modified_timedelta = datetime.now(timezone.utc) - core_props.modified
max_expected_timedelta = timedelta(seconds=2)
assert modified_timedelta < max_expected_timedelta

Expand Down
2 changes: 1 addition & 1 deletion pptx/oxml/coreprops.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def _parse_W3CDTF_to_datetime(cls, w3cdtf_str):
dt = None
for tmpl in templates:
try:
dt = datetime.strptime(parseable_part, tmpl)
dt = datetime.strptime(parseable_part, tmpl).astimezone()
except ValueError:
continue
if dt is None:
Expand Down
4 changes: 2 additions & 2 deletions pptx/parts/coreprops.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

"""Core properties part, corresponds to ``/docProps/core.xml`` part in package."""

from datetime import datetime
from datetime import datetime, timezone

from pptx.opc.constants import CONTENT_TYPE as CT
from pptx.opc.package import XmlPart
Expand All @@ -27,7 +27,7 @@ def default(cls, package):
core_props.title = "PowerPoint Presentation"
core_props.last_modified_by = "python-pptx"
core_props.revision = 1
core_props.modified = datetime.utcnow()
core_props.modified = datetime.now(timezone.utc)
return core_props

@property
Expand Down
6 changes: 3 additions & 3 deletions tests/parts/test_coreprops.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone

from pptx.opc.constants import CONTENT_TYPE as CT
from pptx.oxml.coreprops import CT_CoreProperties
Expand Down Expand Up @@ -55,7 +55,7 @@ def it_can_construct_a_default_core_props(self):
assert core_props.revision == 1
# core_props.modified only stores time with seconds resolution, so
# comparison needs to be a little loose (within two seconds)
modified_timedelta = datetime.utcnow() - core_props.modified
modified_timedelta = datetime.now(timezone.utc) - core_props.modified
max_expected_timedelta = timedelta(seconds=2)
assert modified_timedelta < max_expected_timedelta

Expand All @@ -70,7 +70,7 @@ def it_can_construct_a_default_core_props(self):
)
def date_prop_get_fixture(self, request, core_properties):
prop_name, expected_datetime = request.param
return core_properties, prop_name, expected_datetime
return core_properties, prop_name, expected_datetime.astimezone() if expected_datetime is not None else None

@pytest.fixture(
params=[
Expand Down