UptimeMeter.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. htop - UptimeMeter.c
  3. (C) 2004-2011 Hisham H. Muhammad
  4. Released under the GNU GPLv2+, see the COPYING file
  5. in the source distribution for its full text.
  6. */
  7. #include "config.h" // IWYU pragma: keep
  8. #include "UptimeMeter.h"
  9. #include "CRT.h"
  10. #include "Object.h"
  11. #include "Platform.h"
  12. #include "XUtils.h"
  13. static const int UptimeMeter_attributes[] = {
  14. UPTIME
  15. };
  16. static void UptimeMeter_updateValues(Meter* this) {
  17. int totalseconds = Platform_getUptime();
  18. if (totalseconds <= 0) {
  19. xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "(unknown)");
  20. return;
  21. }
  22. int seconds = totalseconds % 60;
  23. int minutes = (totalseconds / 60) % 60;
  24. int hours = (totalseconds / 3600) % 24;
  25. int days = (totalseconds / 86400);
  26. char daysbuf[32];
  27. if (days > 100) {
  28. xSnprintf(daysbuf, sizeof(daysbuf), "%d days(!), ", days);
  29. } else if (days > 1) {
  30. xSnprintf(daysbuf, sizeof(daysbuf), "%d days, ", days);
  31. } else if (days == 1) {
  32. xSnprintf(daysbuf, sizeof(daysbuf), "1 day, ");
  33. } else {
  34. daysbuf[0] = '\0';
  35. }
  36. xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%s%02d:%02d:%02d", daysbuf, hours, minutes, seconds);
  37. }
  38. const MeterClass UptimeMeter_class = {
  39. .super = {
  40. .extends = Class(Meter),
  41. .delete = Meter_delete
  42. },
  43. .updateValues = UptimeMeter_updateValues,
  44. .defaultMode = TEXT_METERMODE,
  45. .supportedModes = (1 << TEXT_METERMODE) | (1 << LED_METERMODE),
  46. .maxItems = 0,
  47. .total = 0.0,
  48. .attributes = UptimeMeter_attributes,
  49. .name = "Uptime",
  50. .uiName = "Uptime",
  51. .caption = "Uptime: "
  52. };