Skip to content

Commit

Permalink
move time related functions to helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
igorbrigadir committed May 6, 2021
1 parent 0277486 commit 477676c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
28 changes: 0 additions & 28 deletions twarc/client2.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,34 +740,6 @@ def _ensure_user_id(self, user):
else:
raise ValueError(f"No such user {user}")

def _ts(dt):
"""
Return ISO 8601 / RFC 3339 datetime in UTC. If no timezone is specified it
is assumed to be in UTC. The Twitter API does not accept microseconds.
Args:
dt (datetime): a `datetime` object to format.
Returns:
str: an ISO 8601 / RFC 3339 datetime in UTC.
"""
if dt.tzinfo:
dt = dt.astimezone(datetime.timezone.utc)
else:
dt = dt.replace(tzinfo=datetime.timezone.utc)
return dt.isoformat(timespec='seconds')

def _utcnow():
"""
Return _now_ in ISO 8601 / RFC 3339 datetime in UTC.
Returns:
datetime: Current timestamp in UTC.
"""
return datetime.datetime.now(datetime.timezone.utc).isoformat(
timespec='seconds'
)

def _append_metadata(result, url):
"""
Appends `__twarc` metadata to the result.
Expand Down
31 changes: 31 additions & 0 deletions twarc/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
Useful functions for converting things into different types
"""

def _ts(dt):
"""
Return ISO 8601 / RFC 3339 datetime in UTC. If no timezone is specified it
is assumed to be in UTC. The Twitter API does not accept microseconds.
Args:
dt (datetime): a `datetime` object to format.
Returns:
str: an ISO 8601 / RFC 3339 datetime in UTC.
"""
if dt.tzinfo:
dt = dt.astimezone(datetime.timezone.utc)
else:
dt = dt.replace(tzinfo=datetime.timezone.utc)
return dt.isoformat(timespec='seconds')

def _utcnow():
"""
Return _now_ in ISO 8601 / RFC 3339 datetime in UTC.
Returns:
datetime: Current timestamp in UTC.
"""
return datetime.datetime.now(datetime.timezone.utc).isoformat(
timespec='seconds'
)

0 comments on commit 477676c

Please sign in to comment.