1
0
mirror of https://github.com/instaloader/instaloader.git synced 2024-10-26 22:22:30 +02:00

Make {Post,StoryItem}.date_utc timezone aware

Also simplify {Post,StoryItem}.date_local.

Discussed in #1316.
This commit is contained in:
Alexander Graf 2022-01-07 14:49:27 +01:00
parent 555c86633c
commit d864ce08ff

View File

@ -3,7 +3,7 @@ import lzma
import re import re
from base64 import b64decode, b64encode from base64 import b64decode, b64encode
from collections import namedtuple from collections import namedtuple
from datetime import datetime, timezone, timedelta from datetime import datetime, timezone
from pathlib import Path from pathlib import Path
from typing import Any, Dict, Iterable, Iterator, List, Optional, Tuple, Union from typing import Any, Dict, Iterable, Iterator, List, Optional, Tuple, Union
@ -225,18 +225,12 @@ class Post:
@property @property
def date_local(self) -> datetime: def date_local(self) -> datetime:
"""Timestamp when the post was created (local time zone).""" """Timestamp when the post was created (local time zone)."""
def get_timedelta(timestamp) -> timedelta: return datetime.fromtimestamp(self.get_timestamp_date_created()).astimezone()
"""Timedelta for a given date"""
return datetime.fromtimestamp(timestamp) - datetime.utcfromtimestamp(timestamp)
timestamp_date = self.get_timestamp_date_created()
tzinfo = timezone(get_timedelta(timestamp_date))
return datetime.fromtimestamp(timestamp_date, tzinfo)
@property @property
def date_utc(self) -> datetime: def date_utc(self) -> datetime:
"""Timestamp when the post was created (UTC).""" """Timestamp when the post was created (UTC)."""
return datetime.utcfromtimestamp(self.get_timestamp_date_created()) return datetime.utcfromtimestamp(self.get_timestamp_date_created()).replace(tzinfo=timezone.utc)
@property @property
def date(self) -> datetime: def date(self) -> datetime:
@ -1087,17 +1081,12 @@ class StoryItem:
@property @property
def date_local(self) -> datetime: def date_local(self) -> datetime:
"""Timestamp when the StoryItem was created (local time zone).""" """Timestamp when the StoryItem was created (local time zone)."""
def get_timedelta(timestamp) -> timedelta: return datetime.fromtimestamp(self._node['taken_at_timestamp']).astimezone()
"""Timedelta for a given date"""
return datetime.fromtimestamp(timestamp) - datetime.utcfromtimestamp(timestamp)
tzinfo = timezone(get_timedelta(self._node['taken_at_timestamp']))
return datetime.fromtimestamp(self._node['taken_at_timestamp'], tzinfo)
@property @property
def date_utc(self) -> datetime: def date_utc(self) -> datetime:
"""Timestamp when the StoryItem was created (UTC).""" """Timestamp when the StoryItem was created (UTC)."""
return datetime.utcfromtimestamp(self._node['taken_at_timestamp']) return datetime.utcfromtimestamp(self._node['taken_at_timestamp']).replace(tzinfo=timezone.utc)
@property @property
def date(self) -> datetime: def date(self) -> datetime: