Revision 2241e2b9 lib/cli.py

b/lib/cli.py
707 707
    return '?'
708 708
  sec, usec = ts
709 709
  return time.strftime("%F %T", time.localtime(sec)) + ".%06d" % usec
710

  
711

  
712
def ParseTimespec(value):
713
  """Parse a time specification.
714

  
715
  The following suffixed will be recognized:
716

  
717
    - s: seconds
718
    - m: minutes
719
    - h: hours
720
    - d: day
721
    - w: weeks
722

  
723
  Without any suffix, the value will be taken to be in seconds.
724

  
725
  """
726
  value = str(value)
727
  if not value:
728
    raise errors.OpPrereqError("Empty time specification passed")
729
  suffix_map = {
730
    's': 1,
731
    'm': 60,
732
    'h': 3600,
733
    'd': 86400,
734
    'w': 604800,
735
    }
736
  if value[-1] not in suffix_map:
737
    try:
738
      value = int(value)
739
    except ValueError:
740
      raise errors.OpPrereqError("Invalid time specification '%s'" % value)
741
  else:
742
    multiplier = suffix_map[value[-1]]
743
    value = value[:-1]
744
    if not value: # no data left after stripping the suffix
745
      raise errors.OpPrereqError("Invalid time specification (only"
746
                                 " suffix passed)")
747
    try:
748
      value = int(value) * multiplier
749
    except ValueError:
750
      raise errors.OpPrereqError("Invalid time specification '%s'" % value)
751
  return value

Also available in: Unified diff