File: time_range.py

package info (click to toggle)
cvs2svn 2.4.0-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,552 kB
  • ctags: 2,998
  • sloc: python: 22,334; sh: 512; perl: 121; makefile: 84
file content (50 lines) | stat: -rw-r--r-- 1,719 bytes parent folder | download | duplicates (3)
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
# (Be in -*- python -*- mode.)
#
# ====================================================================
# Copyright (c) 2006-2008 CollabNet.  All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.  The terms
# are also available at http://subversion.tigris.org/license-1.html.
# If newer versions of this license are posted there, you may use a
# newer version instead, at your option.
#
# This software consists of voluntary contributions made by many
# individuals.  For exact contribution history, see the revision
# history and logs, available at http://cvs2svn.tigris.org/.
# ====================================================================

"""This module contains a class to manage time ranges."""


class TimeRange(object):
  __slots__ = ('t_min', 't_max')

  def __init__(self):
    # Start out with a t_min higher than any incoming time T, and a
    # t_max lower than any incoming T.  This way the first T will push
    # t_min down to T, and t_max up to T, naturally (without any
    # special-casing), and successive times will then ratchet them
    # outward as appropriate.
    self.t_min = 1L<<32
    self.t_max = 0

  def add(self, timestamp):
    """Expand the range to encompass TIMESTAMP."""

    if timestamp < self.t_min:
      self.t_min = timestamp
    if timestamp > self.t_max:
      self.t_max = timestamp

  def __cmp__(self, other):
    # Sorted by t_max, and break ties using t_min.
    return cmp(self.t_max, other.t_max) or cmp(self.t_min, other.t_min)

  def __lt__(self, other):
    c = cmp(self.t_max, other.t_max)
    if 0 == c:
      return self.t_min < other.t_min
    return c < 0