2014-04-08 06:51:53 +00:00
|
|
|
|
#pragma once
|
2011-03-09 19:18:01 +00:00
|
|
|
|
|
2011-03-15 20:56:42 +00:00
|
|
|
|
#include <string.h>
|
2011-03-09 19:18:01 +00:00
|
|
|
|
#include <string>
|
2015-09-29 19:19:54 +00:00
|
|
|
|
#include <common/DateLUT.h>
|
2011-03-09 19:18:01 +00:00
|
|
|
|
|
|
|
|
|
#include <mysqlxx/Exception.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace mysqlxx
|
|
|
|
|
{
|
|
|
|
|
|
2011-03-18 20:26:54 +00:00
|
|
|
|
/** Хранит дату в broken-down виде.
|
|
|
|
|
* Может быть инициализирован из даты в текстовом виде '2011-01-01' и из time_t.
|
2011-08-03 14:24:28 +00:00
|
|
|
|
* Может быть инициализирован из даты в текстовом виде '20110101...(юзаются первые 8 символов)
|
2011-03-18 20:26:54 +00:00
|
|
|
|
* Неявно преобразуется в time_t.
|
|
|
|
|
* Сериализуется в ostream в текстовом виде.
|
|
|
|
|
* Внимание: преобразование в unix timestamp и обратно производится в текущей тайм-зоне!
|
|
|
|
|
* При переводе стрелок назад, возникает неоднозначность - преобразование производится в меньшее значение.
|
|
|
|
|
*
|
2011-03-31 15:38:32 +00:00
|
|
|
|
* packed - для memcmp (из-за того, что m_year - 2 байта, little endian, работает корректно только до 2047 года)
|
2011-03-18 20:26:54 +00:00
|
|
|
|
*/
|
2011-10-14 18:03:25 +00:00
|
|
|
|
class __attribute__ ((__packed__)) Date
|
2011-03-09 19:18:01 +00:00
|
|
|
|
{
|
|
|
|
|
private:
|
2011-10-14 18:03:25 +00:00
|
|
|
|
unsigned short m_year;
|
|
|
|
|
unsigned char m_month;
|
|
|
|
|
unsigned char m_day;
|
2011-03-09 19:18:01 +00:00
|
|
|
|
|
2011-03-10 20:31:02 +00:00
|
|
|
|
void init(time_t time)
|
2011-03-09 19:18:01 +00:00
|
|
|
|
{
|
2015-07-07 23:11:30 +00:00
|
|
|
|
const auto & date_lut = DateLUT::instance();
|
2015-06-26 15:11:31 +00:00
|
|
|
|
const auto & values = date_lut.getValues(time);
|
2011-03-09 19:18:01 +00:00
|
|
|
|
|
2011-10-14 18:03:25 +00:00
|
|
|
|
m_year = values.year;
|
|
|
|
|
m_month = values.month;
|
|
|
|
|
m_day = values.day_of_month;
|
2011-03-10 20:31:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void init(const char * s, size_t length)
|
|
|
|
|
{
|
2012-05-02 19:16:50 +00:00
|
|
|
|
if (length < 8)
|
2011-03-10 20:31:02 +00:00
|
|
|
|
throw Exception("Cannot parse Date: " + std::string(s, length));
|
2012-05-02 19:16:50 +00:00
|
|
|
|
|
2011-10-14 18:03:25 +00:00
|
|
|
|
m_year = (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] - '0');
|
2012-05-02 19:16:50 +00:00
|
|
|
|
|
|
|
|
|
if (s[4] == '-')
|
2011-08-03 14:24:28 +00:00
|
|
|
|
{
|
2012-05-02 19:16:50 +00:00
|
|
|
|
if (length < 10)
|
2011-08-03 14:24:28 +00:00
|
|
|
|
throw Exception("Cannot parse Date: " + std::string(s, length));
|
2011-10-14 18:03:25 +00:00
|
|
|
|
m_month = (s[5] - '0') * 10 + (s[6] - '0');
|
|
|
|
|
m_day = (s[8] - '0') * 10 + (s[9] - '0');
|
2011-08-03 14:24:28 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2011-10-14 18:03:25 +00:00
|
|
|
|
m_month = (s[4] -'0') * 10 + (s[5] -'0');
|
|
|
|
|
m_day = (s[6] - '0')* 10 + (s[7] -'0');
|
2011-08-03 14:24:28 +00:00
|
|
|
|
}
|
2011-03-10 20:31:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
2011-03-15 20:56:42 +00:00
|
|
|
|
explicit Date(time_t time)
|
2011-03-10 20:31:02 +00:00
|
|
|
|
{
|
|
|
|
|
init(time);
|
2011-03-09 19:18:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-11 03:40:14 +00:00
|
|
|
|
Date(DayNum_t day_num)
|
2012-05-02 19:16:50 +00:00
|
|
|
|
{
|
2015-06-26 15:11:31 +00:00
|
|
|
|
const auto & values = DateLUT::instance().getValues(day_num);
|
2012-05-02 19:16:50 +00:00
|
|
|
|
m_year = values.year;
|
|
|
|
|
m_month = values.month;
|
|
|
|
|
m_day = values.day_of_month;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-09 19:18:01 +00:00
|
|
|
|
Date(unsigned short year_, unsigned char month_, unsigned char day_)
|
2011-10-14 18:03:25 +00:00
|
|
|
|
: m_year(year_), m_month(month_), m_day(day_)
|
2011-03-09 19:18:01 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-15 20:56:42 +00:00
|
|
|
|
explicit Date(const std::string & s)
|
2011-03-09 19:18:01 +00:00
|
|
|
|
{
|
2011-03-10 20:31:02 +00:00
|
|
|
|
init(s.data(), s.size());
|
|
|
|
|
}
|
2011-03-09 19:18:01 +00:00
|
|
|
|
|
2011-03-10 20:31:02 +00:00
|
|
|
|
Date(const char * data, size_t length)
|
|
|
|
|
{
|
|
|
|
|
init(data, length);
|
2011-03-09 19:18:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-26 02:44:29 +00:00
|
|
|
|
Date() : m_year(0), m_month(0), m_day(0)
|
2011-03-09 19:18:01 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-25 20:31:34 +00:00
|
|
|
|
Date(const Date & x)
|
|
|
|
|
{
|
|
|
|
|
operator=(x);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Date & operator= (const Date & x)
|
|
|
|
|
{
|
2011-10-14 18:03:25 +00:00
|
|
|
|
m_year = x.m_year;
|
|
|
|
|
m_month = x.m_month;
|
|
|
|
|
m_day = x.m_day;
|
2011-04-25 20:31:34 +00:00
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-15 20:56:42 +00:00
|
|
|
|
Date & operator= (time_t time)
|
|
|
|
|
{
|
|
|
|
|
init(time);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-09 19:18:01 +00:00
|
|
|
|
operator time_t() const
|
|
|
|
|
{
|
2014-07-08 23:52:53 +00:00
|
|
|
|
return DateLUT::instance().makeDate(m_year, m_month, m_day);
|
2011-03-09 19:18:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-11 03:40:14 +00:00
|
|
|
|
DayNum_t getDayNum() const
|
2011-05-25 18:15:00 +00:00
|
|
|
|
{
|
2014-07-08 23:52:53 +00:00
|
|
|
|
return DateLUT::instance().makeDayNum(m_year, m_month, m_day);
|
2011-05-25 18:15:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-11 03:40:14 +00:00
|
|
|
|
operator DayNum_t() const
|
2011-06-03 21:02:20 +00:00
|
|
|
|
{
|
|
|
|
|
return getDayNum();
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-14 18:03:25 +00:00
|
|
|
|
unsigned short year() const { return m_year; }
|
|
|
|
|
unsigned char month() const { return m_month; }
|
|
|
|
|
unsigned char day() const { return m_day; }
|
2011-03-09 19:18:01 +00:00
|
|
|
|
|
2011-10-14 18:03:25 +00:00
|
|
|
|
void year(unsigned short x) { m_year = x; }
|
|
|
|
|
void month(unsigned char x) { m_month = x; }
|
|
|
|
|
void day(unsigned char x) { m_day = x; }
|
2011-03-09 19:18:01 +00:00
|
|
|
|
|
|
|
|
|
bool operator< (const Date & other) const
|
|
|
|
|
{
|
2011-10-14 18:03:25 +00:00
|
|
|
|
return 0 > memcmp(this, &other, sizeof(*this));
|
2011-03-09 19:18:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-21 19:46:50 +00:00
|
|
|
|
bool operator> (const Date & other) const
|
|
|
|
|
{
|
2011-10-14 18:03:25 +00:00
|
|
|
|
return 0 < memcmp(this, &other, sizeof(*this));
|
2011-03-21 19:46:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool operator<= (const Date & other) const
|
|
|
|
|
{
|
2011-10-14 18:03:25 +00:00
|
|
|
|
return 0 >= memcmp(this, &other, sizeof(*this));
|
2011-03-21 19:46:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool operator>= (const Date & other) const
|
|
|
|
|
{
|
2011-10-14 18:03:25 +00:00
|
|
|
|
return 0 <= memcmp(this, &other, sizeof(*this));
|
2011-03-21 19:46:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-09 19:18:01 +00:00
|
|
|
|
bool operator== (const Date & other) const
|
|
|
|
|
{
|
2011-10-14 18:03:25 +00:00
|
|
|
|
return 0 == memcmp(this, &other, sizeof(*this));
|
2011-03-09 19:18:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool operator!= (const Date & other) const
|
|
|
|
|
{
|
|
|
|
|
return !(*this == other);
|
|
|
|
|
}
|
2014-04-10 14:50:11 +00:00
|
|
|
|
|
2014-07-07 15:55:39 +00:00
|
|
|
|
std::string toString(char separator = '-') const
|
2014-04-10 14:50:11 +00:00
|
|
|
|
{
|
|
|
|
|
std::stringstream ss;
|
2014-07-07 15:55:39 +00:00
|
|
|
|
if (separator)
|
|
|
|
|
ss << year() << separator << (month() / 10) << (month() % 10)
|
|
|
|
|
<< separator << (day() / 10) << (day() % 10);
|
|
|
|
|
else
|
|
|
|
|
ss << year() << (month() / 10) << (month() % 10)
|
|
|
|
|
<< (day() / 10) << (day() % 10);
|
2014-04-10 14:50:11 +00:00
|
|
|
|
return ss.str();
|
|
|
|
|
}
|
2011-03-09 19:18:01 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
inline std::ostream & operator<< (std::ostream & ostr, const Date & date)
|
|
|
|
|
{
|
|
|
|
|
return ostr << date.year()
|
2011-03-10 20:31:02 +00:00
|
|
|
|
<< '-' << (date.month() / 10) << (date.month() % 10)
|
|
|
|
|
<< '-' << (date.day() / 10) << (date.day() % 10);
|
2011-03-09 19:18:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2015-03-25 10:08:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace std
|
|
|
|
|
{
|
|
|
|
|
inline string to_string(const mysqlxx::Date & date)
|
|
|
|
|
{
|
|
|
|
|
return date.toString();
|
|
|
|
|
}
|
|
|
|
|
}
|