1
0
mirror of https://github.com/instaloader/instaloader.git synced 2024-10-27 05:32:30 +01:00

Timezone option for datetime style formatting (#1316)

Add timezone awareness for datetime objects returned by `date_local` and `date_utc`.
Fixes #1305.
This commit is contained in:
Misael 2021-12-31 08:50:16 -07:00 committed by GitHub
parent 6343e9a69d
commit 21365ec668
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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 from datetime import datetime, timezone, timedelta
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,16 +225,18 @@ 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)."""
return datetime.fromtimestamp(self._node["date"] def get_timedelta(timestamp) -> timedelta:
if "date" in self._node """Timedelta for a given date"""
else self._node["taken_at_timestamp"]) 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._node["date"] return datetime.utcfromtimestamp(self.get_timestamp_date_created())
if "date" in self._node
else self._node["taken_at_timestamp"])
@property @property
def date(self) -> datetime: def date(self) -> datetime:
@ -275,6 +277,12 @@ class Post:
return len(edges) return len(edges)
return 1 return 1
def get_timestamp_date_created(self) -> float:
"""Timestamp when the post was created"""
return (self._node["date"]
if "date" in self._node
else self._node["taken_at_timestamp"])
def get_is_videos(self) -> List[bool]: def get_is_videos(self) -> List[bool]:
""" """
Return a list containing the ``is_video`` property for each media in the post. Return a list containing the ``is_video`` property for each media in the post.
@ -1079,7 +1087,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)."""
return datetime.fromtimestamp(self._node['taken_at_timestamp']) def get_timedelta(timestamp) -> timedelta:
"""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: