datetime_test_suite.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 <iostream>
  24. #include <helpers/test_suite.hpp>
  25. #include <xlnt/utils/date.hpp>
  26. #include <xlnt/utils/datetime.hpp>
  27. #include <xlnt/utils/time.hpp>
  28. class datetime_test_suite : public test_suite
  29. {
  30. public:
  31. datetime_test_suite()
  32. {
  33. register_test(test_from_string);
  34. register_test(test_to_string);
  35. register_test(test_carry);
  36. register_test(test_leap_year_bug);
  37. register_test(test_early_date);
  38. register_test(test_mac_calendar);
  39. register_test(test_operators);
  40. register_test(test_weekday);
  41. }
  42. void test_from_string()
  43. {
  44. xlnt::time t("10:35:45");
  45. xlnt_assert_equals(t.hour, 10);
  46. xlnt_assert_equals(t.minute, 35);
  47. xlnt_assert_equals(t.second, 45);
  48. }
  49. void test_to_string()
  50. {
  51. xlnt::datetime dt(2016, 7, 16, 9, 11, 32, 999999);
  52. xlnt_assert_equals(dt.to_string(), "2016/7/16 9:11:32:999999");
  53. }
  54. void test_carry()
  55. {
  56. // We want a time that rolls over to the next second, minute, and hour
  57. // Start off with a time 1 microsecond before the next hour
  58. xlnt::datetime dt(2016, 7, 9, 10, 59, 59, 999999);
  59. auto number = dt.to_number(xlnt::calendar::windows_1900);
  60. // Add 600 nanoseconds to the raw number which represents time as a fraction of a day
  61. // In other words, 6 tenths of a millionth of a sixtieth of a sixtieth of a twenty-fourth of a day
  62. number += (0.6 / 1000000) / 60 / 60 / 24;
  63. auto rollover = xlnt::datetime::from_number(number, xlnt::calendar::windows_1900);
  64. xlnt_assert_equals(rollover.hour, 11);
  65. xlnt_assert_equals(rollover.minute, 00);
  66. xlnt_assert_equals(rollover.second, 00);
  67. xlnt_assert_equals(rollover.microsecond, 00);
  68. }
  69. void test_leap_year_bug()
  70. {
  71. xlnt::datetime dt(1900, 2, 29, 0, 0, 0, 0);
  72. auto number = dt.to_number(xlnt::calendar::windows_1900);
  73. xlnt_assert_equals(static_cast<int>(number), 60);
  74. auto converted = xlnt::datetime::from_number(number, xlnt::calendar::windows_1900);
  75. xlnt_assert_equals(dt, converted);
  76. }
  77. void test_early_date()
  78. {
  79. xlnt::date d(1900, 1, 29);
  80. auto number = d.to_number(xlnt::calendar::windows_1900);
  81. xlnt_assert_equals(number, 29);
  82. auto converted = xlnt::date::from_number(number, xlnt::calendar::windows_1900);
  83. xlnt_assert_equals(d, converted);
  84. }
  85. void test_mac_calendar()
  86. {
  87. xlnt::date d(2016, 7, 16);
  88. auto number_1900 = d.to_number(xlnt::calendar::windows_1900);
  89. auto number_1904 = d.to_number(xlnt::calendar::mac_1904);
  90. xlnt_assert_equals(number_1900, 42567);
  91. xlnt_assert_equals(number_1904, 41105);
  92. auto converted_1900 = xlnt::date::from_number(number_1900, xlnt::calendar::windows_1900);
  93. auto converted_1904 = xlnt::date::from_number(number_1904, xlnt::calendar::mac_1904);
  94. xlnt_assert_equals(converted_1900, d);
  95. xlnt_assert_equals(converted_1904, d);
  96. }
  97. void test_operators()
  98. {
  99. xlnt::date d1(2016, 7, 16);
  100. xlnt::date d2(2016, 7, 16);
  101. xlnt::date d3(2016, 7, 15);
  102. xlnt_assert_equals(d1, d2);
  103. xlnt_assert_differs(d1, d3);
  104. }
  105. void test_weekday()
  106. {
  107. xlnt_assert_equals(xlnt::date(2000, 1, 1).weekday(), 6); // January 1st 2000 was a Saturday
  108. xlnt_assert_equals(xlnt::date(2016, 7, 15).weekday(), 5); // July 15th 2016 was a Friday
  109. xlnt_assert_equals(xlnt::date(2018, 10, 29).weekday(), 1); // October 29th 2018 was Monday
  110. xlnt_assert_equals(xlnt::date(1970, 1, 1).weekday(), 4); // January 1st 1970 was a Thursday
  111. }
  112. };
  113. static datetime_test_suite x;