1
0
mirror of https://github.com/instaloader/instaloader.git synced 2024-08-18 04:39:39 +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
from base64 import b64decode, b64encode
from collections import namedtuple
from datetime import datetime, timezone, timedelta
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Dict, Iterable, Iterator, List, Optional, Tuple, Union
@ -225,18 +225,12 @@ class Post:
@property
def date_local(self) -> datetime:
"""Timestamp when the post was created (local time zone)."""
def get_timedelta(timestamp) -> timedelta:
"""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)
return datetime.fromtimestamp(self.get_timestamp_date_created()).astimezone()
@property
def date_utc(self) -> datetime:
"""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
def date(self) -> datetime:
@ -1087,17 +1081,12 @@ class StoryItem:
@property
def date_local(self) -> datetime:
"""Timestamp when the StoryItem was created (local time zone)."""
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)
return datetime.fromtimestamp(self._node['taken_at_timestamp']).astimezone()
@property
def date_utc(self) -> datetime:
"""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
def date(self) -> datetime: