Aluf

Time-status (chatango bot cmd)

Jan 18th, 2015
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. def uptime():
  2.  
  3.      try:
  4.          f = open( "/proc/uptime" )
  5.          contents = f.read().split()
  6.          f.close()
  7.      except:
  8.         return "Cannot open uptime file: /proc/uptime"
  9.  
  10.      total_seconds = float(contents[0])
  11.  
  12.      # Helper vars:
  13.      MINUTE  = 60
  14.      HOUR    = MINUTE * 60
  15.      DAY     = HOUR * 24
  16.  
  17.      # Get the days, hours, etc:
  18.      days    = int( total_seconds / DAY )
  19.      hours   = int( ( total_seconds % DAY ) / HOUR )
  20.      minutes = int( ( total_seconds % HOUR ) / MINUTE )
  21.      seconds = int( total_seconds % MINUTE )
  22.  
  23.      # Build up the pretty string (like this: "N days, N hours, N minutes, N seconds")
  24.      string = ""
  25.      if days > 0:
  26.          string += str(days) + " " + (days == 1 and "day" or "days" ) + ", "
  27.      if len(string) > 0 or hours > 0:
  28.          string += str(hours) + " " + (hours == 1 and "hour" or "hours" ) + ", "
  29.      if len(string) > 0 or minutes > 0:
  30.          string += str(minutes) + " " + (minutes == 1 and "minute" or "minutes" ) + ", "
  31.      string += str(seconds) + " " + (seconds == 1 and "second" or "seconds" )
  32.  
  33.      return string;
Advertisement
Add Comment
Please, Sign In to add comment