timedelta.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (c) 2014-2021 Thomas Fussell
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE
  20. //
  21. // @license: http://www.opensource.org/licenses/mit-license.php
  22. // @author: see AUTHORS file
  23. #include <cmath>
  24. #include <ctime>
  25. #include <xlnt/utils/timedelta.hpp>
  26. namespace xlnt {
  27. timedelta::timedelta()
  28. : timedelta(0, 0, 0, 0, 0)
  29. {
  30. }
  31. timedelta::timedelta(int days_, int hours_, int minutes_, int seconds_, int microseconds_)
  32. : days(days_), hours(hours_), minutes(minutes_), seconds(seconds_), microseconds(microseconds_)
  33. {
  34. }
  35. double timedelta::to_number() const
  36. {
  37. std::uint64_t total_microseconds = static_cast<std::uint64_t>(microseconds);
  38. total_microseconds += static_cast<std::uint64_t>(seconds * 1e6);
  39. total_microseconds += static_cast<std::uint64_t>(minutes * 1e6 * 60);
  40. auto microseconds_per_hour = static_cast<std::uint64_t>(1e6) * 60 * 60;
  41. total_microseconds += static_cast<std::uint64_t>(hours) * microseconds_per_hour;
  42. auto number = static_cast<double>(total_microseconds) / (24.0 * static_cast<double>(microseconds_per_hour));
  43. number = std::floor(number * 100e9 + 0.5) / 100e9;
  44. number += days;
  45. return number;
  46. }
  47. timedelta timedelta::from_number(double raw_time)
  48. {
  49. timedelta result;
  50. result.days = static_cast<int>(raw_time);
  51. double fractional_part = raw_time - result.days;
  52. fractional_part *= 24;
  53. result.hours = static_cast<int>(fractional_part);
  54. fractional_part = 60 * (fractional_part - result.hours);
  55. result.minutes = static_cast<int>(fractional_part);
  56. fractional_part = 60 * (fractional_part - result.minutes);
  57. result.seconds = static_cast<int>(fractional_part);
  58. fractional_part = 1000000 * (fractional_part - result.seconds);
  59. result.microseconds = static_cast<int>(fractional_part);
  60. if (result.microseconds == 999999 && fractional_part - result.microseconds > 0.5)
  61. {
  62. result.microseconds = 0;
  63. result.seconds += 1;
  64. if (result.seconds == 60)
  65. {
  66. result.seconds = 0;
  67. result.minutes += 1;
  68. // TODO: too much nesting
  69. if (result.minutes == 60)
  70. {
  71. result.minutes = 0;
  72. result.hours += 1;
  73. if (result.hours == 24)
  74. {
  75. result.hours = 0;
  76. result.days += 1;
  77. }
  78. }
  79. }
  80. }
  81. return result;
  82. }
  83. } // namespace xlnt