def test_utc_timestamp(self):
   self.assertEqual(Timestamp(10000000).isoformat(),
                    '1970-04-26T17:46:40Z')
   self.assertEqual(Timestamp(10000000.000001).isoformat(),
                    '1970-04-26T17:46:40.000001Z')
   self.assertEqual(Timestamp(1458343379.123456).isoformat(),
                    '2016-03-18T23:22:59.123456Z')
 def test_str(self):
   self.assertEqual('Timestamp(1.234567)',
                    str(Timestamp(1.234567)))
   self.assertEqual('Timestamp(-1.234567)',
                    str(Timestamp(-1.234567)))
   self.assertEqual('Timestamp(-999999999.900000)',
                    str(Timestamp(-999999999.9)))
   self.assertEqual('Timestamp(999999999)',
                    str(Timestamp(999999999)))
   self.assertEqual('Timestamp(-999999999)',
                    str(Timestamp(-999999999)))
 def test_sort_order(self):
   self.assertEqual(
       [-63, Timestamp(-3), 2, 9, Timestamp(292.3), 500],
       sorted([9, 2, Timestamp(-3), Timestamp(292.3), -63, 500]))
   self.assertEqual(
       [4, 5, Timestamp(6), Timestamp(7), 8, 9],
       sorted([9, 8, Timestamp(7), Timestamp(6), 5, 4]))
 def test_precision(self):
   self.assertEqual(Timestamp(10000000) % 0.1, 0)
   self.assertEqual(Timestamp(10000000) % 0.05, 0)
   self.assertEqual(Timestamp(10000000) % 0.000005, 0)
   self.assertEqual(Timestamp(10000000) % Duration(0.1), 0)
   self.assertEqual(Timestamp(10000000) % Duration(0.05), 0)
   self.assertEqual(Timestamp(10000000) % Duration(0.000005), 0)
 def __init__(self, timestamp, element=None, existing_windows=None):
   self.timestamp = Timestamp.of(timestamp)
   self.element = element
   self.existing_windows = existing_windows
 def assign(self, context):
   timestamp = context.timestamp
   start = timestamp - (timestamp - self.offset) % self.period
   return [IntervalWindow(Timestamp.of(s), Timestamp.of(s) + self.size)
           for s in range(start, start - self.size, -self.period)]
 def __init__(self, size, period, offset=0):
   if size <= 0:
     raise ValueError('The size parameter must be strictly positive.')
   self.size = Duration.of(size)
   self.period = Duration.of(period)
   self.offset = Timestamp.of(offset) % size
 def __init__(self, value, timestamp):
   self.value = value
   self.timestamp = Timestamp.of(timestamp)
 def __init__(self, value, timestamp, windows):
   self.value = value
   self.timestamp = Timestamp.of(timestamp)
   self.windows = windows
 def __init__(self, start, end):
   super(IntervalWindow, self).__init__(end)
   self.start = Timestamp.of(start)
 def __init__(self, end):
   self.end = Timestamp.of(end)
  def test_arithmetic(self):
    # Supported operations.
    self.assertEqual(Timestamp(123) + 456, 579)
    self.assertEqual(Timestamp(123) + Duration(456), 579)
    self.assertEqual(456 + Timestamp(123), 579)
    self.assertEqual(Duration(456) + Timestamp(123), 579)
    self.assertEqual(Timestamp(123) - 456, -333)
    self.assertEqual(Timestamp(123) - Duration(456), -333)
    self.assertEqual(Timestamp(1230) % 456, 318)
    self.assertEqual(Timestamp(1230) % Duration(456), 318)

    # Check that direct comparison of Timestamp and Duration is allowed.
    self.assertTrue(Duration(123) == Timestamp(123))
    self.assertTrue(Timestamp(123) == Duration(123))
    self.assertFalse(Duration(123) == Timestamp(1230))
    self.assertFalse(Timestamp(123) == Duration(1230))

    # Check return types.
    self.assertEqual((Timestamp(123) + 456).__class__, Timestamp)
    self.assertEqual((Timestamp(123) + Duration(456)).__class__, Timestamp)
    self.assertEqual((456 + Timestamp(123)).__class__, Timestamp)
    self.assertEqual((Duration(456) + Timestamp(123)).__class__, Timestamp)
    self.assertEqual((Timestamp(123) - 456).__class__, Timestamp)
    self.assertEqual((Timestamp(123) - Duration(456)).__class__, Timestamp)
    self.assertEqual((Timestamp(1230) % 456).__class__, Duration)
    self.assertEqual((Timestamp(1230) % Duration(456)).__class__, Duration)

    # Unsupported operations.
    with self.assertRaises(TypeError):
      self.assertEqual(Timestamp(123) * 456, 56088)
    with self.assertRaises(TypeError):
      self.assertEqual(Timestamp(123) * Duration(456), 56088)
    with self.assertRaises(TypeError):
      self.assertEqual(456 * Timestamp(123), 56088)
    with self.assertRaises(TypeError):
      self.assertEqual(Duration(456) * Timestamp(123), 56088)
    with self.assertRaises(TypeError):
      self.assertEqual(456 - Timestamp(123), 333)
    with self.assertRaises(TypeError):
      self.assertEqual(Duration(456) - Timestamp(123), 333)
    with self.assertRaises(TypeError):
      self.assertEqual(-Timestamp(123), -123)
    with self.assertRaises(TypeError):
      self.assertEqual(-Timestamp(123), -Duration(123))
    with self.assertRaises(TypeError):
      self.assertEqual(1230 % Timestamp(456), 318)
    with self.assertRaises(TypeError):
      self.assertEqual(Duration(1230) % Timestamp(456), 318)
 def test_of(self):
   interval = Timestamp(123)
   self.assertEqual(id(interval), id(Timestamp.of(interval)))
   self.assertEqual(interval, Timestamp.of(123.0))
   with self.assertRaises(TypeError):
     Timestamp.of(Duration(10))
def windmill_to_harness_timestamp(windmill_timestamp):
  # The timestamp given by Windmill is in microseconds.
  return Timestamp(micros=windmill_timestamp)