0

I didn't phrase this question correctly and as a result, none of the answers quite hit the mark.

What I wanted to know is, what's the best way to get time to return the end of a day in epoch milliseconds, if given just the date, rather than both date and time?

Here's how I'm doing it at the moment:

def get_epoch_endofday(day):
    return time.mktime(time.strptime('005923'+day,"%S%M%H%d%m%y"))*1000

The function takes the date in the form ddmmyy, and then spits out the epoch milliseconds on that day at 23:59:00.

I wondered, is there a more elegant way of doing this?

Charon
  • 2,344
  • 6
  • 25
  • 44
  • I'm not sure I understand what you want. Can you add an example of the kind of values you are looking for? What second date, why is `00:00` exclusive? – Martijn Pieters Feb 08 '16 at 10:33
  • And if you are working with just dates, why not use `datetime.date()` objects? – Martijn Pieters Feb 08 '16 at 10:34
  • More precisely what value should `day` have and what you expect as a return value from `get_epoch`. – Genti Saliu Feb 08 '16 at 10:34
  • @MartijnPieters - Please see my edit. The dataset has the dates recorded in epoch miliseconds. – Charon Feb 08 '16 at 10:42
  • @GentiSaliu - I've completely rephrased my question. Please let me know if you still need more context. – Charon Feb 08 '16 at 11:08
  • You want to create an interval: beginning should be for eg. `2016-04-16 00:00:00`, end `2016-04-17 23:59:59`. Now you get for the end part `2016-04-17 00:00:00`, correct? – Genti Saliu Feb 08 '16 at 11:13
  • @GentiSaliu - correct – Charon Feb 08 '16 at 11:15
  • The end of the interval should be on the same day, just 23 hours, 59 minutes, 59 seconds later? – Genti Saliu Feb 08 '16 at 11:15
  • @GentiSaliu - It should be at the end of the day which I specify, yes. – Charon Feb 08 '16 at 11:16
  • 1
    If any of the answers given below address your question, pease mark it as such, so that the thread is closed and removed from the board. Otherwise, please elaborate. Thanks. – Pouria Feb 08 '16 at 12:01

2 Answers2

2

It wasn't very clear at first, but you want to create an interval for a given day (say 2016-02-14), which contains the first and last instant of that day. That means:

First instant: 2016-02-14 00:00:00.000

Last instant: 2016-02-14 23:59:59.999

The function below returns that interval as a tupel. It works like this:

  • create the start of the interval by creating a new date instance on the same day with hours, minutes and milliseconds all set to 0
  • create an interval spanning 23 hours, 59 minutes, 59 seconds and 999999 microseconds
  • add this interval to the beginning of the 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 the Python docs for more options.

If the interval can be exclusive, the code becomes simpler, see the summary of this answer.

Community
  • 1
  • 1
Genti Saliu
  • 2,643
  • 4
  • 23
  • 43
2

Generators

Here is how you can create a function that produces an inclusive range:

def inc_range(stop, start=0, step=1):
    numerator = start

    while numerator<=stop:
        yield numerator
        numerator += step


a = [val for val in inc_range(10)]
print(a)

Displays:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Now this method can be adapted for anything you want; date or otherwise.

Date range generator

Here it the method tailored to your needs:

from datetime import date, timedelta 


def date_range(stop, start, step=1):
    delta_date = stop - start
    numerator = 0

    while numerator <= delta_date.days:
        yield str(start + timedelta(numerator))
        numerator += step


date_start = date(2014, 3, 15)
date_end = date(2014, 3, 21)

a = [val for val in date_range(date_end, date_start)]
print(a)

Displays:

['2014-03-15', '2014-03-16', '2014-03-17', '2014-03-18', '2014-03-19', '2014-03-20', '2014-03-21']

Note that if you don't include str() when yielding in the generator, the response would be an object of datetime.date() instead.

Hope this answers your question.

Community
  • 1
  • 1
Pouria
  • 1,081
  • 9
  • 24