mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-22 15:42:02 +00:00
dbms: separated mysqlxx::Date and mysqlxx::DateTime [#METR-17973].
This commit is contained in:
parent
48a1fe182d
commit
3519c3cea2
@ -16,12 +16,12 @@ inline Field toField(const T & x)
|
||||
return Field(typename NearestFieldType<T>::Type(x));
|
||||
}
|
||||
|
||||
inline Field toField(const mysqlxx::Date & x)
|
||||
inline Field toField(const LocalDate & x)
|
||||
{
|
||||
return toField(static_cast<UInt16>(x.getDayNum()));
|
||||
}
|
||||
|
||||
inline Field toField(const mysqlxx::DateTime & x)
|
||||
inline Field toField(const LocalDateTime & x)
|
||||
{
|
||||
return toField(static_cast<UInt32>(static_cast<time_t>(x)));
|
||||
}
|
||||
|
@ -113,9 +113,9 @@ private:
|
||||
}
|
||||
|
||||
|
||||
mysqlxx::DateTime getLastModification() const
|
||||
LocalDateTime getLastModification() const
|
||||
{
|
||||
mysqlxx::DateTime update_time{std::time(nullptr)};
|
||||
LocalDateTime update_time{std::time(nullptr)};
|
||||
|
||||
if (dont_check_update_time)
|
||||
return update_time;
|
||||
@ -417,7 +417,7 @@ private:
|
||||
Block sample_block;
|
||||
mutable mysqlxx::PoolWithFailover pool;
|
||||
const std::string load_all_query;
|
||||
mysqlxx::DateTime last_modification;
|
||||
LocalDateTime last_modification;
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -9,8 +9,8 @@
|
||||
#include <common/Common.h>
|
||||
#include <common/DateLUT.h>
|
||||
|
||||
#include <mysqlxx/Date.h>
|
||||
#include <mysqlxx/DateTime.h>
|
||||
#include <common/LocalDate.h>
|
||||
#include <common/LocalDateTime.h>
|
||||
|
||||
#include <DB/Core/Types.h>
|
||||
#include <DB/Common/Exception.h>
|
||||
@ -429,7 +429,7 @@ inline void readDateText(DayNum_t & date, ReadBuffer & buf)
|
||||
date = DateLUT::instance().makeDayNum(year, month, day);
|
||||
}
|
||||
|
||||
inline void readDateText(mysqlxx::Date & date, ReadBuffer & buf)
|
||||
inline void readDateText(LocalDate & date, ReadBuffer & buf)
|
||||
{
|
||||
char s[10];
|
||||
size_t size = buf.read(s, 10);
|
||||
@ -491,7 +491,7 @@ inline void readDateTimeText(time_t & datetime, ReadBuffer & buf)
|
||||
readDateTimeTextFallback(datetime, buf);
|
||||
}
|
||||
|
||||
inline void readDateTimeText(mysqlxx::DateTime & datetime, ReadBuffer & buf)
|
||||
inline void readDateTimeText(LocalDateTime & datetime, ReadBuffer & buf)
|
||||
{
|
||||
char s[19];
|
||||
size_t size = buf.read(s, 19);
|
||||
@ -527,8 +527,8 @@ inline void readBinary(bool & x, ReadBuffer & buf) { readPODBinary(x, buf); }
|
||||
inline void readBinary(uint128 & x, ReadBuffer & buf) { readPODBinary(x, buf); }
|
||||
|
||||
inline void readBinary(VisitID_t & x, ReadBuffer & buf) { readPODBinary(x, buf); }
|
||||
inline void readBinary(mysqlxx::Date & x, ReadBuffer & buf) { readPODBinary(x, buf); }
|
||||
inline void readBinary(mysqlxx::DateTime & x, ReadBuffer & buf) { readPODBinary(x, buf); }
|
||||
inline void readBinary(LocalDate & x, ReadBuffer & buf) { readPODBinary(x, buf); }
|
||||
inline void readBinary(LocalDateTime & x, ReadBuffer & buf) { readPODBinary(x, buf); }
|
||||
|
||||
|
||||
/// Общие методы для чтения значения в текстовом виде из tab-separated формата.
|
||||
@ -546,8 +546,8 @@ inline void readText(String & x, ReadBuffer & buf) { readEscapedString(x, buf);
|
||||
inline void readText(bool & x, ReadBuffer & buf) { readBoolText(x, buf); }
|
||||
|
||||
inline void readText(VisitID_t & x, ReadBuffer & buf) { readIntText(x, buf); }
|
||||
inline void readText(mysqlxx::Date & x, ReadBuffer & buf) { readDateText(x, buf); }
|
||||
inline void readText(mysqlxx::DateTime & x, ReadBuffer & buf) { readDateTimeText(x, buf); }
|
||||
inline void readText(LocalDate & x, ReadBuffer & buf) { readDateText(x, buf); }
|
||||
inline void readText(LocalDateTime & x, ReadBuffer & buf) { readDateTimeText(x, buf); }
|
||||
|
||||
|
||||
/// Общие методы для чтения значения в текстовом виде, при необходимости, в кавычках.
|
||||
@ -566,14 +566,14 @@ inline void readQuoted(bool & x, ReadBuffer & buf) { readBoolText(x, buf); }
|
||||
|
||||
inline void readQuoted(VisitID_t & x, ReadBuffer & buf) { readIntText(x, buf); }
|
||||
|
||||
inline void readQuoted(mysqlxx::Date & x, ReadBuffer & buf)
|
||||
inline void readQuoted(LocalDate & x, ReadBuffer & buf)
|
||||
{
|
||||
assertString("'", buf);
|
||||
readDateText(x, buf);
|
||||
assertString("'", buf);
|
||||
}
|
||||
|
||||
inline void readQuoted(mysqlxx::DateTime & x, ReadBuffer & buf)
|
||||
inline void readQuoted(LocalDateTime & x, ReadBuffer & buf)
|
||||
{
|
||||
assertString("'", buf);
|
||||
readDateTimeText(x, buf);
|
||||
@ -597,14 +597,14 @@ inline void readDoubleQuoted(bool & x, ReadBuffer & buf) { readBoolText(x, buf
|
||||
|
||||
inline void readDoubleQuoted(VisitID_t & x, ReadBuffer & buf) { readIntText(x, buf); }
|
||||
|
||||
inline void readDoubleQuoted(mysqlxx::Date & x, ReadBuffer & buf)
|
||||
inline void readDoubleQuoted(LocalDate & x, ReadBuffer & buf)
|
||||
{
|
||||
assertString("\"", buf);
|
||||
readDateText(x, buf);
|
||||
assertString("\"", buf);
|
||||
}
|
||||
|
||||
inline void readDoubleQuoted(mysqlxx::DateTime & x, ReadBuffer & buf)
|
||||
inline void readDoubleQuoted(LocalDateTime & x, ReadBuffer & buf)
|
||||
{
|
||||
assertString("\"", buf);
|
||||
readDateTimeText(x, buf);
|
||||
|
@ -393,7 +393,7 @@ inline void writeDateText(DayNum_t date, WriteBuffer & buf)
|
||||
buf.write(s, 10);
|
||||
}
|
||||
|
||||
inline void writeDateText(mysqlxx::Date date, WriteBuffer & buf)
|
||||
inline void writeDateText(LocalDate date, WriteBuffer & buf)
|
||||
{
|
||||
char s[10] = {'0', '0', '0', '0', '-', '0', '0', '-', '0', '0'};
|
||||
|
||||
@ -449,7 +449,7 @@ inline void writeDateTimeText(time_t datetime, WriteBuffer & buf)
|
||||
}
|
||||
|
||||
template <char date_delimeter = '-', char time_delimeter = ':'>
|
||||
inline void writeDateTimeText(mysqlxx::DateTime datetime, WriteBuffer & buf)
|
||||
inline void writeDateTimeText(LocalDateTime datetime, WriteBuffer & buf)
|
||||
{
|
||||
char s[19] = {'0', '0', '0', '0', date_delimeter, '0', '0', date_delimeter, '0', '0', ' ', '0', '0', time_delimeter, '0', '0', time_delimeter, '0', '0'};
|
||||
|
||||
@ -508,8 +508,8 @@ inline void writeBinary(const bool & x, WriteBuffer & buf) { writePODBinary(x,
|
||||
inline void writeBinary(const uint128 & x, WriteBuffer & buf) { writePODBinary(x, buf); }
|
||||
|
||||
inline void writeBinary(const VisitID_t & x, WriteBuffer & buf) { writePODBinary(static_cast<const UInt64 &>(x), buf); }
|
||||
inline void writeBinary(const mysqlxx::Date & x, WriteBuffer & buf) { writePODBinary(x, buf); }
|
||||
inline void writeBinary(const mysqlxx::DateTime & x, WriteBuffer & buf) { writePODBinary(x, buf); }
|
||||
inline void writeBinary(const LocalDate & x, WriteBuffer & buf) { writePODBinary(x, buf); }
|
||||
inline void writeBinary(const LocalDateTime & x, WriteBuffer & buf) { writePODBinary(x, buf); }
|
||||
|
||||
|
||||
/// Методы для вывода значения в текстовом виде для tab-separated формата.
|
||||
@ -531,8 +531,8 @@ inline void writeText(const char * x, WriteBuffer & buf) { writeEscapedString(
|
||||
inline void writeText(const char * x, size_t size, WriteBuffer & buf) { writeEscapedString(x, size, buf); }
|
||||
|
||||
inline void writeText(const VisitID_t & x, WriteBuffer & buf) { writeIntText(static_cast<const UInt64 &>(x), buf); }
|
||||
inline void writeText(const mysqlxx::Date & x, WriteBuffer & buf) { writeDateText(x, buf); }
|
||||
inline void writeText(const mysqlxx::DateTime & x, WriteBuffer & buf) { writeDateTimeText(x, buf); }
|
||||
inline void writeText(const LocalDate & x, WriteBuffer & buf) { writeDateText(x, buf); }
|
||||
inline void writeText(const LocalDateTime & x, WriteBuffer & buf) { writeDateTimeText(x, buf); }
|
||||
|
||||
template<typename T>
|
||||
inline void writeText(const mysqlxx::Null<T> & x, WriteBuffer & buf)
|
||||
@ -563,14 +563,14 @@ inline void writeQuoted(const VisitID_t & x, WriteBuffer & buf)
|
||||
writeIntText(static_cast<const UInt64 &>(x), buf);
|
||||
}
|
||||
|
||||
inline void writeQuoted(const mysqlxx::Date & x, WriteBuffer & buf)
|
||||
inline void writeQuoted(const LocalDate & x, WriteBuffer & buf)
|
||||
{
|
||||
writeChar('\'', buf);
|
||||
writeDateText(x, buf);
|
||||
writeChar('\'', buf);
|
||||
}
|
||||
|
||||
inline void writeQuoted(const mysqlxx::DateTime & x, WriteBuffer & buf)
|
||||
inline void writeQuoted(const LocalDateTime & x, WriteBuffer & buf)
|
||||
{
|
||||
writeChar('\'', buf);
|
||||
writeDateTimeText(x, buf);
|
||||
@ -606,14 +606,14 @@ inline void writeDoubleQuoted(const VisitID_t & x, WriteBuffer & buf)
|
||||
writeIntText(static_cast<const UInt64 &>(x), buf);
|
||||
}
|
||||
|
||||
inline void writeDoubleQuoted(const mysqlxx::Date & x, WriteBuffer & buf)
|
||||
inline void writeDoubleQuoted(const LocalDate & x, WriteBuffer & buf)
|
||||
{
|
||||
writeChar('"', buf);
|
||||
writeDateText(x, buf);
|
||||
writeChar('"', buf);
|
||||
}
|
||||
|
||||
inline void writeDoubleQuoted(const mysqlxx::DateTime & x, WriteBuffer & buf)
|
||||
inline void writeDoubleQuoted(const LocalDateTime & x, WriteBuffer & buf)
|
||||
{
|
||||
writeChar('"', buf);
|
||||
writeDateTimeText(x, buf);
|
||||
|
@ -263,7 +263,7 @@ private:
|
||||
if (current_time % 3 != 0)
|
||||
return false;
|
||||
|
||||
mysqlxx::Date now(current_time);
|
||||
LocalDate now(current_time);
|
||||
return (now.month() == 12 && now.day() >= 20)
|
||||
|| (now.month() == 1 && now.day() <= 5);
|
||||
}
|
||||
|
@ -13,8 +13,8 @@ int main(int argc, char ** argv)
|
||||
<< DB::quote << "Hello, world!" << '\n'
|
||||
<< DB::double_quote << "Hello, world!" << '\n'
|
||||
<< DB::binary << "Hello, world!" << '\n'
|
||||
<< mysqlxx::DateTime(time(0)) << '\n'
|
||||
<< mysqlxx::Date(time(0)) << '\n'
|
||||
<< LocalDateTime(time(0)) << '\n'
|
||||
<< LocalDate(time(0)) << '\n'
|
||||
<< 1234567890123456789UL << '\n'
|
||||
<< DB::flush;
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ String QuotaForInterval::toString() const
|
||||
std::stringstream res;
|
||||
|
||||
res << std::fixed << std::setprecision(3)
|
||||
<< "Interval: " << mysqlxx::DateTime(rounded_time) << " - " << mysqlxx::DateTime(rounded_time + duration) << ".\n"
|
||||
<< "Interval: " << LocalDateTime(rounded_time) << " - " << LocalDateTime(rounded_time + duration) << ".\n"
|
||||
<< "Queries: " << used.queries << ".\n"
|
||||
<< "Errors: " << used.errors << ".\n"
|
||||
<< "Result rows: " << used.result_rows << ".\n"
|
||||
@ -129,7 +129,7 @@ void QuotaForInterval::check(size_t max_amount, size_t used_amount, time_t curre
|
||||
|
||||
message << " has been exceeded. "
|
||||
<< resource_name << ": " << used_amount << ", max: " << max_amount << ". "
|
||||
<< "Interval will end at " << mysqlxx::DateTime(rounded_time + duration) << ".";
|
||||
<< "Interval will end at " << LocalDateTime(rounded_time + duration) << ".";
|
||||
|
||||
throw Exception(message.str(), ErrorCodes::QUOTA_EXPIRED);
|
||||
}
|
||||
|
@ -209,7 +209,7 @@ QueryParseResult QueryParser::parse(std::istream & s)
|
||||
result.limit = DB::parse<unsigned>(limit_nodes->item(0)->innerText());
|
||||
|
||||
LOG_DEBUG(log, "CounterID: " << result.CounterID
|
||||
<< ", dates: " << mysqlxx::Date(result.date_first) << " - " << mysqlxx::Date(result.date_last));
|
||||
<< ", dates: " << LocalDate(result.date_first) << " - " << LocalDate(result.date_last));
|
||||
|
||||
/// получаем список имён атрибутов
|
||||
Poco::AutoPtr<Poco::XML::NodeList> attributes = result.query->getElementsByTagName("attribute");
|
||||
|
@ -8,7 +8,7 @@
|
||||
#include <Poco/File.h>
|
||||
#include <common/logger_useful.h>
|
||||
#include <common/Revision.h>
|
||||
#include <mysqlxx/DateTime.h>
|
||||
#include <common/LocalDateTime.h>
|
||||
|
||||
#include <DB/IO/copyData.h>
|
||||
#include <DB/IO/ReadBufferFromFile.h>
|
||||
@ -69,7 +69,7 @@ StatusFile::StatusFile(const std::string & path_)
|
||||
WriteBufferFromFileDescriptor out(fd, 1024);
|
||||
out
|
||||
<< "PID: " << getpid() << "\n"
|
||||
<< "Started at: " << mysqlxx::DateTime(time(0)) << "\n"
|
||||
<< "Started at: " << LocalDateTime(time(0)) << "\n"
|
||||
<< "Revision: " << Revision::get() << "\n";
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ namespace DB
|
||||
void ReplicatedMergeTreeLogEntry::writeText(WriteBuffer & out) const
|
||||
{
|
||||
out << "format version: 3\n"
|
||||
<< "create_time: " << mysqlxx::DateTime(create_time ? create_time : time(0)) << "\n"
|
||||
<< "create_time: " << LocalDateTime(create_time ? create_time : time(0)) << "\n"
|
||||
<< "source replica: " << source_replica << '\n'
|
||||
<< "block_id: " << escape << block_id << '\n';
|
||||
|
||||
@ -68,7 +68,7 @@ void ReplicatedMergeTreeLogEntry::readText(ReadBuffer & in)
|
||||
|
||||
if (format_version >= 2)
|
||||
{
|
||||
mysqlxx::DateTime create_time_dt;
|
||||
LocalDateTime create_time_dt;
|
||||
in >> "create_time: " >> create_time_dt >> "\n";
|
||||
create_time = create_time_dt;
|
||||
}
|
||||
|
@ -227,7 +227,7 @@ BlockInputStreams StorageSystemParts::read(
|
||||
table_column->insert(table);
|
||||
engine_column->insert(engine);
|
||||
|
||||
mysqlxx::Date partition_date {part->month};
|
||||
LocalDate partition_date {part->month};
|
||||
String partition = toString(partition_date.year()) + (partition_date.month() < 10 ? "0" : "") + toString(partition_date.month());
|
||||
partition_column->insert(partition);
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include <DB/IO/ReadHelpers.h>
|
||||
#include <DB/Storages/MergeTree/ActiveDataPartSet.h>
|
||||
#include <mysqlxx/DateTime.h>
|
||||
#include <common/LocalDateTime.h>
|
||||
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
@ -13,7 +13,7 @@ int main(int argc, char ** argv)
|
||||
std::cerr << name << '\n';
|
||||
|
||||
time_t time = DateLUT::instance().YYYYMMDDToDate(DB::parse<UInt32>(name));
|
||||
std::cerr << mysqlxx::DateTime(time) << '\n';
|
||||
std::cerr << LocalDateTime(time) << '\n';
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -2,17 +2,14 @@
|
||||
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <exception>
|
||||
#include <common/DateLUT.h>
|
||||
|
||||
#include <mysqlxx/Exception.h>
|
||||
|
||||
|
||||
namespace mysqlxx
|
||||
{
|
||||
|
||||
/** Хранит дату в broken-down виде.
|
||||
* Может быть инициализирован из даты в текстовом виде '2011-01-01' и из time_t.
|
||||
* Может быть инициализирован из даты в текстовом виде '20110101...(юзаются первые 8 символов)
|
||||
* Может быть инициализирован из даты в текстовом виде '20110101... (используются первые 8 символов)
|
||||
* Неявно преобразуется в time_t.
|
||||
* Сериализуется в ostream в текстовом виде.
|
||||
* Внимание: преобразование в unix timestamp и обратно производится в текущей тайм-зоне!
|
||||
@ -20,7 +17,7 @@ namespace mysqlxx
|
||||
*
|
||||
* packed - для memcmp (из-за того, что m_year - 2 байта, little endian, работает корректно только до 2047 года)
|
||||
*/
|
||||
class __attribute__ ((__packed__)) Date
|
||||
class __attribute__ ((__packed__)) LocalDate
|
||||
{
|
||||
private:
|
||||
unsigned short m_year;
|
||||
@ -40,14 +37,14 @@ private:
|
||||
void init(const char * s, size_t length)
|
||||
{
|
||||
if (length < 8)
|
||||
throw Exception("Cannot parse Date: " + std::string(s, length));
|
||||
throw std::runtime_error("Cannot parse LocalDate: " + std::string(s, length));
|
||||
|
||||
m_year = (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] - '0');
|
||||
|
||||
if (s[4] == '-')
|
||||
{
|
||||
if (length < 10)
|
||||
throw Exception("Cannot parse Date: " + std::string(s, length));
|
||||
throw std::runtime_error("Cannot parse LocalDate: " + std::string(s, length));
|
||||
m_month = (s[5] - '0') * 10 + (s[6] - '0');
|
||||
m_day = (s[8] - '0') * 10 + (s[9] - '0');
|
||||
}
|
||||
@ -59,12 +56,12 @@ private:
|
||||
}
|
||||
|
||||
public:
|
||||
explicit Date(time_t time)
|
||||
explicit LocalDate(time_t time)
|
||||
{
|
||||
init(time);
|
||||
}
|
||||
|
||||
Date(DayNum_t day_num)
|
||||
LocalDate(DayNum_t day_num)
|
||||
{
|
||||
const auto & values = DateLUT::instance().getValues(day_num);
|
||||
m_year = values.year;
|
||||
@ -72,31 +69,31 @@ public:
|
||||
m_day = values.day_of_month;
|
||||
}
|
||||
|
||||
Date(unsigned short year_, unsigned char month_, unsigned char day_)
|
||||
LocalDate(unsigned short year_, unsigned char month_, unsigned char day_)
|
||||
: m_year(year_), m_month(month_), m_day(day_)
|
||||
{
|
||||
}
|
||||
|
||||
explicit Date(const std::string & s)
|
||||
explicit LocalDate(const std::string & s)
|
||||
{
|
||||
init(s.data(), s.size());
|
||||
}
|
||||
|
||||
Date(const char * data, size_t length)
|
||||
LocalDate(const char * data, size_t length)
|
||||
{
|
||||
init(data, length);
|
||||
}
|
||||
|
||||
Date() : m_year(0), m_month(0), m_day(0)
|
||||
LocalDate() : m_year(0), m_month(0), m_day(0)
|
||||
{
|
||||
}
|
||||
|
||||
Date(const Date & x)
|
||||
LocalDate(const LocalDate & x)
|
||||
{
|
||||
operator=(x);
|
||||
}
|
||||
|
||||
Date & operator= (const Date & x)
|
||||
LocalDate & operator= (const LocalDate & x)
|
||||
{
|
||||
m_year = x.m_year;
|
||||
m_month = x.m_month;
|
||||
@ -105,7 +102,7 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
Date & operator= (time_t time)
|
||||
LocalDate & operator= (time_t time)
|
||||
{
|
||||
init(time);
|
||||
return *this;
|
||||
@ -134,36 +131,37 @@ public:
|
||||
void month(unsigned char x) { m_month = x; }
|
||||
void day(unsigned char x) { m_day = x; }
|
||||
|
||||
bool operator< (const Date & other) const
|
||||
bool operator< (const LocalDate & other) const
|
||||
{
|
||||
return 0 > memcmp(this, &other, sizeof(*this));
|
||||
}
|
||||
|
||||
bool operator> (const Date & other) const
|
||||
bool operator> (const LocalDate & other) const
|
||||
{
|
||||
return 0 < memcmp(this, &other, sizeof(*this));
|
||||
}
|
||||
|
||||
bool operator<= (const Date & other) const
|
||||
bool operator<= (const LocalDate & other) const
|
||||
{
|
||||
return 0 >= memcmp(this, &other, sizeof(*this));
|
||||
}
|
||||
|
||||
bool operator>= (const Date & other) const
|
||||
bool operator>= (const LocalDate & other) const
|
||||
{
|
||||
return 0 <= memcmp(this, &other, sizeof(*this));
|
||||
}
|
||||
|
||||
bool operator== (const Date & other) const
|
||||
bool operator== (const LocalDate & other) const
|
||||
{
|
||||
return 0 == memcmp(this, &other, sizeof(*this));
|
||||
}
|
||||
|
||||
bool operator!= (const Date & other) const
|
||||
bool operator!= (const LocalDate & other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
/// NOTE Неэффективно.
|
||||
std::string toString(char separator = '-') const
|
||||
{
|
||||
std::stringstream ss;
|
||||
@ -177,19 +175,17 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
inline std::ostream & operator<< (std::ostream & ostr, const Date & date)
|
||||
inline std::ostream & operator<< (std::ostream & ostr, const LocalDate & date)
|
||||
{
|
||||
return ostr << date.year()
|
||||
<< '-' << (date.month() / 10) << (date.month() % 10)
|
||||
<< '-' << (date.day() / 10) << (date.day() % 10);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
namespace std
|
||||
{
|
||||
inline string to_string(const mysqlxx::Date & date)
|
||||
inline string to_string(const LocalDate & date)
|
||||
{
|
||||
return date.toString();
|
||||
}
|
@ -1,15 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <common/DateLUT.h>
|
||||
|
||||
#include <mysqlxx/Date.h>
|
||||
#include <iomanip>
|
||||
#include <exception>
|
||||
#include <common/DateLUT.h>
|
||||
#include <common/LocalDate.h>
|
||||
|
||||
|
||||
namespace mysqlxx
|
||||
{
|
||||
|
||||
/** Хранит дату и время в broken-down виде.
|
||||
* Может быть инициализирован из даты и времени в текстовом виде '2011-01-01 00:00:00' и из time_t.
|
||||
* Неявно преобразуется в time_t.
|
||||
@ -19,7 +16,7 @@ namespace mysqlxx
|
||||
*
|
||||
* packed - для memcmp (из-за того, что m_year - 2 байта, little endian, работает корректно только до 2047 года)
|
||||
*/
|
||||
class __attribute__ ((__packed__)) DateTime
|
||||
class __attribute__ ((__packed__)) LocalDateTime
|
||||
{
|
||||
private:
|
||||
unsigned short m_year;
|
||||
@ -57,7 +54,7 @@ private:
|
||||
void init(const char * s, size_t length)
|
||||
{
|
||||
if (length < 19)
|
||||
throw Exception("Cannot parse DateTime: " + std::string(s, length));
|
||||
throw std::runtime_error("Cannot parse LocalDateTime: " + std::string(s, length));
|
||||
|
||||
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');
|
||||
@ -69,40 +66,40 @@ private:
|
||||
}
|
||||
|
||||
public:
|
||||
explicit DateTime(time_t time)
|
||||
explicit LocalDateTime(time_t time)
|
||||
{
|
||||
init(time);
|
||||
}
|
||||
|
||||
DateTime(unsigned short year_, unsigned char month_, unsigned char day_,
|
||||
LocalDateTime(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_)
|
||||
{
|
||||
}
|
||||
|
||||
explicit DateTime(const std::string & s)
|
||||
explicit LocalDateTime(const std::string & s)
|
||||
{
|
||||
if (s.size() < 19)
|
||||
throw Exception("Cannot parse DateTime: " + s);
|
||||
throw std::runtime_error("Cannot parse LocalDateTime: " + s);
|
||||
|
||||
init(s.data(), s.size());
|
||||
}
|
||||
|
||||
DateTime() : m_year(0), m_month(0), m_day(0), m_hour(0), m_minute(0), m_second(0)
|
||||
LocalDateTime() : m_year(0), m_month(0), m_day(0), m_hour(0), m_minute(0), m_second(0)
|
||||
{
|
||||
}
|
||||
|
||||
DateTime(const char * data, size_t length)
|
||||
LocalDateTime(const char * data, size_t length)
|
||||
{
|
||||
init(data, length);
|
||||
}
|
||||
|
||||
DateTime(const DateTime & x)
|
||||
LocalDateTime(const LocalDateTime & x)
|
||||
{
|
||||
operator=(x);
|
||||
}
|
||||
|
||||
DateTime & operator= (const DateTime & x)
|
||||
LocalDateTime & operator= (const LocalDateTime & x)
|
||||
{
|
||||
m_year = x.m_year;
|
||||
m_month = x.m_month;
|
||||
@ -114,7 +111,7 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
DateTime & operator= (time_t time)
|
||||
LocalDateTime & operator= (time_t time)
|
||||
{
|
||||
init(time);
|
||||
return *this;
|
||||
@ -141,42 +138,42 @@ public:
|
||||
void minute(unsigned char x) { m_minute = x; }
|
||||
void second(unsigned char x) { m_second = x; }
|
||||
|
||||
Date toDate() const { return Date(m_year, m_month, m_day); }
|
||||
LocalDate toDate() const { return LocalDate(m_year, m_month, m_day); }
|
||||
|
||||
DateTime toStartOfDate() { return DateTime(m_year, m_month, m_day, 0, 0, 0); }
|
||||
LocalDateTime toStartOfDate() { return LocalDateTime(m_year, m_month, m_day, 0, 0, 0); }
|
||||
|
||||
bool operator< (const DateTime & other) const
|
||||
bool operator< (const LocalDateTime & other) const
|
||||
{
|
||||
return 0 > memcmp(this, &other, sizeof(*this));
|
||||
}
|
||||
|
||||
bool operator> (const DateTime & other) const
|
||||
bool operator> (const LocalDateTime & other) const
|
||||
{
|
||||
return 0 < memcmp(this, &other, sizeof(*this));
|
||||
}
|
||||
|
||||
bool operator<= (const DateTime & other) const
|
||||
bool operator<= (const LocalDateTime & other) const
|
||||
{
|
||||
return 0 >= memcmp(this, &other, sizeof(*this));
|
||||
}
|
||||
|
||||
bool operator>= (const DateTime & other) const
|
||||
bool operator>= (const LocalDateTime & other) const
|
||||
{
|
||||
return 0 <= memcmp(this, &other, sizeof(*this));
|
||||
}
|
||||
|
||||
bool operator== (const DateTime & other) const
|
||||
bool operator== (const LocalDateTime & other) const
|
||||
{
|
||||
return 0 == memcmp(this, &other, sizeof(*this));
|
||||
}
|
||||
|
||||
bool operator!= (const DateTime & other) const
|
||||
bool operator!= (const LocalDateTime & other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
};
|
||||
|
||||
inline std::ostream & operator<< (std::ostream & ostr, const DateTime & datetime)
|
||||
inline std::ostream & operator<< (std::ostream & ostr, const LocalDateTime & datetime)
|
||||
{
|
||||
ostr << std::setfill('0') << std::setw(4) << datetime.year();
|
||||
|
||||
@ -189,12 +186,10 @@ inline std::ostream & operator<< (std::ostream & ostr, const DateTime & datetime
|
||||
return ostr;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
namespace std
|
||||
{
|
||||
inline string to_string(const mysqlxx::DateTime & datetime)
|
||||
inline string to_string(const LocalDateTime & datetime)
|
||||
{
|
||||
stringstream str;
|
||||
str << datetime;
|
@ -7,6 +7,7 @@
|
||||
#include <common/singleton.h>
|
||||
|
||||
#include <mysqlxx/Query.h>
|
||||
#include <mysqlxx/Exception.h>
|
||||
|
||||
#define MYSQLXX_DEFAULT_TIMEOUT 60
|
||||
#define MYSQLXX_DEFAULT_RW_TIMEOUT 1800
|
||||
|
@ -54,8 +54,8 @@ struct EscapeManipResult
|
||||
std::ostream & operator<< (double value) { return ostr << value; }
|
||||
std::ostream & operator<< (long long value) { return ostr << value; }
|
||||
std::ostream & operator<< (unsigned long long value) { return ostr << value; }
|
||||
std::ostream & operator<< (Date value) { return ostr << value; }
|
||||
std::ostream & operator<< (DateTime value) { return ostr << value; }
|
||||
std::ostream & operator<< (LocalDate value) { return ostr << value; }
|
||||
std::ostream & operator<< (LocalDateTime value) { return ostr << value; }
|
||||
|
||||
std::ostream & operator<< (const std::string & value)
|
||||
{
|
||||
@ -181,8 +181,8 @@ public:
|
||||
std::ostream & operator<< (double value) { return ostr << value; }
|
||||
std::ostream & operator<< (long long value) { return ostr << value; }
|
||||
std::ostream & operator<< (unsigned long long value) { return ostr << value; }
|
||||
std::ostream & operator<< (Date value) { return ostr << '\'' << value << '\''; }
|
||||
std::ostream & operator<< (DateTime value) { return ostr << '\'' << value << '\''; }
|
||||
std::ostream & operator<< (LocalDate value) { return ostr << '\'' << value << '\''; }
|
||||
std::ostream & operator<< (LocalDateTime value) { return ostr << '\'' << value << '\''; }
|
||||
|
||||
std::ostream & operator<< (const std::string & value)
|
||||
{
|
||||
@ -395,19 +395,19 @@ struct UnEscapeManipResult
|
||||
return istr;
|
||||
}
|
||||
|
||||
std::istream & operator>> (Date & value)
|
||||
std::istream & operator>> (LocalDate & value)
|
||||
{
|
||||
std::string s;
|
||||
(*this) >> s;
|
||||
value = Date(s);
|
||||
value = LocalDate(s);
|
||||
return istr;
|
||||
}
|
||||
|
||||
std::istream & operator>> (DateTime & value)
|
||||
std::istream & operator>> (LocalDateTime & value)
|
||||
{
|
||||
std::string s;
|
||||
(*this) >> s;
|
||||
value = DateTime(s);
|
||||
value = LocalDateTime(s);
|
||||
return istr;
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include <mysqlxx/Types.h>
|
||||
#include <mysqlxx/Value.h>
|
||||
#include <mysqlxx/ResultBase.h>
|
||||
#include <mysqlxx/Exception.h>
|
||||
|
||||
|
||||
namespace mysqlxx
|
||||
|
@ -4,8 +4,8 @@
|
||||
#include <mysql/mysql.h>
|
||||
#include <Poco/Types.h>
|
||||
|
||||
#include <mysqlxx/Date.h>
|
||||
#include <mysqlxx/DateTime.h>
|
||||
#include <common/LocalDate.h>
|
||||
#include <common/LocalDateTime.h>
|
||||
|
||||
|
||||
namespace mysqlxx
|
||||
|
@ -89,15 +89,15 @@ public:
|
||||
}
|
||||
|
||||
/// Получить дату-время (из значения вида '2011-01-01 00:00:00').
|
||||
DateTime getDateTime() const
|
||||
LocalDateTime getDateTime() const
|
||||
{
|
||||
return DateTime(data(), size());
|
||||
return LocalDateTime(data(), size());
|
||||
}
|
||||
|
||||
/// Получить дату (из значения вида '2011-01-01' или '2011-01-01 00:00:00').
|
||||
Date getDate() const
|
||||
LocalDate getDate() const
|
||||
{
|
||||
return Date(data(), size());
|
||||
return LocalDate(data(), size());
|
||||
}
|
||||
|
||||
/// Получить строку.
|
||||
@ -392,8 +392,8 @@ template <> inline unsigned long long Value::get<unsigned long long >() const {
|
||||
template <> inline float Value::get<float >() const { return getDouble(); }
|
||||
template <> inline double Value::get<double >() const { return getDouble(); }
|
||||
template <> inline std::string Value::get<std::string >() const { return getString(); }
|
||||
template <> inline Date Value::get<Date >() const { return getDate(); }
|
||||
template <> inline DateTime Value::get<DateTime >() const { return getDateTime(); }
|
||||
template <> inline LocalDate Value::get<LocalDate >() const { return getDate(); }
|
||||
template <> inline LocalDateTime Value::get<LocalDateTime >() const { return getDateTime(); }
|
||||
|
||||
template <> inline VisitID_t Value::get<VisitID_t >() const { return VisitID_t(getUInt()); }
|
||||
|
||||
|
@ -4,8 +4,8 @@
|
||||
#include <mysqlxx/Transaction.h>
|
||||
#include <mysqlxx/Manip.h>
|
||||
#include <mysqlxx/Pool.h>
|
||||
#include <mysqlxx/Date.h>
|
||||
#include <mysqlxx/DateTime.h>
|
||||
#include <common/LocalDate.h>
|
||||
#include <common/LocalDateTime.h>
|
||||
#include <mysqlxx/Null.h>
|
||||
|
||||
|
||||
@ -63,5 +63,5 @@
|
||||
* из принципа "всё, что не используется сейчас - не реализовано",
|
||||
* а также зависит от небольшого количества кода из других мест репозитория Метрики
|
||||
* (при необходимости, зависимости можно убрать).
|
||||
* Предполагается, что пользователь сам допишет недостающий функционал.
|
||||
* Предполагается, что пользователь сам допишет недостающую функциональность.
|
||||
*/
|
||||
|
@ -33,8 +33,8 @@ int main(int argc, char ** argv)
|
||||
|
||||
time_t t1 = row[0];
|
||||
time_t t2 = row[1];
|
||||
std::cerr << t1 << ", " << mysqlxx::DateTime(t1) << std::endl;
|
||||
std::cerr << t2 << ", " << mysqlxx::DateTime(t2) << std::endl;
|
||||
std::cerr << t1 << ", " << LocalDateTime(t1) << std::endl;
|
||||
std::cerr << t2 << ", " << LocalDateTime(t2) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user