2015-12-25 06:10:02 +08:00
|
|
|
// Copyright (c) 2014-2016 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
|
2014-05-31 06:42:25 +08:00
|
|
|
#include <cmath>
|
|
|
|
#include <ctime>
|
|
|
|
|
2015-11-03 21:38:09 +08:00
|
|
|
#include <xlnt/utils/datetime.hpp>
|
2015-11-20 11:54:54 +08:00
|
|
|
#include <xlnt/utils/date.hpp>
|
|
|
|
#include <xlnt/utils/time.hpp>
|
2014-05-31 06:42:25 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
std::string fill(const std::string &string, std::size_t length = 2)
|
|
|
|
{
|
|
|
|
if (string.size() >= length)
|
|
|
|
{
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::string(length - string.size(), '0') + string;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-05-31 06:42:25 +08:00
|
|
|
namespace xlnt {
|
|
|
|
|
2014-07-20 04:59:05 +08:00
|
|
|
datetime datetime::from_number(long double raw_time, calendar base_date)
|
2014-06-12 04:41:34 +08:00
|
|
|
{
|
2016-11-10 08:52:18 +08:00
|
|
|
auto date_part = date::from_number(static_cast<int>(raw_time), base_date);
|
2014-06-14 03:05:24 +08:00
|
|
|
auto time_part = time::from_number(raw_time);
|
2015-11-03 21:38:09 +08:00
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
return datetime(date_part.year, date_part.month, date_part.day, time_part.hour, time_part.minute, time_part.second,
|
|
|
|
time_part.microsecond);
|
2014-06-12 04:41:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool datetime::operator==(const datetime &comparand) const
|
|
|
|
{
|
2015-11-01 22:43:01 +08:00
|
|
|
return year == comparand.year && month == comparand.month && day == comparand.day && hour == comparand.hour &&
|
|
|
|
minute == comparand.minute && second == comparand.second && microsecond == comparand.microsecond;
|
2014-05-31 06:42:25 +08:00
|
|
|
}
|
|
|
|
|
2015-10-07 00:31:49 +08:00
|
|
|
long double datetime::to_number(calendar base_date) const
|
2014-06-12 04:41:34 +08:00
|
|
|
{
|
2015-11-01 22:43:01 +08:00
|
|
|
return date(year, month, day).to_number(base_date) + time(hour, minute, second, microsecond).to_number();
|
2014-06-12 04:41:34 +08:00
|
|
|
}
|
|
|
|
|
2016-07-17 03:57:50 +08:00
|
|
|
std::string datetime::to_string() const
|
2015-10-14 02:19:46 +08:00
|
|
|
{
|
2015-11-11 07:58:54 +08:00
|
|
|
return std::to_string(year) + "/" + std::to_string(month) + "/" + std::to_string(day) + " " + std::to_string(hour) +
|
|
|
|
":" + std::to_string(minute) + ":" + std::to_string(second) + ":" + std::to_string(microsecond);
|
2015-10-14 02:19:46 +08:00
|
|
|
}
|
|
|
|
|
2014-05-31 06:42:25 +08:00
|
|
|
datetime datetime::now()
|
|
|
|
{
|
2016-08-16 12:23:49 +08:00
|
|
|
return datetime(date::today(), time::now());
|
2014-05-31 06:42:25 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-12-25 04:51:11 +08:00
|
|
|
datetime datetime::today()
|
|
|
|
{
|
2016-08-16 12:23:49 +08:00
|
|
|
return datetime(date::today(), time(0, 0, 0, 0));
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
datetime::datetime(int year_, int month_, int day_, int hour_, int minute_, int second_, int microsecond_)
|
|
|
|
: year(year_),
|
|
|
|
month(month_),
|
|
|
|
day(day_),
|
|
|
|
hour(hour_),
|
|
|
|
minute(minute_),
|
|
|
|
second(second_),
|
|
|
|
microsecond(microsecond_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
datetime::datetime(const date &d, const time &t)
|
|
|
|
: year(d.year),
|
|
|
|
month(d.month),
|
|
|
|
day(d.day),
|
|
|
|
hour(t.hour),
|
|
|
|
minute(t.minute),
|
|
|
|
second(t.second),
|
|
|
|
microsecond(t.microsecond)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-07-21 07:04:44 +08:00
|
|
|
int datetime::weekday() const
|
|
|
|
{
|
|
|
|
return date(year, month, day).weekday();
|
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
datetime datetime::from_iso_string(const std::string &string)
|
|
|
|
{
|
|
|
|
xlnt::datetime result(1900, 1, 1);
|
|
|
|
|
|
|
|
auto separator_index = string.find('-');
|
|
|
|
result.year = std::stoi(string.substr(0, separator_index));
|
|
|
|
result.month = std::stoi(string.substr(separator_index + 1, string.find('-', separator_index + 1)));
|
|
|
|
separator_index = string.find('-', separator_index + 1);
|
|
|
|
result.day = std::stoi(string.substr(separator_index + 1, string.find('T', separator_index + 1)));
|
|
|
|
separator_index = string.find('T', separator_index + 1);
|
|
|
|
result.hour = std::stoi(string.substr(separator_index + 1, string.find(':', separator_index + 1)));
|
|
|
|
separator_index = string.find(':', separator_index + 1);
|
|
|
|
result.minute = std::stoi(string.substr(separator_index + 1, string.find(':', separator_index + 1)));
|
|
|
|
separator_index = string.find(':', separator_index + 1);
|
|
|
|
result.second = std::stoi(string.substr(separator_index + 1, string.find('Z', separator_index + 1)));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string datetime::to_iso_string() const
|
|
|
|
{
|
|
|
|
return std::to_string(year) + "-"
|
|
|
|
+ fill(std::to_string(month)) + "-"
|
|
|
|
+ fill(std::to_string(day)) + "T"
|
|
|
|
+ fill(std::to_string(hour)) + ":"
|
|
|
|
+ fill(std::to_string(minute)) + ":"
|
|
|
|
+ fill(std::to_string(second)) + "Z";
|
|
|
|
}
|
|
|
|
|
2014-05-31 06:42:25 +08:00
|
|
|
} // namespace xlnt
|