You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.0 KiB

4 years ago
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Glances.
  4. #
  5. # Copyright (C) 2018 Nicolargo <nicolas@nicolargo.com>
  6. #
  7. # Glances is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU Lesser General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Glances is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. """The timer manager."""
  20. from time import time
  21. from datetime import datetime
  22. # Global list to manage the elapsed time
  23. last_update_times = {}
  24. def getTimeSinceLastUpdate(IOType):
  25. """Return the elapsed time since last update."""
  26. global last_update_times
  27. # assert(IOType in ['net', 'disk', 'process_disk'])
  28. current_time = time()
  29. last_time = last_update_times.get(IOType)
  30. if not last_time:
  31. time_since_update = 1
  32. else:
  33. time_since_update = current_time - last_time
  34. last_update_times[IOType] = current_time
  35. return time_since_update
  36. class Timer(object):
  37. """The timer class. A simple chronometer."""
  38. def __init__(self, duration):
  39. self.duration = duration
  40. self.start()
  41. def start(self):
  42. self.target = time() + self.duration
  43. def reset(self):
  44. self.start()
  45. def get(self):
  46. return self.duration - (self.target - time())
  47. def set(self, duration):
  48. self.duration = duration
  49. def finished(self):
  50. return time() > self.target
  51. class Counter(object):
  52. """The counter class."""
  53. def __init__(self):
  54. self.start()
  55. def start(self):
  56. self.target = datetime.now()
  57. def reset(self):
  58. self.start()
  59. def get(self):
  60. return (datetime.now() - self.target).total_seconds()