PageRenderTime 12ms CodeModel.GetById 50ms RepoModel.GetById 0ms 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. import math
  26. class TimeStamp(object):
  27. def __init__(self, s=0, us=0):
  28. self.seconds = s
  29. self.micros = int(us)
  30. if type(s) is str:
  31. parts = s.split('.') # pylint: disable-msg=E1101
  32. n = len(parts)
  33. self.seconds = 0
  34. if n >= 1:
  35. self.seconds = int(parts[0])
  36. if n >= 2:
  37. us = int((parts[1] + "0000000")[0:7]) / 10.0
  38. self.micros = int(round(us))
  39. self._normalize()
  40. def __str__(self):
  41. return "%d.%06d" % (self.seconds, self.micros)
  42. def __repr__(self):
  43. return "TimeStamp(%d,%d)" % (self.seconds, self.micros)
  44. def __hash__(self):
  45. return hash(self.seconds) ^ hash(self.micros)
  46. def __cmp__(self, other):
  47. r = cmp(self.seconds, other.seconds)
  48. if r == 0:
  49. r = cmp(self.micros, other.micros)
  50. return r
  51. def __add__(self, other):
  52. s = self.seconds + other.seconds
  53. us = self.micros + other.micros
  54. if (us >= 1000000):
  55. us -= 1000000
  56. s += 1
  57. return TimeStamp(s, us)
  58. def __sub__(self, other):
  59. s = self.seconds - other.seconds
  60. us = self.micros - other.micros
  61. if (us < 0):
  62. us += 1000000
  63. s -= 1
  64. return TimeStamp(s, us)
  65. def __mul__(self, number):
  66. seconds = self.seconds * float(number)
  67. micros = self.micros * float(number)
  68. return TimeStamp(seconds, micros)
  69. def __float__(self):
  70. return self.seconds + self.micros / 1.0e6
  71. def _normalize(self):
  72. if type(self.seconds) is float:
  73. (frac_part, int_part) = math.modf(self.seconds)
  74. self.seconds = int(int_part)
  75. self.micros += int(round(frac_part * 1.0e6))
  76. while self.micros > 1000000:
  77. self.micros -= 1000000
  78. self.seconds += 1
  79. while self.micros < -1000000:
  80. self.micros += 1000000
  81. self.seconds -= 1