Last active
August 23, 2017 14:00
-
-
Save VITIMan/d2982363cb9d63ed355d to your computer and use it in GitHub Desktop.
Date Range Iterator in minutes
This file contains 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
# python imports | |
import datetime | |
RANGE_TIME = 15 | |
def date_range_iterator(start_date, end_date, delta=None): | |
""" Iterator for date ranges, given an start_date, end_date and an | |
optional delta (the range) | |
Args: | |
start_date: A datetime to start the range | |
end_date: A datetime to end the range | |
Yields: | |
a tuple of datetime, with the range. | |
Raises: | |
StopIteration: If end_date is less than start_date | |
""" | |
DEFAULT_RANGE_TIME = 15 | |
if end_date < start_date: | |
raise StopIteration | |
if delta is None: | |
delta = datetime.timedelta(minutes=DEFAULT_RANGE_TIME) | |
while start_date < end_date: | |
yield start_date, start_date + delta | |
start_date += delta | |
if __name__ == '__main__': | |
start_date = datetime.datetime(2015, 10, 8, 13, 15, 00) | |
end_date = datetime.datetime(2015, 10, 8, 18, 15, 00) | |
for start_range, end_range in date_range_iterator( | |
start_date, | |
end_date, | |
delta=datetime.timedelta(minutes=RANGE_TIME)): | |
print("START: {} - END: {}".format(start_range, end_range)) |
This file contains 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
# https://stackoverflow.com/a/29798653 | |
""" | |
/** times **/ | |
01 00:00 02 00:30 03 01:00 04 01:30 05 02:00 06 02:30 07 03:00 08 03:30 | |
09 04:00 10 04:30 11 05:00 12 05:30 13 06:00 14 06:30 15 07:00 16 07:30 | |
17 08:00 18 08:30 19 09:00 20 09:30 21 10:00 22 10:30 23 11:00 24 11:30 | |
25 12:00 26 12:30 27 13:00 28 13:30 29 14:00 30 14:30 31 15:00 32 15:30 | |
33 16:00 34 16:30 35 17:00 36 17:30 37 18:00 38 18:30 39 19:00 40 19:30 | |
41 20:00 42 20:30 43 21:00 44 21:30 45 22:00 46 22:30 47 23:00 48 23:30 | |
49-57 unused | |
/** days of week **/ | |
58 Sunday 59 Monday 60 Tuesday 61 Wednesday 62 Thursday 63 Friday 64 Saturday | |
Ok so now how do you use this? | |
If you wanted to say a business was open sunday - saturday 7am to 8am AND 9am to 10am you would add up (using a bitwise OR operator) | |
take all the "bits" from above and OR them together, this can be accomplished by doing 2 ^ (bit-1) | |
$hours = 2^14 | 2^15 | 2^17 | 2^18 | 2^57 | 2^58 | 2^59 | 2^60 | 2^61 | 2^62 | 2^63; | |
Then do do your querying you simply do a bitwise AND. | |
/** this is checking to see if it's open at 7am and 9am on Thursday **/ | |
$query = (2^14 & 2^17 & 2^61); | |
/** this will match only if all conditions are true **/ | |
$isOpen = $query & $hours === $query; | |
/** this will match if some or all conditions are true **/ | |
$isOpen = $query & $hours > 0; | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment