Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Message from discussion date and time range checking

View parsed - Show only message text

Path: g2news1.google.com!news3.google.com!newshub.sdsu.edu!elnk-nf2-pas!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread1.news.pas.earthlink.net.POSTED!c1928424!not-for-mail
From: Andrew Dalke <da...@dalkescientific.com>
Organization: Dalke Scientific Software, LLC
Subject: Re: date and time range checking
User-Agent: Pan/0.14.2 (This is not a psychotic episode. It's a cleansing moment of clarity.)
Message-Id: <pan.2005.06.02.19.38.51.885034@dalkescientific.com>
Newsgroups: comp.lang.python
References: <d7nicc$2mrh$1@news.tsystems.kiev.ua>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Lines: 58
Date: Thu, 02 Jun 2005 19:38:57 GMT
NNTP-Posting-Host: 66.167.128.226
X-Complaints-To: abuse@earthlink.net
X-Trace: newsread1.news.pas.earthlink.net 1117741137 66.167.128.226 (Thu, 02 Jun 2005 12:38:57 PDT)
NNTP-Posting-Date: Thu, 02 Jun 2005 12:38:57 PDT

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
				da...@dalkescientific.com


Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google