xlnt/source/common/datetime.cpp

189 lines
5.6 KiB
C++
Raw Normal View History

2014-05-31 06:42:25 +08:00
#include <cmath>
#include <ctime>
2014-08-14 06:56:34 +08:00
#include <xlnt/common/datetime.hpp>
2014-05-31 06:42:25 +08:00
namespace xlnt {
2014-06-14 03:05:24 +08:00
time time::from_number(long double raw_time)
2014-05-31 06:42:25 +08:00
{
time result;
2014-05-31 06:42:25 +08:00
double integer_part;
2014-06-11 05:12:15 +08:00
double fractional_part = std::modf((double)raw_time, &integer_part);
2014-05-31 06:42:25 +08:00
fractional_part *= 24;
result.hour = (int)fractional_part;
fractional_part = 60 * (fractional_part - result.hour);
result.minute = (int)fractional_part;
fractional_part = 60 * (fractional_part - result.minute);
result.second = (int)fractional_part;
fractional_part = 1000000 * (fractional_part - result.second);
result.microsecond = (int)fractional_part;
if (result.microsecond == 999999 && fractional_part - result.microsecond > 0.5)
{
result.microsecond = 0;
result.second += 1;
if (result.second == 60)
{
result.second = 0;
result.minute += 1;
if (result.minute == 60)
{
result.minute = 0;
result.hour += 1;
}
}
}
return result;
2014-06-12 04:41:34 +08:00
}
2014-07-20 04:59:05 +08:00
date date::from_number(int days_since_base_year, calendar base_date)
2014-06-12 04:41:34 +08:00
{
2014-06-14 03:05:24 +08:00
date result(0, 0, 0);
if (base_date == calendar::mac_1904)
2014-06-14 03:05:24 +08:00
{
days_since_base_year += 1462;
}
if (days_since_base_year == 60)
2014-06-14 03:05:24 +08:00
{
result.day = 29;
result.month = 2;
result.year = 1900;
}
else if (days_since_base_year < 60)
2014-06-14 03:05:24 +08:00
{
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;
2014-06-12 04:41:34 +08:00
}
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
{
2014-07-20 04:59:05 +08:00
auto date_part = date::from_number((int)raw_time, base_date);
2014-06-14 03:05:24 +08:00
auto time_part = time::from_number(raw_time);
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 date::operator==(const date &comparand) const
{
return year == comparand.year && month == comparand.month && day == comparand.day;
2014-06-12 04:41:34 +08:00
}
bool time::operator==(const time &comparand) const
{
return hour == comparand.hour && minute == comparand.minute && second == comparand.second &&
microsecond == comparand.microsecond;
2014-06-12 04:41:34 +08:00
}
bool datetime::operator==(const datetime &comparand) const
{
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
}
2014-06-07 23:49:19 +08:00
time::time(const std::string &time_string) : hour(0), minute(0), second(0), microsecond(0)
{
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(':');
if (colon_index != std::string::npos)
2014-06-07 23:49:19 +08:00
{
remaining = remaining.substr(colon_index + 1);
second = std::stoi(remaining);
2014-06-07 23:49:19 +08:00
}
}
long double time::to_number() const
2014-06-07 23:49:19 +08:00
{
2015-11-03 03:22:13 +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);
auto microseconds_per_hour = static_cast<std::uint64_t>(1e6) * 60 * 60;
microseconds += static_cast<std::uint64_t>(hour * microseconds_per_hour);
auto number = microseconds / (24.0L * microseconds_per_hour);
auto hundred_billion = static_cast<std::uint64_t>(1e9) * 100;
number = std::floor(number * hundred_billion + 0.5L) / hundred_billion;
2014-06-07 23:49:19 +08:00
return number;
}
2014-07-20 04:59:05 +08:00
int date::to_number(calendar base_date) const
2014-06-12 04:41:34 +08:00
{
if (day == 29 && month == 2 && year == 1900)
2014-06-14 03:05:24 +08:00
{
return 60;
}
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;
2014-06-14 03:05:24 +08:00
if (days_since_1900 <= 60)
2014-06-14 03:05:24 +08:00
{
days_since_1900--;
}
if (base_date == calendar::mac_1904)
2014-06-14 03:05:24 +08:00
{
2014-07-17 07:53:45 +08:00
return days_since_1900 - 1462;
2014-06-14 03:05:24 +08:00
}
return days_since_1900;
2014-06-12 04:41:34 +08:00
}
long double datetime::to_number(calendar base_date) const
2014-06-12 04:41:34 +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
}
2015-10-15 06:05:13 +08:00
std::string datetime::to_string(xlnt::calendar /*base_date*/) const
{
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);
}
2014-06-11 06:36:31 +08:00
date date::today()
{
std::time_t raw_time = std::time(0);
std::tm now = *std::localtime(&raw_time);
2014-06-14 03:05:24 +08:00
return date(1900 + now.tm_year, now.tm_mon + 1, now.tm_mday);
2014-06-11 06:36:31 +08:00
}
2014-05-31 06:42:25 +08:00
datetime datetime::now()
{
std::time_t raw_time = std::time(0);
std::tm now = *std::localtime(&raw_time);
2014-06-14 03:05:24 +08:00
return datetime(1900 + now.tm_year, now.tm_mon + 1, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec);
2014-05-31 06:42:25 +08:00
}
2014-07-20 04:59:05 +08:00
double timedelta::to_number() const
{
return days + hours / 24.0;
}
2015-10-19 03:30:46 +08:00
timedelta timedelta::from_number(long double number)
{
return timedelta(static_cast<long long int>(number), 0);
}
2014-05-31 06:42:25 +08:00
} // namespace xlnt