Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
date and time range checking
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Maksim Kasimov  
View profile  
 More options Jun 2 2005, 2:11 pm
Newsgroups: comp.lang.python
From: Maksim Kasimov <kasi...@i.com.ua>
Date: Thu, 02 Jun 2005 21:11:56 +0300
Local: Thurs, Jun 2 2005 2:11 pm
Subject: date and time range checking

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: kasi...@i.com.ua


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Hansen  
View profile  
 More options Jun 2 2005, 2:32 pm
Newsgroups: comp.lang.python
From: Peter Hansen <pe...@engcorp.com>
Date: Thu, 02 Jun 2005 14:32:53 -0400
Local: Thurs, Jun 2 2005 2:32 pm
Subject: Re: date and time range checking

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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Dalke  
View profile  
(1 user)  More options Jun 2 2005, 3:38 pm
Newsgroups: comp.lang.python
From: Andrew Dalke <da...@dalkescientific.com>
Date: Thu, 02 Jun 2005 19:38:57 GMT
Local: Thurs, Jun 2 2005 3:38 pm
Subject: Re: date and time range checking

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

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google