2018-01-22 22:38:48 +08:00
|
|
|
// Copyright (c) 2014-2018 Thomas Fussell
|
2015-12-25 04:51:11 +08:00
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE
|
|
|
|
//
|
|
|
|
// @license: http://www.opensource.org/licenses/mit-license.php
|
|
|
|
// @author: see AUTHORS file
|
2015-11-20 11:54:54 +08:00
|
|
|
#include <cmath>
|
|
|
|
#include <ctime>
|
|
|
|
|
|
|
|
#include <xlnt/utils/date.hpp>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
std::tm safe_localtime(std::time_t raw_time)
|
|
|
|
{
|
|
|
|
#ifdef _MSC_VER
|
2016-12-24 23:04:57 +08:00
|
|
|
std::tm result;
|
|
|
|
localtime_s(&result, &raw_time);
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
return result;
|
2015-11-20 11:54:54 +08:00
|
|
|
#else
|
2016-12-24 23:04:57 +08:00
|
|
|
return *localtime(&raw_time);
|
2015-11-20 11:54:54 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace xlnt {
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
date::date(int year_, int month_, int day_)
|
|
|
|
: year(year_), month(month_), day(day_)
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-11-20 11:54:54 +08:00
|
|
|
date date::from_number(int days_since_base_year, calendar base_date)
|
|
|
|
{
|
|
|
|
date result(0, 0, 0);
|
|
|
|
|
|
|
|
if (base_date == calendar::mac_1904)
|
|
|
|
{
|
|
|
|
days_since_base_year += 1462;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (days_since_base_year == 60)
|
|
|
|
{
|
|
|
|
result.day = 29;
|
|
|
|
result.month = 2;
|
|
|
|
result.year = 1900;
|
2016-07-17 07:40:20 +08:00
|
|
|
|
|
|
|
return result;
|
2015-11-20 11:54:54 +08:00
|
|
|
}
|
|
|
|
else if (days_since_base_year < 60)
|
|
|
|
{
|
|
|
|
days_since_base_year++;
|
|
|
|
}
|
|
|
|
|
|
|
|
int l = days_since_base_year + 68569 + 2415019;
|
|
|
|
int n = int((4 * l) / 146097);
|
|
|
|
l = l - int((146097 * n + 3) / 4);
|
|
|
|
int i = int((4000 * (l + 1)) / 1461001);
|
|
|
|
l = l - int((1461 * i) / 4) + 31;
|
|
|
|
int j = int((80 * l) / 2447);
|
|
|
|
result.day = l - int((2447 * j) / 80);
|
|
|
|
l = int(j / 11);
|
|
|
|
result.month = j + 2 - (12 * l);
|
|
|
|
result.year = 100 * (n - 49) + i + l;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool date::operator==(const date &comparand) const
|
|
|
|
{
|
|
|
|
return year == comparand.year && month == comparand.month && day == comparand.day;
|
|
|
|
}
|
|
|
|
|
2017-04-14 07:24:20 +08:00
|
|
|
bool date::operator!=(const date &comparand) const
|
|
|
|
{
|
|
|
|
return !(*this == comparand);
|
|
|
|
}
|
|
|
|
|
2015-11-20 11:54:54 +08:00
|
|
|
int date::to_number(calendar base_date) const
|
|
|
|
{
|
|
|
|
if (day == 29 && month == 2 && year == 1900)
|
|
|
|
{
|
|
|
|
return 60;
|
|
|
|
}
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
int days_since_1900 = int((1461 * (year + 4800 + int((month - 14) / 12))) / 4)
|
|
|
|
+ int((367 * (month - 2 - 12 * ((month - 14) / 12))) / 12)
|
|
|
|
- int((3 * (int((year + 4900 + int((month - 14) / 12)) / 100))) / 4) + day - 2415019 - 32075;
|
2015-11-20 11:54:54 +08:00
|
|
|
|
|
|
|
if (days_since_1900 <= 60)
|
|
|
|
{
|
|
|
|
days_since_1900--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (base_date == calendar::mac_1904)
|
|
|
|
{
|
|
|
|
return days_since_1900 - 1462;
|
|
|
|
}
|
|
|
|
|
|
|
|
return days_since_1900;
|
|
|
|
}
|
|
|
|
|
|
|
|
date date::today()
|
|
|
|
{
|
2017-09-04 22:10:51 +08:00
|
|
|
std::tm now = safe_localtime(std::time(nullptr));
|
2015-11-20 11:54:54 +08:00
|
|
|
return date(1900 + now.tm_year, now.tm_mon + 1, now.tm_mday);
|
|
|
|
}
|
|
|
|
|
2016-07-21 07:04:44 +08:00
|
|
|
int date::weekday() const
|
|
|
|
{
|
2018-10-29 02:46:09 +08:00
|
|
|
std::tm tm {0, 0, 0, day, month - 1, year - 1900};
|
|
|
|
std::time_t time = std::mktime(&tm);
|
2016-07-21 07:04:44 +08:00
|
|
|
|
2018-10-29 02:46:09 +08:00
|
|
|
return safe_localtime(time).tm_wday;
|
2016-07-21 07:04:44 +08:00
|
|
|
}
|
|
|
|
|
2015-11-20 11:54:54 +08:00
|
|
|
} // namespace xlnt
|