Inclusive date ranges in python -
i didn't phrase question correctly , result, none of answers quite hit mark.
what wanted know is, what's best way time
return end of day in epoch milliseconds, if given date, rather both date , time?
here's how i'm doing @ moment:
def get_epoch_endofday(day): return time.mktime(time.strptime('005923'+day,"%s%m%h%d%m%y"))*1000
the function takes date in form ddmmyy, , spits out epoch milliseconds on day @ 23:59:00.
i wondered, there more elegant way of doing this?
it wasn't clear @ first, want create interval given day (say 2016-02-14
), contains first , last instant of day. means:
first instant: 2016-02-14 00:00:00.000
last instant: 2016-02-14 23:59:59.999
the function below returns interval tupel. works this:
- create
start
of interval creating new date instance on same day hours, minutes , milliseconds set0
- create interval spanning
23
hours,59
minutes,59
seconds ,999999
microseconds - add interval beginning of day
code:
from datetime import datetime, timedelta # 2016-02-08 11:39:31.093209 day = datetime.now() def get_day_interval(day): start = datetime(day.year, day.month, day.day) interval = timedelta(seconds=59, microseconds=999999, minutes=59, hours=23) return (start, start + interval) (start, end) = get_day_interval(day) # 2016-02-08 00:00:00 print(start) # 2016-02-08 23:59:59.999999 print(end)
consult python docs more options.
if interval can exclusive, code becomes simpler, see summary of this answer.
Comments
Post a Comment