xlnt/source/datetime.cpp

183 lines
4.6 KiB
C++
Raw Normal View History

2014-05-31 06:42:25 +08:00
#include <cmath>
#include <ctime>
2014-06-06 04:19:31 +08:00
#include "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-06-14 03:05:24 +08:00
date date::from_number(int days_since_base_year, int base_year)
2014-06-12 04:41:34 +08:00
{
2014-06-14 03:05:24 +08:00
date result(0, 0, 0);
if(base_year == 1904)
{
days_since_base_year += 1462;
}
if(days_since_base_year == 60)
{
result.day = 29;
result.month = 2;
result.year = 1900;
}
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;
2014-06-12 04:41:34 +08:00
}
datetime datetime::from_number(long double raw_time, int base_year)
2014-06-12 04:41:34 +08:00
{
2014-06-14 03:05:24 +08:00
auto date_part = date::from_number((int)raw_time, base_year);
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;
}
bool time::operator==(const time &comparand) const
{
return hour == comparand.hour
&& minute == comparand.minute
&& second == comparand.second
&& microsecond == comparand.microsecond;
}
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)
{
remaining = remaining.substr(colon_index + 1);
second = std::stoi(remaining);
}
}
2014-06-14 03:05:24 +08:00
double time::to_number() const
2014-06-07 23:49:19 +08:00
{
double number = microsecond;
number /= 1000000;
number += second;
number /= 60;
number += minute;
number /= 60;
number += hour;
number /= 24;
return number;
}
2014-06-14 03:05:24 +08:00
int date::to_number(int base_year) const
2014-06-12 04:41:34 +08:00
{
2014-06-14 03:05:24 +08:00
if(day == 29 && month == 2 && year == 1900)
{
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;
if(days_since_1900 <= 60)
{
days_since_1900--;
}
if(base_year == 1904)
{
return days_since_1900 - 1461;
}
return days_since_1900;
2014-06-12 04:41:34 +08:00
}
double datetime::to_number(int base_year) const
2014-06-12 04:41:34 +08:00
{
2014-06-14 03:05:24 +08:00
return date(year, month, day).to_number(base_year)
+ time(hour, minute, second, microsecond).to_number();
2014-06-12 04:41:34 +08:00
}
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
}
} // namespace xlnt