def _operate(self, other, op_func): if isinstance(other, UTCDateTime): ndigits = min(self.precision, other.precision) - 9 if self.precision != other.precision: msg = ('Comparing UTCDateTime objects of different precision' ' is not defined and may lead to surpising behavior') warnings.warn(msg) a = py3_round(self._ns, ndigits) b = py3_round(other._ns, ndigits) return op_func(a, b) else: try: return self._operate(UTCDateTime(other), op_func) except TypeError: return False
def _operate(self, other, op_func): if isinstance(other, UTCDateTime): ndigits = min(self.precision, other.precision) - 9 if self.precision != other.precision: msg = ('Comparing UTCDateTime objects of different precision' ' is not defined will raise an Exception in a future' ' version of obspy') warnings.warn(msg, ObsPyDeprecationWarning) a = py3_round(self._ns, ndigits) b = py3_round(other._ns, ndigits) return op_func(a, b) else: try: return self._operate(UTCDateTime(other), op_func) except TypeError: return False
def test_py3_round(self): """ Ensure py3_round rounds correctly and returns expected data types. """ # list of tuples; (input, ndigits, expected, excpected type) test_list = [ (.222, 2, .22, float), (1516047903968282880, -3, 1516047903968283000, int), (1.499999999, None, 1, int), (.0222, None, 0, int), (12, -1, 10, int), (15, -1, 20, int), (15, -2, 0, int), ] for number, ndigits, expected, expected_type in test_list: # this is a fix for py3.4 which cannot take None as ndigits if ndigits is not None: out = py3_round(number, ndigits) else: out = py3_round(number) self.assertEqual(out, expected) self.assertIsInstance(out, expected_type)
def _get_microsecond(self): """ Returns microseconds as an integer. :rtype: int :return: Returns microseconds as an integer. .. rubric:: Example >>> dt = UTCDateTime(2012, 2, 11, 10, 11, 12, 345234) >>> dt.microsecond 345234 """ return int(py3_round(self._ns % 10**9, self.precision - 9) // 1000)
def __str__(self): """ Returns ISO8601 string representation from current UTCDateTime object. :return: string .. rubric:: Example >>> dt = UTCDateTime(2008, 10, 1, 12, 30, 35, 45020) >>> str(dt) '2008-10-01T12:30:35.045020Z' """ dt = self.datetime time_str = YMDHMS_FORMAT % tuple(getattr(dt, x) for x in YMDHMS) if self.precision > 0: ns = py3_round(self.ns, self.precision - 9) ns_str = ('%09d' % (ns % 10 ** 9))[:self.precision] time_str += ('.' + ns_str) return time_str + 'Z'
def __str__(self): """ Returns ISO8601 string representation from current UTCDateTime object. :return: string .. rubric:: Example >>> dt = UTCDateTime(2008, 10, 1, 12, 30, 35, 45020) >>> str(dt) '2008-10-01T12:30:35.045020Z' """ dt = self.datetime time_str = YMDHMS_FORMAT % tuple(getattr(dt, x) for x in YMDHMS) if self.precision > 0: ns = py3_round(self.ns, self.precision - 9) ns_str = ('%09d' % (ns % 10**9))[:self.precision] time_str += ('.' + ns_str) return time_str + 'Z'