Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > date and time range checking

Reply
Thread Tools

date and time range checking

 
 
Maksim Kasimov
Guest
Posts: n/a
 
      06-02-2005

there are few of a time periods, for example:
2005-06-08 12:30 -> 2005-06-10 15:30,
2005-06-12 12:30 -> 2005-06-14 15:30

and there is some date and time value:
2005-06-11 12:30

what is the "pythonic" way to check is the date/time value in the given periods range?

something like xrange:
>>> a = xrange(1,5)
>>> b = 3
>>> if b in a:

.... print "OK"


thanks

--
Best regards,
Maksim Kasimov
mailto:
 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      06-02-2005
Maksim Kasimov wrote:
> what is the "pythonic" way to check is the date/time value in the given
> periods range?


Something like this, though I won't make strong claims of
"pythonicness". If you want to use the "in" keyword you'll want a
custom class and overriding of __contains__.

import time
from datetime import datetime

def make_datetime(s, fmt='%Y-%m-%d %H:%M'):
'''convert string to datetime'''
ts = time.mktime(time.strptime(s, fmt))
return datetime.fromtimestamp(ts)


def inRange(s, ranges):
dt = make_datetime(s)
for begin,end in ranges:
if begin <= dt <= end:
return True
else:
return False


ranges = [(make_datetime(b), make_datetime(e)) for (b,e) in [
('2005-06-08 12:30', '2005-06-10 15:30'),
('2005-06-12 12:30', '2005-06-14 15:30'),
]]

print inRange('2005-06-11 12:30', ranges)
 
Reply With Quote
 
 
 
 
Andrew Dalke
Guest
Posts: n/a
 
      06-02-2005
Maksim Kasimov wrote:
> there are few of a time periods, for example:
> 2005-06-08 12:30 -> 2005-06-10 15:30,
> 2005-06-12 12:30 -> 2005-06-14 15:30
>
> and there is some date and time value:
> 2005-06-11 12:30




> what is the "pythonic" way to check is the date/time value in the given periods range?



>>> import datetime
>>> t1 = datetime.datetime(2005, 6, 8, 12, 30)
>>> t2 = datetime.datetime(2005, 6, 10, 15, 30)
>>> t = datetime.datetime(2005, 6, 9, 14, 00)
>>> if t1 < t < t2:

.... print "In range"
....
In range
>>> t = datetime.datetime(2005, 6, 8, 14, 00)
>>> if t1 < t < t2:

.... print "In range"
....
In range
>>> t = datetime.datetime(2005, 6, 7, 14, 00)
>>>
>>> if t1 < t < t2:

.... print "In range"
....
>>>


If you want to use the "in" syntax

>>> class InRange:

.... def __init__(self, low, high):
.... self.low = low
.... self.high = high
.... def __contains__(self, obj):
.... return self.low < obj < self.high
....
>>> r = InRange(t1, t2)
>>> datetime.datetime(2005, 6, 7, 14, 00) in r

False
>>> datetime.datetime(2005, 6, 8, 14, 00) in r

True
>>> datetime.datetime(2005, 6, 9, 14, 00) in r

True
>>> datetime.datetime(2005, 6, 9, 18, 00) in r

True
>>> datetime.datetime(2005, 6, 10, 18, 00) in r

False
>>>


Andrew


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
w3.org suggestion .. page, date, time and topic, date, time code (wish list). Keith Cochrane HTML 2 08-06-2006 06:57 AM
covert time from date Hour min sec format to epoch time i.e time since 1 jan 1970 in C Summu82 C Programming 5 06-07-2006 02:51 PM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Given a date, how to find the beginning date and ending date of that week Matt ASP .Net 1 11-08-2003 09:14 PM
Date & Time chooser for java 1.1 - using only the mouse to select time & date Chris Berg Java 0 10-27-2003 10:59 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57