SHARE
TWEET

Untitled

a guest Sep 20th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from datetime import datetime
  2.  
  3. def uptime(start_datetime):
  4.     """Returns the time elapsed since a given datetime.
  5.  
  6.     Given the 'start_datetime' returns a string with the elapsed time between
  7.     now and start_datetime.
  8.  
  9.     If elapsed time > 24 then string format is:
  10.         %d days, %H:%M:%S
  11.     else:
  12.         %H:%M:%S
  13.  
  14.     :param start_datetime: datetime
  15.     :return: str
  16.     """
  17.     delta = datetime.now() - start_datetime
  18.     days = delta.days
  19.     hours, remainder = divmod(delta.seconds, 3600)
  20.     mins, secs = divmod(remainder, 60)
  21.     kwargs = dict(days=days, hours=hours, mins=mins, secs=secs)
  22.     if days:
  23.         return "{days} days, {hours:02d}:{mins:02d}:{secs:02d}".format(**kwargs)
  24.     return "{hours:02d}:{mins:02d}:{secs:02d}".format(**kwargs)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top