xlnt/source/utils/datetime.cpp

60 lines
1.6 KiB
C++
Raw Normal View History

2014-05-31 06:42:25 +08:00
#include <cmath>
#include <ctime>
#include <xlnt/utils/datetime.hpp>
#include <xlnt/utils/date.hpp>
#include <xlnt/utils/time.hpp>
2014-05-31 06:42:25 +08:00
2015-11-03 06:25:10 +08:00
namespace {
std::tm safe_localtime(std::time_t raw_time)
{
#ifdef _MSC_VER
std::tm result;
localtime_s(&result, &raw_time);
return result;
#else
return *localtime(&raw_time);
#endif
}
} // 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
{
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 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
}
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
}
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-05-31 06:42:25 +08:00
datetime datetime::now()
{
2015-11-03 06:25:10 +08:00
std::tm now = safe_localtime(std::time(0));
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-05-31 06:42:25 +08:00
} // namespace xlnt