MockClock.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 or (at your option)
  7. * version 3 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "MockClock.h"
  18. MockClock::MockClock(int year, int month, int day, int hour, int min, int second)
  19. : Clock()
  20. , m_utcCurrent(datetimeUtc(year, month, day, hour, min, second))
  21. {
  22. }
  23. MockClock::MockClock(QDateTime utcBase)
  24. : Clock()
  25. , m_utcCurrent(utcBase)
  26. {
  27. }
  28. const QDateTime& MockClock::advanceSecond(int seconds)
  29. {
  30. m_utcCurrent = m_utcCurrent.addSecs(seconds);
  31. return m_utcCurrent;
  32. }
  33. const QDateTime& MockClock::advanceMinute(int minutes)
  34. {
  35. m_utcCurrent = m_utcCurrent.addSecs(minutes * 60);
  36. return m_utcCurrent;
  37. }
  38. const QDateTime& MockClock::advanceHour(int hours)
  39. {
  40. m_utcCurrent = m_utcCurrent.addSecs(hours * 60 * 60);
  41. return m_utcCurrent;
  42. }
  43. const QDateTime& MockClock::advanceDay(int days)
  44. {
  45. m_utcCurrent = m_utcCurrent.addDays(days);
  46. return m_utcCurrent;
  47. }
  48. const QDateTime& MockClock::advanceMonth(int months)
  49. {
  50. m_utcCurrent = m_utcCurrent.addMonths(months);
  51. return m_utcCurrent;
  52. }
  53. const QDateTime& MockClock::advanceYear(int years)
  54. {
  55. m_utcCurrent = m_utcCurrent.addYears(years);
  56. return m_utcCurrent;
  57. }
  58. void MockClock::setup(Clock* clock)
  59. {
  60. Clock::setInstance(clock);
  61. }
  62. void MockClock::teardown()
  63. {
  64. Clock::resetInstance();
  65. }
  66. QDateTime MockClock::currentDateTimeUtcImpl() const
  67. {
  68. return m_utcCurrent;
  69. }
  70. QDateTime MockClock::currentDateTimeImpl() const
  71. {
  72. return m_utcCurrent.toLocalTime();
  73. }