#include "data/Data.h" #include "data/DateTimeData.h" #include #include #include #include "Utils.h" DateTimeData::DateTimeData() { std::time_t t = std::time(0); // get time now //std::tm* now = std::localtime(&t); std::tm* now = std::localtime(&t); m_year = (unsigned short)(now->tm_year + 1900); m_month = (unsigned char)(now->tm_mon + 1); m_day = (unsigned char)now->tm_mday; m_hour = (unsigned char)now->tm_hour; m_minute = (unsigned char)now->tm_min; m_second = (unsigned char)now->tm_sec; } DateTimeData::DateTimeData(unsigned short year, unsigned char month, unsigned char day, unsigned char hour, unsigned char minute, unsigned char second) : m_year(year) , m_month(month) , m_day(day) , m_hour(hour) , m_minute(minute) , m_second(second) { } Data* DateTimeData::clone() { Data* data = new DateTimeData(m_year, m_month, m_day, m_hour, m_minute, m_second); return data; } void DateTimeData::copy(Data* data) { DateTimeData *dt = dynamic_cast(data); m_year = dt->getYear(); m_month = dt->getMonth(); m_day = dt->getDay(); m_hour = dt->getHour(); m_minute = dt->getMinute(); m_second = dt->getSecond(); } void DateTimeData::copy(Data& data) { DateTimeData *dt = dynamic_cast(&data); m_year = dt->getYear(); m_month = dt->getMonth(); m_day = dt->getDay(); m_hour = dt->getHour(); m_minute = dt->getMinute(); m_second = dt->getSecond(); } std::string DateTimeData::toStr() { std::stringstream ss; ss << ((int)m_year) << "-" << std::setw(2) << std::setfill('0') << (int)m_month << "-" << (int)m_day << " " << (int)m_hour << ":" << (int)m_minute << ":" << (int)m_second; return ss.str(); } std::string DateTimeData::toByteStr() { std::string s; s.append(Utils::shortToByteStr(m_year)); s.append(Utils::charToByteStr(m_month)); s.append(Utils::charToByteStr(m_day)); s.append(Utils::charToByteStr(m_hour)); s.append(Utils::charToByteStr(m_minute)); s.append(Utils::charToByteStr(m_second)); return s; } std::string DateTimeData::toHexStr() { std::string s; s.append(Utils::strToHexStr(Utils::shortToByteStr(m_year))); s.append(Utils::charToHexStr(m_month)); s.append(Utils::charToHexStr(m_day)); s.append(Utils::charToHexStr(m_hour)); s.append(Utils::charToHexStr(m_minute)); s.append(Utils::charToHexStr(m_second)); return s; } bool DateTimeData::fromByteStr(std::string str) { if(str.length() < 7) { return false; } m_year = Utils::byteStrToShort(str.substr(0, 2)); m_month = str.at(2); m_day = str.at(3); m_hour = str.at(4); m_minute = str.at(5); m_second = str.at(6); return true; }