Skip to content

Instantly share code, notes, and snippets.

@nfitzen
Last active November 19, 2021 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nfitzen/7b9d50f00f70b3b86f6b9a8c406c0e1a to your computer and use it in GitHub Desktop.
Save nfitzen/7b9d50f00f70b3b86f6b9a8c406c0e1a to your computer and use it in GitHub Desktop.
Converts a snowflake ID into a timestamp
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
# MIT License
#
# Copyright (C) 2015-2021 Rapptz and the discord.py contributors
# Copyright (C) 2021 nfitzen <https://github.com/nfitzen>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# the snowflake_time() function is taken from
# <https://github.com/Rapptz/discord.py/blob/45d498c1b76deaf3b394d17ccf56112fa691d160/discord/utils.py#L319-L332>
# all other code is by me (nfitzen).
import argparse
from datetime import datetime, timedelta, timezone
DISCORD_EPOCH = datetime(2015, 1, 1, tzinfo=timezone.utc).timestamp() * 1000
def snowflake_time(id: int) -> datetime:
"""
Parameters
-----------
id: :class:`int`
The snowflake ID.
Returns
--------
:class:`datetime.datetime`
An aware datetime in UTC representing the creation time of the snowflake.
"""
timestamp = ((id >> 22) + DISCORD_EPOCH) / 1000
return datetime.fromtimestamp(timestamp, tz=timezone.utc)
# back to my own code (Copyright (C) 2021 nfitzen)
def _parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Converts a snowflake ID into a timestamp.")
parser.add_argument('snowflake', help="The Snowflake ID", type=int)
snowflake = parser.add_argument_group(title="Time",description="Time to add.")
snowflake.add_argument('--weeks', '-w', default=0, type=int)
snowflake.add_argument('--days', '-d', default=0, type=int)
snowflake.add_argument('--hours', '-hr', default=0, type=int)
snowflake.add_argument('--minutes', '-m', default=0, type=int)
parser.add_argument("offset", help="The UTC offset in hours.", nargs="?", type=float, default=0.0)
return parser.parse_args()
def _main():
args = _parse_args()
dt = snowflake_time(args.snowflake)
td = timedelta(weeks=args.weeks, days=args.days, hours=args.hours, minutes=args.minutes)
tz = timezone(timedelta(hours=args.offset))
combined_dt = dt + td
combined_dt = combined_dt.astimezone(tz=tz)
print("All integer timestamps are rounded down.\n")
print("Timestamp:", combined_dt)
print("Unix time:", int(combined_dt.timestamp()))
print("ISO time:", combined_dt.isoformat(timespec="seconds"))
isolocal = combined_dt.replace(tzinfo=None).isoformat(timespec="seconds")
print("ISO local:", isolocal)
print("Simple ISO local:", isolocal.replace("-","").replace(":",""))
if __name__ == "__main__":
_main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment