Ejemplo n.º 1
0
    def test_basics(self):
        now = timestamp.now()
        self.assertTrue(timestamp.is_valid(now))
        # Check that we can round-trip the timestamp produced by now()
        # through the human-readable format.
        human_readable = timestamp.get_human_readable(now)
        parsed = timestamp.parse_human_readable(human_readable)
        self.assertEqual(now, parsed)
        # Now check that a known example encodes correctly.
        ts = 1228080954
        self.assertTrue(timestamp.is_valid(ts))
        human_readable = timestamp.get_human_readable(ts)
        self.assertEqual("20081130-153554", human_readable)
        parsed = timestamp.parse_human_readable(human_readable)
        self.assertEqual(ts, parsed)

        # Make sure that calling timestamp.get_human_readable w/o an
        # argument returns a value for now.  We retry a few times just
        # in case we are very unlucky and call timestamp.now() for the
        # second time after the second has incremented.
        for _ in range(3):
            now_str = timestamp.get_human_readable(timestamp.now())
            no_arg_str = timestamp.get_human_readable()
            if no_arg_str == now_str:
                break
        else:
            self.assertTrue(False)

        # Check that is_valid will reject bad timestamps.
        self.assertFalse(timestamp.is_valid(-1))
        self.assertFalse(timestamp.is_valid(0))
        self.assertFalse(timestamp.is_valid(1000))  # The distant past
        self.assertFalse(timestamp.is_valid(1000000000000))  # The far future
        self.assertFalse(
            timestamp.is_valid(timestamp._MIN_REASONABLE_TIMESTAMP - 1))
        self.assertFalse(
            timestamp.is_valid(timestamp._MAX_REASONABLE_TIMESTAMP + 1))

        # Should raise ValueError on bad inputs.
        self.assertRaises(ValueError, timestamp.get_human_readable, 0)
        self.assertRaises(ValueError, timestamp.parse_human_readable,
                          "malformed")
        self.assertRaises(ValueError, timestamp.parse_human_readable,
                          "20081356-999999")
    def test_basics(self):
        now = timestamp.now()
        self.assertTrue(timestamp.is_valid(now))
        # Check that we can round-trip the timestamp produced by now()
        # through the human-readable format.
        human_readable = timestamp.get_human_readable(now)
        parsed = timestamp.parse_human_readable(human_readable)
        self.assertEqual(now, parsed)
        # Now check that a known example encodes correctly.
        ts = 1228080954
        self.assertTrue(timestamp.is_valid(ts))
        human_readable = timestamp.get_human_readable(ts)
        self.assertEqual("20081130-153554", human_readable)
        parsed = timestamp.parse_human_readable(human_readable)
        self.assertEqual(ts, parsed)

        # Make sure that calling timestamp.get_human_readable w/o an
        # argument returns a value for now.  We retry a few times just
        # in case we are very unlucky and call timestamp.now() for the
        # second time after the second has incremented.
        for _ in range(3):
            now_str = timestamp.get_human_readable(timestamp.now())
            no_arg_str = timestamp.get_human_readable()
            if no_arg_str == now_str:
                break
        else:
            self.assertTrue(False)

        # Check that is_valid will reject bad timestamps.
        self.assertFalse(timestamp.is_valid(-1))
        self.assertFalse(timestamp.is_valid(0))
        self.assertFalse(timestamp.is_valid(1000))  # The distant past
        self.assertFalse(timestamp.is_valid(1000000000000))  # The far future
        self.assertFalse(timestamp.is_valid(timestamp._MIN_REASONABLE_TIMESTAMP - 1))
        self.assertFalse(timestamp.is_valid(timestamp._MAX_REASONABLE_TIMESTAMP + 1))

        # Should raise ValueError on bad inputs.
        self.assertRaises(ValueError, timestamp.get_human_readable, 0)
        self.assertRaises(ValueError, timestamp.parse_human_readable, "malformed")
        self.assertRaises(ValueError, timestamp.parse_human_readable, "20081356-999999")
Ejemplo n.º 3
0
def parse(ufid_str):
    """Extract information from a UFID string.
    
    Args:
      ufid_str: A string, probably produced by a prior call to ufid()
      
    Returns:
      A (volume number, deposit timestamp, fingerprint) 3-tuple.

    Raises:
      ValueError: if ufid_str is invalid.
    """
    match = _UFID_RE.match(ufid_str)
    if match:
        vol = int(match.group(1), 16)
        ts = timestamp.parse_human_readable(match.group(2))
        fp = match.group(3)
        if vol > 0 and timestamp.is_valid(ts) and fingerprint.is_valid(fp):
            return vol, ts, fp
    raise ValueError("Bad UFID string \"%s\"" % ufid_str)
Ejemplo n.º 4
0
def parse(ufid_str):
    """Extract information from a UFID string.
    
    Args:
      ufid_str: A string, probably produced by a prior call to ufid()
      
    Returns:
      A (volume number, deposit timestamp, fingerprint) 3-tuple.

    Raises:
      ValueError: if ufid_str is invalid.
    """
    match = _UFID_RE.match(ufid_str)
    if match:
        vol = int(match.group(1), 16)
        ts = timestamp.parse_human_readable(match.group(2))
        fp = match.group(3)
        if vol > 0 and timestamp.is_valid(ts) and fingerprint.is_valid(fp):
            return vol, ts, fp
    raise ValueError("Bad UFID string \"%s\"" % ufid_str)