Skip to content

Instantly share code, notes, and snippets.

@bitaxis
Last active December 14, 2018 19:23
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 bitaxis/105df8d4142e761426b45179b49e6e90 to your computer and use it in GitHub Desktop.
Save bitaxis/105df8d4142e761426b45179b49e6e90 to your computer and use it in GitHub Desktop.
import datetime
import time
class µstamp(datetime.datetime):
def __init__(self, year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0):
super().__init__()
def __add__(self, other):
if type(other) is int: other = datetime.timedelta(days=other)
result = self.datetime() + other
return µstamp.fromtimestamp(result.timestamp(), self.tzinfo)
def __int__(self):
return self.µs()
def __repr__(self):
return '%s (%d)' % (self.datetime(), self.µs())
def __str__(self):
return repr(self)
def __sub__(self, other):
if type(other) is int: other = datetime.timedelta(days=other)
result = self.datetime() - other
return µstamp.fromtimestamp(result.timestamp(), self.tzinfo)
@staticmethod
def dayofweek(dow):
'''
Returns a µstamp instance for a day in the current week based on the dow argument,
where 0 is for Sunday and 6 is for Saturday.
'''
today = datetime.date.today()
offset = dow - (today.weekday() + 1) % 7
someday = today + datetime.timedelta(days=offset)
return µstamp(someday.year, someday.month, someday.day)
@staticmethod
def fromdatetime(dt):
return µstamp.fromtimestamp(dt.timestamp(), dt.tzinfo)
@staticmethod
def fromtimestamp(timestamp_in_seconds, tzinfo=None):
ts = datetime.datetime.fromtimestamp(timestamp_in_seconds, tzinfo)
return µstamp(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second, ts.microsecond, ts.tzinfo, fold=ts.fold)
@staticmethod
def fromµs(timestamp_in_microseconds, tzinfo=None):
return µstamp.fromtimestamp(timestamp_in_microseconds / 1000000., tzinfo)
@staticmethod
def saturday():
return µstamp.dayofweek(6)
@staticmethod
def sunday():
return µstamp.dayofweek(0)
@staticmethod
def today():
today = datetime.date.today()
return µstamp(today.year, today.month, today.day)
@staticmethod
def tomorrow():
tomorrow = datetime.datetime.today() + datetime.timedelta(days=1)
return µstamp(tomorrow.year, tomorrow.month, tomorrow.day)
@staticmethod
def yesterday():
yesterday = datetime.datetime.today() - datetime.timedelta(days=1)
return µstamp(yesterday.year, yesterday.month, yesterday.day)
def datetime(self):
return datetime.datetime.fromtimestamp(self.timestamp(), self.tzinfo)
def offset(self, days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0):
'''
Convenience method; apply a time delta to a µstamp instance and return a new one.
'''
return self + datetime.timedelta(days, seconds, microseconds, milliseconds, minutes, hours, weeks)
def timestamp(self):
return super().timestamp()
def weekday(self):
'''
Returns an integer representing day of week that is between 0 (Sunday) and 6 (Saturday).
'''
return (super().weekday() + 1) % 7
def µs(self):
return int(self.timestamp() * 1000000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment