Sugar_Project_Public / Date_Manipulation.py
BajiraoS1's picture
Upload 3 files
9688163 verified
#!/usr/bin/env python
# coding: utf-8
# In[1]:
from datetime import datetime
from datetime import timedelta
# In[17]:
# now = datetime.now()
# In[3]:
# now
# In[4]:
def convertStrToDateTime(strDate, formatStr = '%Y-%m-%d %H:%M:%S'):
ret = datetime.strptime(strDate, formatStr)
return int(ret.timestamp() * 1000)
def getMonthStart(now : datetime):
ts = (now.replace(day=1, hour=0, minute=0, second=0, microsecond=0).timestamp()) * 1000
return int(ts)
# In[5]:
# getMonthStart(now)
# In[6]:
def getPrevMonthEnd(now : datetime):
return int(getMonthStart(now) - 1000)
# In[7]:
# getPrevMonthEnd(now)
# In[8]:
def getMonthEnd(now : datetime):
for date in (range(31, 27, -1)):
try:
ts = (now.replace(day=date, hour=23, minute=59, second=59, microsecond=0).timestamp()) * 1000
break
except:
continue
return int(ts)
# In[9]:
# getMonthEnd(now)
# In[10]:
def getNextMonthStart(now : datetime):
return getMonthEnd(now) + 1000
# In[11]:
# getNextMonthStart(now)
# In[13]:
# datetime.fromtimestamp(getNextMonthStart(now) / 1000)
# In[14]:
# datetime.fromtimestamp(getMonthStart(now) / 1000)
# In[15]:
# datetime.fromtimestamp(getPrevMonthEnd(now) / 1000)
# In[4]:
def getDayStart(now : datetime):
# ts = datetime.combine(now, datetime.min.time()).timestamp() * 1000
ts = (now.replace(hour=0, minute=0, second=0, microsecond=0).timestamp()) * 1000
return ts
# In[10]:
def getDayEnd(now : datetime):
ts = (now.replace(hour=23, minute=59, second=59, microsecond=0).timestamp()) * 1000
return ts
# In[7]:
def getPrevDayStart(now : datetime):
now = datetime.fromtimestamp((getDayStart(now) / 1000) - 1)
ts = (now.replace(hour=0, minute=0, second=0, microsecond=0).timestamp()) * 1000
return ts
def getPrevDayEnd(now : datetime):
now = datetime.fromtimestamp((getDayStart(now) / 1000) - 1)
ts = (now.replace(hour=0, minute=0, second=0, microsecond=0).timestamp()) * 1000
return ts + 23 * 3600 * 1000 + 59 * 60 * 1000 + 59 * 1000
# In[8]:
now = datetime.now()
# In[9]:
# getDayStart(now)
# In[11]:
# getDayEnd(now)
# In[12]:
# getPrevDayStart(now)
# In[ ]: