def GetSystemUptime():
  """Returns the system uptime.

  Returns:
    float seconds of uptime
  Raises:
    Error: if an error occurs in calculating uptime
  """
  libc = ctypes.cdll.LoadLibrary(ctypes.util.find_library('c'))

  # 2 integers returned, might be 4 or 8 bytes each
  l = ctypes.c_int(16)
  buf = ctypes.create_string_buffer(16)

  try:
    r = libc.sysctlbyname(
        ctypes.c_char_p('kern.boottime'),
        buf, ctypes.byref(l), None, None)
  except __HOLE__:
    raise Error('Cannot find sysctlbyname()')

  if r == 0 and l.value in [8, 16]:
    if l.value == 8:  # <=10.5
      fmt = 'II'
    else:             # >=10.6
      fmt = 'QQ'
    (since_s, since_ms) = struct.unpack(fmt, ctypes.string_at(buf, l))
    uptime = time.time() - float('%s.%s' % (since_s, since_ms))
  else:
    raise Error('sysctlbyname() returned %d, oldlenp=%d' % (r, l.value))

  return uptime
dataset/ETHPy150Open google/simian/src/simian/mac/client/flight_common.py/GetSystemUptime