ClickHouse/libs/libmysqlxx/include/mysqlxx/DateTime.h

192 lines
5.0 KiB
C
Raw Normal View History

2011-03-10 20:31:02 +00:00
#ifndef MYSQLXX_DATETIME_H
#define MYSQLXX_DATETIME_H
#include <string>
#include <Yandex/DateLUT.h>
2011-03-15 20:56:42 +00:00
#include <mysqlxx/Date.h>
2011-03-10 20:31:02 +00:00
namespace mysqlxx
{
2011-03-18 20:26:54 +00:00
/** Хранит дату и время в broken-down виде.
* Может быть инициализирован из даты и времени в текстовом виде '2011-01-01 00:00:00' и из time_t.
* Неявно преобразуется в 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-03-15 20:56:42 +00:00
class __attribute__ ((__packed__)) DateTime
2011-03-10 20:31:02 +00:00
{
private:
unsigned short m_year;
unsigned char m_month;
unsigned char m_day;
unsigned char m_hour;
unsigned char m_minute;
unsigned char m_second;
void init(time_t time)
{
2012-06-07 20:02:41 +00:00
if (unlikely(time > DATE_LUT_MAX || time == 0))
{
m_year = 0;
m_month = 0;
m_day = 0;
m_hour = 0;
m_minute = 0;
m_second = 0;
return;
}
2011-03-10 20:31:02 +00:00
Yandex::DateLUTSingleton & date_lut = Yandex::DateLUTSingleton::instance();
const Yandex::DateLUT::Values & values = date_lut.getValues(time);
m_year = values.year;
m_month = values.month;
m_day = values.day_of_month;
m_hour = date_lut.toHourInaccurate(time);
2012-08-31 20:38:05 +00:00
m_minute = date_lut.toMinuteInaccurate(time);
m_second = date_lut.toSecondInaccurate(time);
2011-03-10 20:31:02 +00:00
}
void init(const char * s, size_t length)
{
if (length < 19)
throw Exception("Cannot parse DateTime: " + std::string(s, length));
2011-03-15 21:14:05 +00:00
m_year = (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] - '0');
m_month = (s[5] - '0') * 10 + (s[6] - '0');
m_day = (s[8] - '0') * 10 + (s[9] - '0');
2011-03-10 20:31:02 +00:00
2011-03-15 21:14:05 +00:00
m_hour = (s[11] - '0') * 10 + (s[12] - '0');
m_minute = (s[14] - '0') * 10 + (s[15] - '0');
m_second = (s[17] - '0') * 10 + (s[18] - '0');
2011-03-10 20:31:02 +00:00
}
public:
2011-03-15 20:56:42 +00:00
explicit DateTime(time_t time)
2011-03-10 20:31:02 +00:00
{
init(time);
}
DateTime(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_)
{
}
2011-03-15 20:56:42 +00:00
explicit DateTime(const std::string & s)
2011-03-10 20:31:02 +00:00
{
if (s.size() < 19)
throw Exception("Cannot parse DateTime: " + s);
2011-04-25 20:31:34 +00:00
init(s.data(), s.size());
2011-03-10 20:31:02 +00:00
}
DateTime() : m_year(0), m_month(0), m_day(0), m_hour(0), m_minute(0), m_second(0)
2011-03-10 20:31:02 +00:00
{
}
DateTime(const char * data, size_t length)
{
init(data, length);
}
2011-04-25 20:31:34 +00:00
DateTime(const DateTime & x)
{
operator=(x);
}
DateTime & operator= (const DateTime & x)
{
m_year = x.m_year;
m_month = x.m_month;
m_day = x.m_day;
m_hour = x.m_hour;
m_minute = x.m_minute;
m_second = x.m_second;
return *this;
}
2011-03-15 20:56:42 +00:00
DateTime & operator= (time_t time)
{
init(time);
return *this;
}
2011-03-10 20:31:02 +00:00
operator time_t() const
{
2012-06-07 20:22:15 +00:00
return m_year == 0
? 0
: Yandex::DateLUTSingleton::instance().makeDateTime(m_year, m_month, m_day, m_hour, m_minute, m_second);
2011-03-10 20:31:02 +00:00
}
unsigned short year() const { return m_year; }
unsigned char month() const { return m_month; }
unsigned char day() const { return m_day; }
unsigned char hour() const { return m_hour; }
unsigned char minute() const { return m_minute; }
unsigned char second() const { return m_second; }
void year(unsigned short x) { m_year = x; }
void month(unsigned char x) { m_month = x; }
void day(unsigned char x) { m_day = x; }
void hour(unsigned char x) { m_hour = x; }
void minute(unsigned char x) { m_minute = x; }
void second(unsigned char x) { m_second = x; }
2011-03-15 20:56:42 +00:00
Date toDate() const { return Date(m_year, m_month, m_day); }
DateTime toStartOfDate() { return DateTime(m_year, m_month, m_day, 0, 0, 0); }
2011-03-15 20:56:42 +00:00
bool operator< (const Date & other) const
2011-03-10 20:31:02 +00:00
{
return 0 > memcmp(this, &other, sizeof(*this));
2011-03-10 20:31:02 +00:00
}
2011-03-21 19:46:50 +00:00
bool operator> (const Date & other) const
{
return 0 < memcmp(this, &other, sizeof(*this));
2011-03-21 19:46:50 +00:00
}
bool operator<= (const Date & other) const
{
return 0 >= memcmp(this, &other, sizeof(*this));
}
bool operator>= (const Date & other) const
{
return 0 <= memcmp(this, &other, sizeof(*this));
}
2011-03-15 20:56:42 +00:00
bool operator== (const Date & other) const
2011-03-10 20:31:02 +00:00
{
2011-03-15 20:56:42 +00:00
return 0 == memcmp(this, &other, sizeof(*this));
2011-03-10 20:31:02 +00:00
}
bool operator!= (const DateTime & other) const
{
return !(*this == other);
}
};
inline std::ostream & operator<< (std::ostream & ostr, const DateTime & datetime)
{
return ostr << datetime.year()
<< '-' << (datetime.month() / 10) << (datetime.month() % 10)
<< '-' << (datetime.day() / 10) << (datetime.day() % 10)
<< ' ' << (datetime.hour() / 10) << (datetime.hour() % 10)
<< ':' << (datetime.minute() / 10) << (datetime.minute() % 10)
<< ':' << (datetime.second() / 10) << (datetime.second() % 10);
}
}
#endif