2020-02-09 01:12:59 +08:00
|
|
|
// Copyright (c) 2014-2020 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/time.hpp>
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
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);
|
2016-08-16 12:23:49 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
return result;
|
2016-08-16 12:23:49 +08:00
|
|
|
#else
|
2016-12-24 23:04:57 +08:00
|
|
|
return *localtime(&raw_time);
|
2016-08-16 12:23:49 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2015-11-20 11:54:54 +08:00
|
|
|
namespace xlnt {
|
|
|
|
|
2017-09-09 09:05:34 +08:00
|
|
|
time time::from_number(double raw_time)
|
2015-11-20 11:54:54 +08:00
|
|
|
{
|
|
|
|
time result;
|
|
|
|
|
|
|
|
double integer_part;
|
2016-11-10 08:52:18 +08:00
|
|
|
double fractional_part = std::modf(static_cast<double>(raw_time), &integer_part);
|
2015-11-20 11:54:54 +08:00
|
|
|
|
|
|
|
fractional_part *= 24;
|
2016-11-10 08:52:18 +08:00
|
|
|
result.hour = static_cast<int>(fractional_part);
|
2015-11-20 11:54:54 +08:00
|
|
|
fractional_part = 60 * (fractional_part - result.hour);
|
2016-11-10 08:52:18 +08:00
|
|
|
result.minute = static_cast<int>(fractional_part);
|
2015-11-20 11:54:54 +08:00
|
|
|
fractional_part = 60 * (fractional_part - result.minute);
|
2016-11-10 08:52:18 +08:00
|
|
|
result.second = static_cast<int>(fractional_part);
|
2015-11-20 11:54:54 +08:00
|
|
|
fractional_part = 1000000 * (fractional_part - result.second);
|
2016-11-10 08:52:18 +08:00
|
|
|
result.microsecond = static_cast<int>(fractional_part);
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2015-11-20 11:54:54 +08:00
|
|
|
if (result.microsecond == 999999 && fractional_part - result.microsecond > 0.5)
|
|
|
|
{
|
|
|
|
result.microsecond = 0;
|
|
|
|
result.second += 1;
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2015-11-20 11:54:54 +08:00
|
|
|
if (result.second == 60)
|
|
|
|
{
|
|
|
|
result.second = 0;
|
|
|
|
result.minute += 1;
|
2016-12-24 23:04:57 +08:00
|
|
|
|
|
|
|
// TODO: too much nesting
|
2015-11-20 11:54:54 +08:00
|
|
|
if (result.minute == 60)
|
|
|
|
{
|
|
|
|
result.minute = 0;
|
|
|
|
result.hour += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2015-11-20 11:54:54 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-12-25 04:51:11 +08:00
|
|
|
time::time(int hour_, int minute_, int second_, int microsecond_)
|
|
|
|
: hour(hour_), minute(minute_), second(second_), microsecond(microsecond_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-11-20 11:54:54 +08:00
|
|
|
bool time::operator==(const time &comparand) const
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
return hour == comparand.hour && minute == comparand.minute && second == comparand.second
|
|
|
|
&& microsecond == comparand.microsecond;
|
2015-11-20 11:54:54 +08:00
|
|
|
}
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
time::time(const std::string &time_string)
|
|
|
|
: hour(0), minute(0), second(0), microsecond(0)
|
2015-11-20 11:54:54 +08:00
|
|
|
{
|
|
|
|
std::string remaining = time_string;
|
|
|
|
auto colon_index = remaining.find(':');
|
|
|
|
hour = std::stoi(remaining.substr(0, colon_index));
|
|
|
|
remaining = remaining.substr(colon_index + 1);
|
|
|
|
colon_index = remaining.find(':');
|
|
|
|
minute = std::stoi(remaining.substr(0, colon_index));
|
|
|
|
colon_index = remaining.find(':');
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2015-11-20 11:54:54 +08:00
|
|
|
if (colon_index != std::string::npos)
|
|
|
|
{
|
|
|
|
remaining = remaining.substr(colon_index + 1);
|
|
|
|
second = std::stoi(remaining);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-09 09:05:34 +08:00
|
|
|
double time::to_number() const
|
2015-11-20 11:54:54 +08:00
|
|
|
{
|
|
|
|
std::uint64_t microseconds = static_cast<std::uint64_t>(microsecond);
|
|
|
|
microseconds += static_cast<std::uint64_t>(second * 1e6);
|
|
|
|
microseconds += static_cast<std::uint64_t>(minute * 1e6 * 60);
|
2016-12-24 23:04:57 +08:00
|
|
|
auto microseconds_per_hour = static_cast<std::uint64_t>(1e6) * 60 * 60;
|
2015-11-20 11:54:54 +08:00
|
|
|
microseconds += static_cast<std::uint64_t>(hour) * microseconds_per_hour;
|
2021-01-04 01:20:46 +08:00
|
|
|
auto number = static_cast<double>(microseconds) / (24.0 * static_cast<double>(microseconds_per_hour));
|
|
|
|
number = std::floor(number * 100e9 + 0.5) / 100e9;
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2015-11-20 11:54:54 +08:00
|
|
|
return number;
|
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
time time::now()
|
|
|
|
{
|
2017-09-04 22:10:51 +08:00
|
|
|
std::tm now = safe_localtime(std::time(nullptr));
|
2016-12-24 23:04:57 +08:00
|
|
|
return time(now.tm_hour, now.tm_min, now.tm_sec);
|
2016-08-16 12:23:49 +08:00
|
|
|
}
|
|
|
|
|
2015-11-20 11:54:54 +08:00
|
|
|
} // namespace xlnt
|