PageRenderTime 242ms CodeModel.GetById 222ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/apiary/tools/timestamp.py

https://bitbucket.org/lindenlab/apiary/
Python | 99 lines | 57 code | 18 blank | 24 comment | 9 complexity | e6e52207e459c47b5c8f4f4e44de5ec4 MD5 | raw file
 1#
 2# $LicenseInfo:firstyear=2010&license=mit$
 3# 
 4# Copyright (c) 2010, Linden Research, Inc.
 5# 
 6# Permission is hereby granted, free of charge, to any person obtaining a copy
 7# of this software and associated documentation files (the "Software"), to deal
 8# in the Software without restriction, including without limitation the rights
 9# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10# copies of the Software, and to permit persons to whom the Software is
11# furnished to do so, subject to the following conditions:
12# 
13# The above copyright notice and this permission notice shall be included in
14# all copies or substantial portions of the Software.
15# 
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22# THE SOFTWARE.
23# $/LicenseInfo$
24#
25
26import math
27
28class TimeStamp(object):
29
30    def __init__(self, s=0, us=0):
31        self.seconds = s
32        self.micros = int(us)
33    
34        if type(s) is str:
35            parts = s.split('.') # pylint: disable-msg=E1101
36            n = len(parts)
37            self.seconds = 0
38            if n >= 1:
39                self.seconds = int(parts[0])
40            if n >= 2:
41                us = int((parts[1] + "0000000")[0:7]) / 10.0
42                self.micros = int(round(us))
43        
44        self._normalize()
45
46    def __str__(self):
47        return "%d.%06d" % (self.seconds, self.micros)
48
49    def __repr__(self):
50        return "TimeStamp(%d,%d)" % (self.seconds, self.micros)
51    
52    def __hash__(self):
53        return hash(self.seconds) ^ hash(self.micros)
54        
55    def __cmp__(self, other):
56        r = cmp(self.seconds, other.seconds)
57        if r == 0:
58            r = cmp(self.micros, other.micros)
59        return r
60    
61    def __add__(self, other):
62        s = self.seconds + other.seconds
63        us = self.micros + other.micros
64        if (us >= 1000000):
65            us -= 1000000
66            s += 1
67        return TimeStamp(s, us)
68        
69    def __sub__(self, other):
70        s = self.seconds - other.seconds
71        us = self.micros - other.micros
72        if (us < 0):
73            us += 1000000
74            s -= 1
75        return TimeStamp(s, us)
76
77    def __mul__(self, number):
78        seconds = self.seconds * float(number)
79        micros = self.micros * float(number)
80        
81        return TimeStamp(seconds, micros)
82        
83    def __float__(self):
84        return self.seconds + self.micros / 1.0e6
85    
86    def _normalize(self):
87        if type(self.seconds) is float:
88            (frac_part, int_part) = math.modf(self.seconds)
89            self.seconds = int(int_part)
90            self.micros += int(round(frac_part * 1.0e6))        
91        
92        while self.micros > 1000000:
93            self.micros -= 1000000
94            self.seconds += 1
95        
96        while self.micros < -1000000:
97            self.micros += 1000000
98            self.seconds -= 1
99