add processing dates before 1900

This commit is contained in:
RIMINMIR
2023-05-15 20:02:37 +06:00
parent 50c8f69956
commit be77e4a00a
3 changed files with 35 additions and 11 deletions

View File

@ -131,8 +131,9 @@ void CellFormatController::ProcessCellType(OOX::Spreadsheet::CCell *pCell, const
}
DateReader dateReader = {};
auto digitalDate = dateReader.GetDigitalDate(value);
if(digitalDate != 0)
_INT32 digitalDate = 0;
auto validDate = dateReader.GetDigitalDate(value, digitalDate);
if(validDate)
{
pCell->m_oValue.Init();
pCell->m_oValue->m_sText = std::to_wstring(digitalDate);

View File

@ -50,7 +50,7 @@ std::vector<std::wstring> DateFormats = {
L"%m-%d-%y", L"%d-%m-%Y", L"%d-%m-%y"
};
_UINT32 DateReader::GetDigitalDate(const std::wstring &date)
bool DateReader::GetDigitalDate(const std::wstring &date, _INT32 &result)
{
// Перебор форматов даты, пока не найдется подходящий
for (const auto& format : DateFormats) {
@ -64,13 +64,25 @@ _UINT32 DateReader::GetDigitalDate(const std::wstring &date)
continue;
}
// Преобразование даты в формат excel
auto tp = std::chrono::system_clock::from_time_t(mktime(&time));
auto excelTime = (tp.time_since_epoch().count() / 10000000) + 2209161600;
double tempTime = excelTime / 86400.0;
return round(excelTime / 86400.0);
//определяем стандартная ли дата
if(time.tm_year > 0)
{
result = getStandartDate(time);
return true;
}
return false;
}
// Если не найден подходящий формат даты, возвращаем 0
return 0;
// Если не найден подходящий формат даты, возвращаем false
return false;
}
_INT32 DateReader::getStandartDate(tm &date)
{
// Преобразование даты в формат excel
auto tp = std::chrono::system_clock::from_time_t(mktime(&date));
auto excelTime = (tp.time_since_epoch().count() / 10000000) + 2209161600;
_INT32 tempTime = round(excelTime / 86400.0);
return tempTime;
}

View File

@ -35,10 +35,21 @@
#include "../../../../Base/Base.h"
#include <string>
#include <chrono>
class DateReader
{
public:
_UINT32 GetDigitalDate(const std::wstring &date);
/// @brief получение даты в виде числа в формате excel
/// @param date дата в строковом формате
/// @param result результат в формате excel
/// @param return true в случае успешной конвертации, иначе false
bool GetDigitalDate(const std::wstring &date, _INT32 &result);
private:
/// @brief получение даты в виде числа в формате excel из дат позднее 1900 года
/// @param datetime структура с датой
/// @return дата в формате excel
_INT32 getStandartDate(tm &date);
};