date and time range checking

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Maksim Kasimov

    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:[color=blue][color=green][color=darkred]
    >>> a = xrange(1,5)
    >>> b = 3
    >>> if b in a:[/color][/color][/color]
    .... print "OK"


    thanks

    --
    Best regards,
    Maksim Kasimov
    mailto: kasimov@i.com.u a
  • Peter Hansen

    #2
    Re: date and time range checking

    Maksim Kasimov wrote:[color=blue]
    > what is the "pythonic" way to check is the date/time value in the given
    > periods range?[/color]

    Something like this, though I won't make strong claims of
    "pythonicne ss". 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(tim e.strptime(s, fmt))
    return datetime.fromti mestamp(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)

    Comment

    • Andrew Dalke

      #3
      Re: date and time range checking

      Maksim Kasimov wrote:[color=blue]
      > 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[/color]


      [color=blue]
      > what is the "pythonic" way to check is the date/time value in the given periods range?[/color]

      [color=blue][color=green][color=darkred]
      >>> import datetime
      >>> t1 = datetime.dateti me(2005, 6, 8, 12, 30)
      >>> t2 = datetime.dateti me(2005, 6, 10, 15, 30)
      >>> t = datetime.dateti me(2005, 6, 9, 14, 00)
      >>> if t1 < t < t2:[/color][/color][/color]
      .... print "In range"
      ....
      In range[color=blue][color=green][color=darkred]
      >>> t = datetime.dateti me(2005, 6, 8, 14, 00)
      >>> if t1 < t < t2:[/color][/color][/color]
      .... print "In range"
      ....
      In range[color=blue][color=green][color=darkred]
      >>> t = datetime.dateti me(2005, 6, 7, 14, 00)
      >>>
      >>> if t1 < t < t2:[/color][/color][/color]
      .... print "In range"
      ....[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      If you want to use the "in" syntax
      [color=blue][color=green][color=darkred]
      >>> class InRange:[/color][/color][/color]
      .... def __init__(self, low, high):
      .... self.low = low
      .... self.high = high
      .... def __contains__(se lf, obj):
      .... return self.low < obj < self.high
      ....[color=blue][color=green][color=darkred]
      >>> r = InRange(t1, t2)
      >>> datetime.dateti me(2005, 6, 7, 14, 00) in r[/color][/color][/color]
      False[color=blue][color=green][color=darkred]
      >>> datetime.dateti me(2005, 6, 8, 14, 00) in r[/color][/color][/color]
      True[color=blue][color=green][color=darkred]
      >>> datetime.dateti me(2005, 6, 9, 14, 00) in r[/color][/color][/color]
      True[color=blue][color=green][color=darkred]
      >>> datetime.dateti me(2005, 6, 9, 18, 00) in r[/color][/color][/color]
      True[color=blue][color=green][color=darkred]
      >>> datetime.dateti me(2005, 6, 10, 18, 00) in r[/color][/color][/color]
      False[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      Andrew
      dalke@dalkescie ntific.com

      Comment

      Working...