Created
January 17, 2018 21:13
-
-
Save chriscauley/8ced5fa6d5a2a970a3b9bcbd9ae7b3f8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# DANGERZONE!! This is globally overriding default python behavior. Caveat emptor | |
# FAKE_DATE=1999-01-01 python | |
# >>> import datetime_hack, datetime | |
# >>> datetime.date.today() | |
# 1999-01-01 | |
import datetime | |
import os | |
class Datetime(datetime.datetime): | |
@classmethod | |
def now(cls,*args,**kwargs): | |
dt = super(Datetime,cls).now(*args,**kwargs) | |
if os.environ.get("FAKE_DATE"): | |
y,m,d = os.environ['FAKE_DATE'].split("-") | |
dt = dt.replace(year=int(y),month=int(m),day=int(d)) | |
if os.environ.get("FAKE_TIME"): | |
h,m = os.environ.FAKE_TIME.split(':') | |
dt = dt.replace(hour=int(h),minute=int(m)) | |
return dt | |
class Date(datetime.date): | |
@classmethod | |
def today(cls,*args,**kwargs): | |
if os.environ.get("FAKE_DATE"): | |
y,m,d = os.environ['FAKE_DATE'].split("-") | |
return cls(year=int(y),month=int(m),day=int(d)) | |
return super(Date,cls).today(*args,**kwargs) | |
datetime.datetime = Datetime | |
datetime.date = Date |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment