PageRenderTime 305ms CodeModel.GetById 159ms app.highlight 8ms RepoModel.GetById 136ms app.codeStats 1ms

/historical/timestamp.py

https://bitbucket.org/lindenlab/apiary/
Python | 81 lines | 46 code | 11 blank | 24 comment | 6 complexity | 00717adf60cf7f02d21cdca842a36721 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        t = type(s)
35        if t is float:
36            (frac_part, int_part) = math.modf(s)
37            self.seconds = int(int_part)
38            self.micros = int(round(frac_part * 1.0e6))
39        elif t is str:
40            parts = s.split('.')
41            n = len(parts)
42            self.seconds = 0
43            if n >= 1:
44                self.seconds = int(parts[0])
45            if n >= 2:
46                us = int((parts[1] + "0000000")[0:7]) / 10.0
47                self.micros = int(round(us))
48
49    def __str__(self):
50        return "%d.%06d" % (self.seconds, self.micros)
51
52    def __repr__(self):
53        return "TimeStamp(%d,%d)" % (self.seconds, self.micros)
54    
55    def __hash__(self):
56        return hash(self.seconds) ^ hash(self.micros)
57        
58    def __cmp__(self, other):
59        r = cmp(self.seconds, other.seconds)
60        if r == 0:
61            r = cmp(self.micros, other.micros)
62        return r
63    
64    def __add__(self, other):
65        s = self.seconds + other.seconds
66        us = self.micros + other.micros
67        if (us >= 1000000):
68            us -= 1000000
69            s += 1
70        return TimeStamp(s, us)
71        
72    def __sub__(self, other):
73        s = self.seconds - other.seconds
74        us = self.micros - other.micros
75        if (us < 0):
76            us += 1000000
77            s -= 1
78        return TimeStamp(s, us)
79
80    def __float__(self):
81        return self.seconds + self.micros / 1.0e6