-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move time related functions to helpers
- Loading branch information
1 parent
0277486
commit 477676c
Showing
2 changed files
with
31 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
) |