date.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/date.hpp>
  26. namespace {
  27. std::tm safe_localtime(std::time_t raw_time)
  28. {
  29. #ifdef _MSC_VER
  30. std::tm result;
  31. localtime_s(&result, &raw_time);
  32. return result;
  33. #else
  34. return *localtime(&raw_time);
  35. #endif
  36. }
  37. } // namespace
  38. namespace xlnt {
  39. date::date(int year_, int month_, int day_)
  40. : year(year_), month(month_), day(day_)
  41. {
  42. }
  43. date date::from_number(int days_since_base_year, calendar base_date)
  44. {
  45. date result(0, 0, 0);
  46. if (base_date == calendar::mac_1904)
  47. {
  48. days_since_base_year += 1462;
  49. }
  50. if (days_since_base_year == 60)
  51. {
  52. result.day = 29;
  53. result.month = 2;
  54. result.year = 1900;
  55. return result;
  56. }
  57. else if (days_since_base_year < 60)
  58. {
  59. days_since_base_year++;
  60. }
  61. int l = days_since_base_year + 68569 + 2415019;
  62. int n = int((4 * l) / 146097);
  63. l = l - int((146097 * n + 3) / 4);
  64. int i = int((4000 * (l + 1)) / 1461001);
  65. l = l - int((1461 * i) / 4) + 31;
  66. int j = int((80 * l) / 2447);
  67. result.day = l - int((2447 * j) / 80);
  68. l = int(j / 11);
  69. result.month = j + 2 - (12 * l);
  70. result.year = 100 * (n - 49) + i + l;
  71. return result;
  72. }
  73. bool date::operator==(const date &comparand) const
  74. {
  75. return year == comparand.year && month == comparand.month && day == comparand.day;
  76. }
  77. bool date::operator!=(const date &comparand) const
  78. {
  79. return !(*this == comparand);
  80. }
  81. int date::to_number(calendar base_date) const
  82. {
  83. if (day == 29 && month == 2 && year == 1900)
  84. {
  85. return 60;
  86. }
  87. int days_since_1900 = int((1461 * (year + 4800 + int((month - 14) / 12))) / 4)
  88. + int((367 * (month - 2 - 12 * ((month - 14) / 12))) / 12)
  89. - int((3 * (int((year + 4900 + int((month - 14) / 12)) / 100))) / 4) + day - 2415019 - 32075;
  90. if (days_since_1900 <= 60)
  91. {
  92. days_since_1900--;
  93. }
  94. if (base_date == calendar::mac_1904)
  95. {
  96. return days_since_1900 - 1462;
  97. }
  98. return days_since_1900;
  99. }
  100. date date::today()
  101. {
  102. std::tm now = safe_localtime(std::time(nullptr));
  103. return date(1900 + now.tm_year, now.tm_mon + 1, now.tm_mday);
  104. }
  105. int date::weekday() const
  106. {
  107. std::tm tm = std::tm();
  108. tm.tm_mday = day;
  109. tm.tm_mon = month - 1;
  110. tm.tm_year = year - 1900;
  111. std::time_t time = std::mktime(&tm);
  112. return safe_localtime(time).tm_wday;
  113. }
  114. } // namespace xlnt