datetime.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #include <xlnt/utils/datetime.hpp>
  27. #include <xlnt/utils/time.hpp>
  28. namespace {
  29. std::string fill(const std::string &string, std::size_t length = 2)
  30. {
  31. if (string.size() >= length)
  32. {
  33. return string;
  34. }
  35. return std::string(length - string.size(), '0') + string;
  36. }
  37. } // namespace
  38. namespace xlnt {
  39. datetime datetime::from_number(double raw_time, calendar base_date)
  40. {
  41. auto date_part = date::from_number(static_cast<int>(raw_time), base_date);
  42. auto time_part = time::from_number(raw_time);
  43. return datetime(date_part.year, date_part.month, date_part.day, time_part.hour, time_part.minute, time_part.second,
  44. time_part.microsecond);
  45. }
  46. bool datetime::operator==(const datetime &comparand) const
  47. {
  48. return year == comparand.year
  49. && month == comparand.month
  50. && day == comparand.day
  51. && hour == comparand.hour
  52. && minute == comparand.minute
  53. && second == comparand.second
  54. && microsecond == comparand.microsecond;
  55. }
  56. double datetime::to_number(calendar base_date) const
  57. {
  58. return date(year, month, day).to_number(base_date)
  59. + time(hour, minute, second, microsecond).to_number();
  60. }
  61. std::string datetime::to_string() const
  62. {
  63. return std::to_string(year) + "/" + std::to_string(month) + "/" + std::to_string(day) + " " + std::to_string(hour)
  64. + ":" + std::to_string(minute) + ":" + std::to_string(second) + ":" + std::to_string(microsecond);
  65. }
  66. datetime datetime::now()
  67. {
  68. return datetime(date::today(), time::now());
  69. }
  70. datetime datetime::today()
  71. {
  72. return datetime(date::today(), time(0, 0, 0, 0));
  73. }
  74. datetime::datetime(int year_, int month_, int day_, int hour_, int minute_, int second_, int microsecond_)
  75. : year(year_), month(month_), day(day_), hour(hour_), minute(minute_), second(second_), microsecond(microsecond_)
  76. {
  77. }
  78. datetime::datetime(const date &d, const time &t)
  79. : year(d.year),
  80. month(d.month),
  81. day(d.day),
  82. hour(t.hour),
  83. minute(t.minute),
  84. second(t.second),
  85. microsecond(t.microsecond)
  86. {
  87. }
  88. int datetime::weekday() const
  89. {
  90. return date(year, month, day).weekday();
  91. }
  92. datetime datetime::from_iso_string(const std::string &string)
  93. {
  94. xlnt::datetime result(1900, 1, 1);
  95. auto separator_index = string.find('-');
  96. result.year = std::stoi(string.substr(0, separator_index));
  97. result.month = std::stoi(string.substr(separator_index + 1, string.find('-', separator_index + 1)));
  98. separator_index = string.find('-', separator_index + 1);
  99. result.day = std::stoi(string.substr(separator_index + 1, string.find('T', separator_index + 1)));
  100. separator_index = string.find('T', separator_index + 1);
  101. result.hour = std::stoi(string.substr(separator_index + 1, string.find(':', separator_index + 1)));
  102. separator_index = string.find(':', separator_index + 1);
  103. result.minute = std::stoi(string.substr(separator_index + 1, string.find(':', separator_index + 1)));
  104. separator_index = string.find(':', separator_index + 1);
  105. result.second = std::stoi(string.substr(separator_index + 1, string.find('Z', separator_index + 1)));
  106. return result;
  107. }
  108. std::string datetime::to_iso_string() const
  109. {
  110. return std::to_string(year) + "-" + fill(std::to_string(month)) + "-" + fill(std::to_string(day)) + "T"
  111. + fill(std::to_string(hour)) + ":" + fill(std::to_string(minute)) + ":" + fill(std::to_string(second)) + "Z";
  112. }
  113. } // namespace xlnt