2020-11-16 03:29:29 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Core/Types.h>
|
|
|
|
|
2021-05-06 15:45:58 +00:00
|
|
|
|
2020-11-16 03:29:29 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2023-07-04 21:22:08 +00:00
|
|
|
class ReadBuffer;
|
|
|
|
class WriteBuffer;
|
2020-11-16 03:29:29 +00:00
|
|
|
|
2023-07-04 21:22:08 +00:00
|
|
|
/// Proleptic Gregorian calendar date.
|
|
|
|
class GregorianDate
|
2020-12-08 18:28:18 +00:00
|
|
|
{
|
2023-07-04 21:22:08 +00:00
|
|
|
public:
|
2023-07-04 22:39:10 +00:00
|
|
|
GregorianDate() {}
|
|
|
|
|
|
|
|
void init(ReadBuffer & in);
|
|
|
|
bool tryInit(ReadBuffer & in);
|
|
|
|
|
2023-07-04 21:22:08 +00:00
|
|
|
/** Construct from date in text form 'YYYY-MM-DD' by reading from
|
|
|
|
* ReadBuffer.
|
|
|
|
*/
|
|
|
|
explicit GregorianDate(ReadBuffer & in);
|
2020-11-16 03:29:29 +00:00
|
|
|
|
2023-07-04 22:39:10 +00:00
|
|
|
void init(int64_t modified_julian_day);
|
|
|
|
bool tryInit(int64_t modified_julian_day);
|
|
|
|
|
2023-07-04 21:22:08 +00:00
|
|
|
/** Construct from Modified Julian Day. The type T is an
|
|
|
|
* integral type which should be at least 32 bits wide, and
|
|
|
|
* should preferably signed.
|
|
|
|
*/
|
|
|
|
explicit GregorianDate(int64_t modified_julian_day);
|
2020-11-16 03:29:29 +00:00
|
|
|
|
2023-07-04 21:22:08 +00:00
|
|
|
/** Convert to Modified Julian Day. The type T is an integral type
|
|
|
|
* which should be at least 32 bits wide, and should preferably
|
|
|
|
* signed.
|
|
|
|
*/
|
|
|
|
int64_t toModifiedJulianDay() const;
|
2023-07-04 22:39:10 +00:00
|
|
|
bool tryToModifiedJulianDay(int64_t & res) const;
|
2020-11-16 03:29:29 +00:00
|
|
|
|
2023-07-04 21:22:08 +00:00
|
|
|
/** Write the date in text form 'YYYY-MM-DD' to a buffer.
|
2020-11-16 03:29:29 +00:00
|
|
|
*/
|
2023-07-04 21:22:08 +00:00
|
|
|
void write(WriteBuffer & buf) const
|
2020-11-16 03:29:29 +00:00
|
|
|
{
|
2023-07-04 21:22:08 +00:00
|
|
|
writeImpl<void>(buf);
|
2020-11-16 03:29:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-04 21:22:08 +00:00
|
|
|
bool tryWrite(WriteBuffer & buf) const
|
2020-11-16 03:29:29 +00:00
|
|
|
{
|
2023-07-04 21:22:08 +00:00
|
|
|
return writeImpl<bool>(buf);
|
2020-11-16 03:29:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-04 21:22:08 +00:00
|
|
|
/** Convert to a string in text form 'YYYY-MM-DD'.
|
2020-11-16 03:29:29 +00:00
|
|
|
*/
|
2023-07-04 21:22:08 +00:00
|
|
|
std::string toString() const;
|
2020-11-16 03:29:29 +00:00
|
|
|
|
2023-07-04 21:22:08 +00:00
|
|
|
int32_t year() const noexcept
|
2020-11-16 03:29:29 +00:00
|
|
|
{
|
2023-07-04 21:22:08 +00:00
|
|
|
return year_;
|
2020-11-16 03:29:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-04 21:22:08 +00:00
|
|
|
uint8_t month() const noexcept
|
2020-11-16 03:29:29 +00:00
|
|
|
{
|
2023-07-04 21:22:08 +00:00
|
|
|
return month_;
|
2020-11-16 03:29:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-04 21:22:08 +00:00
|
|
|
uint8_t dayOfMonth() const noexcept
|
2020-11-16 03:29:29 +00:00
|
|
|
{
|
2023-07-04 21:22:08 +00:00
|
|
|
return day_of_month_;
|
2020-11-16 03:29:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-04 21:22:08 +00:00
|
|
|
private:
|
|
|
|
int32_t year_ = 0;
|
|
|
|
uint8_t month_ = 0;
|
|
|
|
uint8_t day_of_month_ = 0;
|
2020-11-16 03:29:29 +00:00
|
|
|
|
2023-06-25 00:37:10 +00:00
|
|
|
template <typename ReturnType>
|
2023-07-04 21:22:08 +00:00
|
|
|
ReturnType writeImpl(WriteBuffer & buf) const;
|
|
|
|
};
|
2020-11-16 03:29:29 +00:00
|
|
|
|
2023-07-04 21:22:08 +00:00
|
|
|
/** ISO 8601 Ordinal Date.
|
|
|
|
*/
|
|
|
|
class OrdinalDate
|
|
|
|
{
|
|
|
|
public:
|
2023-07-04 22:39:10 +00:00
|
|
|
OrdinalDate() {}
|
|
|
|
|
|
|
|
void init(int32_t year, uint16_t day_of_year);
|
|
|
|
bool tryInit(int32_t year, uint16_t day_of_year);
|
|
|
|
|
|
|
|
void init(int64_t modified_julian_day);
|
|
|
|
bool tryInit(int64_t modified_julian_day);
|
|
|
|
|
2023-07-04 21:22:08 +00:00
|
|
|
OrdinalDate(int32_t year, uint16_t day_of_year);
|
2020-11-16 03:29:29 +00:00
|
|
|
|
2023-07-04 21:22:08 +00:00
|
|
|
/** Construct from Modified Julian Day. The type T is an
|
|
|
|
* integral type which should be at least 32 bits wide, and
|
|
|
|
* should preferably signed.
|
|
|
|
*/
|
|
|
|
explicit OrdinalDate(int64_t modified_julian_day);
|
2023-06-25 00:37:10 +00:00
|
|
|
|
2023-07-04 21:22:08 +00:00
|
|
|
/** Convert to Modified Julian Day. The type T is an integral
|
|
|
|
* type which should be at least 32 bits wide, and should
|
|
|
|
* preferably be signed.
|
|
|
|
*/
|
|
|
|
int64_t toModifiedJulianDay() const noexcept;
|
2020-11-16 03:29:29 +00:00
|
|
|
|
2023-07-04 21:22:08 +00:00
|
|
|
int32_t year() const noexcept
|
2020-11-16 03:29:29 +00:00
|
|
|
{
|
2023-07-04 21:22:08 +00:00
|
|
|
return year_;
|
2020-11-16 03:29:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-04 21:22:08 +00:00
|
|
|
uint16_t dayOfYear() const noexcept
|
2020-11-16 03:29:29 +00:00
|
|
|
{
|
2023-07-04 21:22:08 +00:00
|
|
|
return day_of_year_;
|
2020-11-16 03:29:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-04 21:22:08 +00:00
|
|
|
private:
|
|
|
|
int32_t year_ = 0;
|
|
|
|
uint16_t day_of_year_ = 0;
|
|
|
|
};
|
2022-10-19 01:26:22 +00:00
|
|
|
|
2023-07-04 21:22:08 +00:00
|
|
|
class MonthDay
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** Construct from month and day. */
|
|
|
|
MonthDay(uint8_t month, uint8_t day_of_month);
|
2022-10-19 01:26:22 +00:00
|
|
|
|
2023-07-04 21:22:08 +00:00
|
|
|
/** Construct from day of year in Gregorian or Julian
|
|
|
|
* calendars to month and day.
|
|
|
|
*/
|
|
|
|
MonthDay(bool is_leap_year, uint16_t day_of_year);
|
2022-10-19 01:26:22 +00:00
|
|
|
|
2023-07-04 21:22:08 +00:00
|
|
|
/** Convert month and day in Gregorian or Julian calendars to
|
|
|
|
* day of year.
|
|
|
|
*/
|
|
|
|
uint16_t dayOfYear(bool is_leap_year) const;
|
2020-11-16 03:29:29 +00:00
|
|
|
|
2023-07-04 21:22:08 +00:00
|
|
|
uint8_t month() const noexcept
|
2020-11-16 03:29:29 +00:00
|
|
|
{
|
2023-07-04 21:22:08 +00:00
|
|
|
return month_;
|
2020-11-16 03:29:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-04 21:22:08 +00:00
|
|
|
uint8_t dayOfMonth() const noexcept
|
2020-11-16 03:29:29 +00:00
|
|
|
{
|
2023-07-04 21:22:08 +00:00
|
|
|
return day_of_month_;
|
2020-11-16 03:29:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-04 21:22:08 +00:00
|
|
|
private:
|
|
|
|
uint8_t month_ = 0;
|
|
|
|
uint8_t day_of_month_ = 0;
|
|
|
|
};
|
2020-11-16 03:29:29 +00:00
|
|
|
|
|
|
|
}
|