Skip to content

Commit

Permalink
Merge branch 'eumiro-modern-datetime'
Browse files Browse the repository at this point in the history
  • Loading branch information
halcy authored and halcy committed Nov 30, 2022
2 parents 262d150 + 2d7f495 commit 99432f5
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 15 deletions.
2 changes: 1 addition & 1 deletion mastodon/Mastodon.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import time
import datetime
import collections
from contextlib import closing
import requests
from requests.models import urlencode
import dateutil
Expand Down Expand Up @@ -3744,4 +3745,3 @@ def stream_healthy(self):
if api_okay in [b'OK', b'success']:
return True
return False

16 changes: 5 additions & 11 deletions mastodon/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import threading
import six
import uuid
import pytz
import dateutil.parser
import time
import copy
Expand All @@ -24,23 +23,18 @@
###
# Internal helpers, dragons probably
###
class Mastodon():
class Mastodon():
def __datetime_to_epoch(self, date_time):
"""
Converts a python datetime to unix epoch, accounting for
time zones and such.
Assumes UTC if timezone is not given.
"""
date_time_utc = None
if date_time.tzinfo is None:
date_time_utc = date_time.replace(tzinfo=pytz.utc)
else:
date_time_utc = date_time.astimezone(pytz.utc)

epoch_utc = datetime.datetime.utcfromtimestamp(0).replace(tzinfo=pytz.utc)
date_time = date_time.replace(tzinfo=datetime.timezone.utc)
return date_time.timestamp()

return (date_time_utc - epoch_utc).total_seconds()

def __get_logged_in_id(self):
"""
Expand Down Expand Up @@ -73,7 +67,7 @@ def __json_date_parse(json_object):
if v is not None:
try:
if isinstance(v, int):
json_object[k] = datetime.datetime.fromtimestamp(v, pytz.utc)
json_object[k] = datetime.datetime.fromtimestamp(v, datetime.timezone.utc)
else:
json_object[k] = dateutil.parser.parse(v)
except:
Expand Down Expand Up @@ -129,7 +123,7 @@ def __consistent_isoformat_utc(datetime_val):
every time instead of randomly doing different things on some systems
and also it represents that time as the equivalent UTC time.
"""
isotime = datetime_val.astimezone(pytz.utc).strftime("%Y-%m-%dT%H:%M:%S%z")
isotime = datetime_val.astimezone(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S%z")
if isotime[-2] != ":":
isotime = isotime[:-2] + ":" + isotime[-2:]
return isotime
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
'requests>=2.4.2',
'python-dateutil',
'six',
'pytz',
'python-magic',
'decorator>=4.0.0',
] + blurhash_deps,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_status.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from mastodon.Mastodon import MastodonAPIError, MastodonNotFoundError
import datetime
import pytz
import zoneinfo
import vcr
import time
import pickle
Expand Down Expand Up @@ -154,7 +154,7 @@ def test_status_pin_unpin(status, api):

@pytest.mark.vcr(match_on=['path'])
def test_scheduled_status(api):
base_time = datetime.datetime(4000, 1, 1, 12, 13, 14, 0, pytz.timezone("Etc/GMT+2"))
base_time = datetime.datetime(4000, 1, 1, 12, 13, 14, 0, zoneinfo.ZoneInfo("Etc/GMT+2"))
the_future = base_time + datetime.timedelta(minutes=20)
scheduled_toot = api.status_post("please ensure adequate headroom", scheduled_at=the_future)
assert scheduled_toot
Expand Down

0 comments on commit 99432f5

Please sign in to comment.