Construct datetime without converting to localtime

This commit is contained in:
Sonny 2019-07-12 20:20:58 +02:00
parent 8f314e0ca6
commit 1b774a7208
3 changed files with 9 additions and 9 deletions

View file

@ -70,7 +70,7 @@ class FeedBuilder(Builder):
value = self.truncate_text(model_field, entry[field]) value = self.truncate_text(model_field, entry[field])
if field == "published_parsed": if field == "published_parsed":
created, aware_datetime = build_publication_date(value, tz) aware_datetime, created = build_publication_date(value, tz)
data[model_field] = aware_datetime if created else None data[model_field] = aware_datetime if created else None
elif field == "summary": elif field == "summary":
summary = self.sanitize_summary(value) summary = self.sanitize_summary(value)

View file

@ -139,7 +139,7 @@ class FeedCollectorTestCase(TestCase):
self.mocked_parse.return_value = duplicate_mock self.mocked_parse.return_value = duplicate_mock
rule = CollectionRuleFactory() rule = CollectionRuleFactory()
_, aware_datetime = build_publication_date( aware_datetime, _ = build_publication_date(
struct_time((2019, 5, 20, 16, 7, 37, 0, 140, 0)), pytz.utc struct_time((2019, 5, 20, 16, 7, 37, 0, 140, 0)), pytz.utc
) )
@ -152,7 +152,7 @@ class FeedCollectorTestCase(TestCase):
rule=rule, rule=rule,
) )
_, aware_datetime = build_publication_date( aware_datetime, _ = build_publication_date(
struct_time((2019, 5, 20, 12, 19, 19, 0, 140, 0)), pytz.utc struct_time((2019, 5, 20, 12, 19, 19, 0, 140, 0)), pytz.utc
) )
@ -165,7 +165,7 @@ class FeedCollectorTestCase(TestCase):
rule=rule, rule=rule,
) )
_, aware_datetime = build_publication_date( aware_datetime, _ = build_publication_date(
struct_time((2019, 5, 20, 16, 32, 38, 0, 140, 0)), pytz.utc struct_time((2019, 5, 20, 16, 32, 38, 0, 140, 0)), pytz.utc
) )

View file

@ -1,5 +1,5 @@
from datetime import datetime, tzinfo from datetime import datetime, tzinfo
from time import mktime, struct_time from time import struct_time
from typing import Tuple from typing import Tuple
from django.utils import timezone from django.utils import timezone
@ -14,11 +14,11 @@ from newsreader.news.collection.response_handler import ResponseHandler
def build_publication_date(dt: struct_time, tz: tzinfo) -> Tuple: def build_publication_date(dt: struct_time, tz: tzinfo) -> Tuple:
try: try:
naive_datetime = datetime.fromtimestamp(mktime(dt)) naive_datetime = datetime(*dt[:6])
published_parsed = timezone.make_aware(naive_datetime, timezone=tz) published_parsed = timezone.make_aware(naive_datetime, timezone=tz)
except TypeError: except (TypeError, ValueError):
return False, None return None, False
return True, published_parsed return published_parsed, True
def fetch(url: str) -> Response: def fetch(url: str) -> Response: