power_utils.cc 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/public/cpp/power_utils.h"
  5. #include "base/numerics/safe_conversions.h"
  6. #include "base/time/time.h"
  7. #include "chromeos/dbus/power_manager/power_supply_properties.pb.h"
  8. namespace ash {
  9. namespace power_utils {
  10. bool ShouldDisplayBatteryTime(const base::TimeDelta& time) {
  11. // Put limits on the maximum and minimum battery time-to-full or time-to-empty
  12. // that should be displayed in the UI. If the current is close to zero,
  13. // battery time estimates can get very large; avoid displaying these large
  14. // numbers.
  15. return time >= base::Minutes(1) && time <= base::Days(1);
  16. }
  17. int GetRoundedBatteryPercent(double battery_percent) {
  18. // Minimum battery percentage rendered in UI.
  19. constexpr int kMinBatteryPercent = 1;
  20. return std::max(kMinBatteryPercent, base::ClampRound(battery_percent));
  21. }
  22. void SplitTimeIntoHoursAndMinutes(const base::TimeDelta& time,
  23. int* hours,
  24. int* minutes) {
  25. DCHECK(hours);
  26. DCHECK(minutes);
  27. *minutes = base::ClampRound(time / base::Minutes(1));
  28. *hours = *minutes / 60;
  29. *minutes %= 60;
  30. }
  31. } // namespace power_utils
  32. } // namespace ash