2015-09-29 19:21:02 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-08-17 22:53:46 +00:00
|
|
|
#include "DayNum.h"
|
2020-03-19 10:38:34 +00:00
|
|
|
#include "defines.h"
|
|
|
|
#include "types.h"
|
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
#include <ctime>
|
|
|
|
#include <string>
|
2015-09-29 19:21:02 +00:00
|
|
|
|
2017-10-29 01:13:28 +00:00
|
|
|
#define DATE_LUT_MAX (0xFFFFFFFFU - 86400)
|
|
|
|
#define DATE_LUT_MAX_DAY_NUM (0xFFFFFFFFU / 86400)
|
2017-10-29 00:51:40 +00:00
|
|
|
/// Table size is bigger than DATE_LUT_MAX_DAY_NUM to fill all indices within UInt16 range: this allows to remove extra check.
|
|
|
|
#define DATE_LUT_SIZE 0x10000
|
2015-09-29 19:21:02 +00:00
|
|
|
#define DATE_LUT_MIN_YEAR 1970
|
2020-04-12 22:25:41 +00:00
|
|
|
#define DATE_LUT_MAX_YEAR 2106 /// Last supported year (incomplete)
|
2017-10-29 01:13:28 +00:00
|
|
|
#define DATE_LUT_YEARS (1 + DATE_LUT_MAX_YEAR - DATE_LUT_MIN_YEAR) /// Number of years in lookup table
|
2015-09-29 19:21:02 +00:00
|
|
|
|
2019-01-23 15:24:14 +00:00
|
|
|
#if defined(__PPC__)
|
2019-06-18 09:48:07 +00:00
|
|
|
#if !__clang__
|
|
|
|
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
|
|
|
|
#endif
|
2019-01-23 14:18:19 +00:00
|
|
|
#endif
|
2015-09-29 19:21:02 +00:00
|
|
|
|
2019-06-18 09:48:07 +00:00
|
|
|
/// Flags for toYearWeek() function.
|
|
|
|
enum class WeekModeFlag : UInt8
|
|
|
|
{
|
|
|
|
MONDAY_FIRST = 1,
|
|
|
|
YEAR = 2,
|
|
|
|
FIRST_WEEKDAY = 4,
|
|
|
|
NEWYEAR_DAY = 8
|
|
|
|
};
|
2019-08-28 16:15:57 +00:00
|
|
|
using YearWeek = std::pair<UInt16, UInt8>;
|
2019-06-18 09:48:07 +00:00
|
|
|
|
2016-07-30 01:08:00 +00:00
|
|
|
/** Lookup table to conversion of time to date, and to month / year / day of week / day of month and so on.
|
|
|
|
* First time was implemented for OLAPServer, that needed to do billions of such transformations.
|
2015-09-29 19:21:02 +00:00
|
|
|
*/
|
|
|
|
class DateLUTImpl
|
|
|
|
{
|
|
|
|
public:
|
2020-05-11 07:42:47 +00:00
|
|
|
explicit DateLUTImpl(const std::string & time_zone);
|
|
|
|
|
|
|
|
DateLUTImpl(const DateLUTImpl &) = delete;
|
|
|
|
DateLUTImpl & operator=(const DateLUTImpl &) = delete;
|
|
|
|
DateLUTImpl(const DateLUTImpl &&) = delete;
|
|
|
|
DateLUTImpl & operator=(const DateLUTImpl &&) = delete;
|
2015-09-29 19:21:02 +00:00
|
|
|
|
|
|
|
public:
|
2018-12-11 19:13:22 +00:00
|
|
|
/// The order of fields matters for alignment and sizeof.
|
2017-04-01 07:20:54 +00:00
|
|
|
struct Values
|
|
|
|
{
|
|
|
|
/// Least significat 32 bits from time_t at beginning of the day.
|
2017-10-29 00:51:40 +00:00
|
|
|
/// If the unix timestamp of beginning of the day is negative (example: 1970-01-01 MSK, where time_t == -10800), then value is zero.
|
|
|
|
/// Change to time_t; change constants above; and recompile the sources if you need to support time after 2105 year.
|
|
|
|
UInt32 date;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// Properties of the day.
|
|
|
|
UInt16 year;
|
|
|
|
UInt8 month;
|
|
|
|
UInt8 day_of_month;
|
|
|
|
UInt8 day_of_week;
|
|
|
|
|
2017-10-29 04:18:48 +00:00
|
|
|
/// Total number of days in current month. Actually we can use separate table that is independent of time zone.
|
|
|
|
/// But due to alignment, this field is totally zero cost.
|
|
|
|
UInt8 days_in_month;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// For days, when offset from UTC was changed due to daylight saving time or permanent change, following values could be non zero.
|
2017-07-10 19:30:10 +00:00
|
|
|
Int16 amount_of_offset_change; /// Usually -3600 or 3600, but look at Lord Howe Island.
|
2018-12-11 19:13:22 +00:00
|
|
|
UInt32 time_at_offset_change; /// In seconds from beginning of the day.
|
2017-04-01 07:20:54 +00:00
|
|
|
};
|
2015-09-29 19:21:02 +00:00
|
|
|
|
2018-12-11 19:13:22 +00:00
|
|
|
static_assert(sizeof(Values) == 16);
|
|
|
|
|
2015-09-29 19:21:02 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Lookup table is indexed by DayNum.
|
|
|
|
/// Day nums are the same in all time zones. 1970-01-01 is 0 and so on.
|
2017-10-29 06:32:21 +00:00
|
|
|
/// Table is relatively large, so better not to place the object on stack.
|
2017-04-01 07:20:54 +00:00
|
|
|
/// In comparison to std::vector, plain array is cheaper by one indirection.
|
2017-10-29 00:51:40 +00:00
|
|
|
Values lut[DATE_LUT_SIZE];
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// Year number after DATE_LUT_MIN_YEAR -> day num for start of year.
|
2018-05-25 13:29:15 +00:00
|
|
|
DayNum years_lut[DATE_LUT_YEARS];
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-11-15 22:58:50 +00:00
|
|
|
/// Year number after DATE_LUT_MIN_YEAR * month number starting at zero -> day num for first day of month
|
2018-05-25 13:29:15 +00:00
|
|
|
DayNum years_months_lut[DATE_LUT_YEARS * 12];
|
2017-11-15 22:58:50 +00:00
|
|
|
|
2017-10-29 00:51:40 +00:00
|
|
|
/// UTC offset at beginning of the Unix epoch. The same as unix timestamp of 1970-01-01 00:00:00 local time.
|
2017-04-01 07:20:54 +00:00
|
|
|
time_t offset_at_start_of_epoch;
|
2017-10-29 00:51:40 +00:00
|
|
|
bool offset_is_whole_number_of_hours_everytime;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// Time zone name.
|
|
|
|
std::string time_zone;
|
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
|
2018-03-14 16:19:22 +00:00
|
|
|
/// We can correctly process only timestamps that less DATE_LUT_MAX (i.e. up to 2105 year inclusively)
|
2018-12-23 21:38:19 +00:00
|
|
|
/// We don't care about overflow.
|
|
|
|
inline DayNum findIndex(time_t t) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
/// First guess.
|
2018-12-23 21:38:19 +00:00
|
|
|
DayNum guess(t / 86400);
|
|
|
|
|
|
|
|
/// UTC offset is from -12 to +14 in all known time zones. This requires checking only three indices.
|
|
|
|
|
2020-04-20 07:41:03 +00:00
|
|
|
if ((guess == 0 || t >= lut[guess].date) && t < lut[DayNum(guess + 1)].date)
|
2017-04-01 07:20:54 +00:00
|
|
|
return guess;
|
|
|
|
|
2018-12-23 21:38:19 +00:00
|
|
|
/// Time zones that have offset 0 from UTC do daylight saving time change (if any) towards increasing UTC offset (example: British Standard Time).
|
2020-04-20 07:49:00 +00:00
|
|
|
if (t >= lut[DayNum(guess + 1)].date)
|
|
|
|
return DayNum(guess + 1);
|
2018-12-23 21:38:19 +00:00
|
|
|
|
2020-04-20 07:49:00 +00:00
|
|
|
return DayNum(guess - 1);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline const Values & find(time_t t) const
|
|
|
|
{
|
|
|
|
return lut[findIndex(t)];
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2015-09-29 19:21:02 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
const std::string & getTimeZone() const { return time_zone; }
|
|
|
|
|
2017-10-29 00:51:40 +00:00
|
|
|
/// All functions below are thread-safe; arguments are not checked.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-10 19:30:10 +00:00
|
|
|
inline time_t toDate(time_t t) const { return find(t).date; }
|
|
|
|
inline unsigned toMonth(time_t t) const { return find(t).month; }
|
2017-12-22 01:54:29 +00:00
|
|
|
inline unsigned toQuarter(time_t t) const { return (find(t).month - 1) / 3 + 1; }
|
2017-07-10 19:30:10 +00:00
|
|
|
inline unsigned toYear(time_t t) const { return find(t).year; }
|
|
|
|
inline unsigned toDayOfWeek(time_t t) const { return find(t).day_of_week; }
|
|
|
|
inline unsigned toDayOfMonth(time_t t) const { return find(t).day_of_month; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-10-29 01:13:28 +00:00
|
|
|
/// Round down to start of monday.
|
2017-07-10 19:30:10 +00:00
|
|
|
inline time_t toFirstDayOfWeek(time_t t) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-12-23 21:38:19 +00:00
|
|
|
DayNum index = findIndex(t);
|
|
|
|
return lut[DayNum(index - (lut[index].day_of_week - 1))].date;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline DayNum toFirstDayNumOfWeek(DayNum d) const
|
|
|
|
{
|
|
|
|
return DayNum(d - (lut[d].day_of_week - 1));
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline DayNum toFirstDayNumOfWeek(time_t t) const
|
|
|
|
{
|
|
|
|
return toFirstDayNumOfWeek(toDayNum(t));
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-10-29 01:13:28 +00:00
|
|
|
/// Round down to start of month.
|
2017-07-10 19:30:10 +00:00
|
|
|
inline time_t toFirstDayOfMonth(time_t t) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-12-23 21:38:19 +00:00
|
|
|
DayNum index = findIndex(t);
|
2017-04-01 07:20:54 +00:00
|
|
|
return lut[index - (lut[index].day_of_month - 1)].date;
|
|
|
|
}
|
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline DayNum toFirstDayNumOfMonth(DayNum d) const
|
|
|
|
{
|
|
|
|
return DayNum(d - (lut[d].day_of_month - 1));
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline DayNum toFirstDayNumOfMonth(time_t t) const
|
|
|
|
{
|
|
|
|
return toFirstDayNumOfMonth(toDayNum(t));
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-10-29 01:13:28 +00:00
|
|
|
/// Round down to start of quarter.
|
2018-05-25 13:29:15 +00:00
|
|
|
inline DayNum toFirstDayNumOfQuarter(DayNum d) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-12-23 21:38:19 +00:00
|
|
|
DayNum index = d;
|
2017-10-29 22:55:27 +00:00
|
|
|
size_t month_inside_quarter = (lut[index].month - 1) % 3;
|
|
|
|
|
2018-12-23 21:38:19 +00:00
|
|
|
index -= lut[index].day_of_month;
|
2017-10-29 22:55:27 +00:00
|
|
|
while (month_inside_quarter)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-12-23 21:38:19 +00:00
|
|
|
index -= lut[index].day_of_month;
|
2017-10-29 22:55:27 +00:00
|
|
|
--month_inside_quarter;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-10-29 22:55:27 +00:00
|
|
|
|
2018-05-25 13:29:15 +00:00
|
|
|
return DayNum(index + 1);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline DayNum toFirstDayNumOfQuarter(time_t t) const
|
|
|
|
{
|
|
|
|
return toFirstDayNumOfQuarter(toDayNum(t));
|
|
|
|
}
|
2017-10-29 22:51:00 +00:00
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline time_t toFirstDayOfQuarter(time_t t) const
|
|
|
|
{
|
|
|
|
return fromDayNum(toFirstDayNumOfQuarter(t));
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-10-29 01:13:28 +00:00
|
|
|
/// Round down to start of year.
|
2019-06-18 10:08:12 +00:00
|
|
|
inline time_t toFirstDayOfYear(time_t t) const
|
|
|
|
{
|
|
|
|
return lut[years_lut[lut[findIndex(t)].year - DATE_LUT_MIN_YEAR]].date;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline DayNum toFirstDayNumOfYear(DayNum d) const
|
|
|
|
{
|
|
|
|
return years_lut[lut[d].year - DATE_LUT_MIN_YEAR];
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline DayNum toFirstDayNumOfYear(time_t t) const
|
|
|
|
{
|
|
|
|
return toFirstDayNumOfYear(toDayNum(t));
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-10 19:30:10 +00:00
|
|
|
inline time_t toFirstDayOfNextMonth(time_t t) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-12-23 21:38:19 +00:00
|
|
|
DayNum index = findIndex(t);
|
2017-04-01 07:20:54 +00:00
|
|
|
index += 32 - lut[index].day_of_month;
|
|
|
|
return lut[index - (lut[index].day_of_month - 1)].date;
|
|
|
|
}
|
|
|
|
|
2017-07-10 19:30:10 +00:00
|
|
|
inline time_t toFirstDayOfPrevMonth(time_t t) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-12-23 21:38:19 +00:00
|
|
|
DayNum index = findIndex(t);
|
2017-04-01 07:20:54 +00:00
|
|
|
index -= lut[index].day_of_month;
|
|
|
|
return lut[index - (lut[index].day_of_month - 1)].date;
|
|
|
|
}
|
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline UInt8 daysInMonth(DayNum d) const
|
|
|
|
{
|
|
|
|
return lut[d].days_in_month;
|
|
|
|
}
|
2017-10-29 04:18:48 +00:00
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline UInt8 daysInMonth(time_t t) const
|
|
|
|
{
|
|
|
|
return find(t).days_in_month;
|
|
|
|
}
|
2017-10-29 04:18:48 +00:00
|
|
|
|
2017-10-29 22:38:06 +00:00
|
|
|
inline UInt8 daysInMonth(UInt16 year, UInt8 month) const
|
2017-10-29 04:18:48 +00:00
|
|
|
{
|
2017-11-17 23:48:55 +00:00
|
|
|
/// 32 makes arithmetic more simple.
|
2018-12-23 21:38:19 +00:00
|
|
|
DayNum any_day_of_month = DayNum(years_lut[year - DATE_LUT_MIN_YEAR] + 32 * (month - 1));
|
2017-10-29 04:18:48 +00:00
|
|
|
return lut[any_day_of_month].days_in_month;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-10-29 01:13:28 +00:00
|
|
|
/** Round to start of day, then shift for specified amount of days.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
2019-06-18 10:08:12 +00:00
|
|
|
inline time_t toDateAndShift(time_t t, Int32 days) const
|
|
|
|
{
|
|
|
|
return lut[DayNum(findIndex(t) + days)].date;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-10 19:30:10 +00:00
|
|
|
inline time_t toTime(time_t t) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-12-23 21:38:19 +00:00
|
|
|
DayNum index = findIndex(t);
|
2017-10-29 00:51:40 +00:00
|
|
|
|
|
|
|
if (unlikely(index == 0))
|
2017-10-29 06:32:21 +00:00
|
|
|
return t + offset_at_start_of_epoch;
|
2017-10-29 00:51:40 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
time_t res = t - lut[index].date;
|
|
|
|
|
|
|
|
if (res >= lut[index].time_at_offset_change)
|
|
|
|
res += lut[index].amount_of_offset_change;
|
|
|
|
|
2017-10-29 01:13:28 +00:00
|
|
|
return res - offset_at_start_of_epoch; /// Starting at 1970-01-01 00:00:00 local time.
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-10 19:30:10 +00:00
|
|
|
inline unsigned toHour(time_t t) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-12-23 21:38:19 +00:00
|
|
|
DayNum index = findIndex(t);
|
2017-10-29 00:51:40 +00:00
|
|
|
|
2018-03-14 15:57:13 +00:00
|
|
|
/// If it is not 1970 year (findIndex found nothing appropriate),
|
|
|
|
/// than limit number of hours to avoid insane results like 1970-01-01 89:28:15
|
2017-10-29 00:51:40 +00:00
|
|
|
if (unlikely(index == 0))
|
2018-03-14 15:57:13 +00:00
|
|
|
return static_cast<unsigned>((t + offset_at_start_of_epoch) / 3600) % 24;
|
2017-10-29 00:51:40 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
time_t res = t - lut[index].date;
|
|
|
|
|
2018-12-12 10:08:53 +00:00
|
|
|
/// Data is cleaned to avoid possibility of underflow.
|
2017-04-01 07:20:54 +00:00
|
|
|
if (res >= lut[index].time_at_offset_change)
|
|
|
|
res += lut[index].amount_of_offset_change;
|
|
|
|
|
|
|
|
return res / 3600;
|
|
|
|
}
|
|
|
|
|
2017-10-29 00:51:40 +00:00
|
|
|
/** Only for time zones with/when offset from UTC is multiple of five minutes.
|
|
|
|
* This is true for all time zones: right now, all time zones have an offset that is multiple of 15 minutes.
|
|
|
|
*
|
|
|
|
* "By 1929, most major countries had adopted hourly time zones. Nepal was the last
|
|
|
|
* country to adopt a standard offset, shifting slightly to UTC+5:45 in 1986."
|
|
|
|
* - https://en.wikipedia.org/wiki/Time_zone#Offsets_from_UTC
|
|
|
|
*
|
|
|
|
* Also please note, that unix timestamp doesn't count "leap seconds":
|
|
|
|
* each minute, with added or subtracted leap second, spans exactly 60 unix timestamps.
|
|
|
|
*/
|
|
|
|
|
|
|
|
inline unsigned toSecond(time_t t) const { return t % 60; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-10-29 00:51:40 +00:00
|
|
|
inline unsigned toMinute(time_t t) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-10-29 00:51:40 +00:00
|
|
|
if (offset_is_whole_number_of_hours_everytime)
|
|
|
|
return (t / 60) % 60;
|
|
|
|
|
2020-04-26 16:22:44 +00:00
|
|
|
UInt32 date = find(t).date;
|
|
|
|
return (UInt32(t) - date) / 60 % 60;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-10-29 00:51:40 +00:00
|
|
|
inline time_t toStartOfMinute(time_t t) const { return t / 60 * 60; }
|
|
|
|
inline time_t toStartOfFiveMinute(time_t t) const { return t / 300 * 300; }
|
2017-12-22 10:36:39 +00:00
|
|
|
inline time_t toStartOfFifteenMinutes(time_t t) const { return t / 900 * 900; }
|
2019-02-11 11:59:17 +00:00
|
|
|
inline time_t toStartOfTenMinutes(time_t t) const { return t / 600 * 600; }
|
2019-02-06 22:50:04 +00:00
|
|
|
|
2017-10-29 00:51:40 +00:00
|
|
|
inline time_t toStartOfHour(time_t t) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-10-29 00:51:40 +00:00
|
|
|
if (offset_is_whole_number_of_hours_everytime)
|
|
|
|
return t / 3600 * 3600;
|
|
|
|
|
2020-04-26 16:22:44 +00:00
|
|
|
UInt32 date = find(t).date;
|
|
|
|
return date + (UInt32(t) - date) / 3600 * 3600;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-10-29 00:51:40 +00:00
|
|
|
/** Number of calendar day since the beginning of UNIX epoch (1970-01-01 is zero)
|
|
|
|
* We use just two bytes for it. It covers the range up to 2105 and slightly more.
|
|
|
|
*
|
|
|
|
* This is "calendar" day, it itself is independent of time zone
|
|
|
|
* (conversion from/to unix timestamp will depend on time zone,
|
|
|
|
* because the same calendar day starts/ends at different timestamps in different time zones)
|
|
|
|
*/
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-12-23 21:38:19 +00:00
|
|
|
inline DayNum toDayNum(time_t t) const { return findIndex(t); }
|
2018-05-25 13:29:15 +00:00
|
|
|
inline time_t fromDayNum(DayNum d) const { return lut[d].date; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-05-25 13:29:15 +00:00
|
|
|
inline time_t toDate(DayNum d) const { return lut[d].date; }
|
|
|
|
inline unsigned toMonth(DayNum d) const { return lut[d].month; }
|
|
|
|
inline unsigned toQuarter(DayNum d) const { return (lut[d].month - 1) / 3 + 1; }
|
|
|
|
inline unsigned toYear(DayNum d) const { return lut[d].year; }
|
|
|
|
inline unsigned toDayOfWeek(DayNum d) const { return lut[d].day_of_week; }
|
|
|
|
inline unsigned toDayOfMonth(DayNum d) const { return lut[d].day_of_month; }
|
2018-09-17 01:27:34 +00:00
|
|
|
inline unsigned toDayOfYear(DayNum d) const { return d + 1 - toFirstDayNumOfYear(d); }
|
|
|
|
|
|
|
|
inline unsigned toDayOfYear(time_t t) const { return toDayOfYear(toDayNum(t)); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-10-29 01:13:28 +00:00
|
|
|
/// Number of week from some fixed moment in the past. Week begins at monday.
|
|
|
|
/// (round down to monday and divide DayNum by 7; we made an assumption,
|
|
|
|
/// that in domain of the function there was no weeks with any other number of days than 7)
|
2018-05-25 13:29:15 +00:00
|
|
|
inline unsigned toRelativeWeekNum(DayNum d) const
|
2017-10-29 01:13:28 +00:00
|
|
|
{
|
|
|
|
/// We add 8 to avoid underflow at beginning of unix epoch.
|
2018-09-17 00:42:39 +00:00
|
|
|
return (d + 8 - toDayOfWeek(d)) / 7;
|
2017-10-29 01:13:28 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline unsigned toRelativeWeekNum(time_t t) const
|
|
|
|
{
|
|
|
|
return toRelativeWeekNum(toDayNum(t));
|
|
|
|
}
|
2017-10-29 01:13:28 +00:00
|
|
|
|
2018-09-17 00:42:39 +00:00
|
|
|
/// Get year that contains most of the current week. Week begins at monday.
|
|
|
|
inline unsigned toISOYear(DayNum d) const
|
|
|
|
{
|
|
|
|
/// That's effectively the year of thursday of current week.
|
2018-09-17 01:00:55 +00:00
|
|
|
return toYear(DayNum(d + 4 - toDayOfWeek(d)));
|
2018-09-17 00:42:39 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline unsigned toISOYear(time_t t) const
|
|
|
|
{
|
|
|
|
return toISOYear(toDayNum(t));
|
|
|
|
}
|
2018-09-17 00:42:39 +00:00
|
|
|
|
2018-09-17 03:09:56 +00:00
|
|
|
/// ISO year begins with a monday of the week that is contained more than by half in the corresponding calendar year.
|
|
|
|
/// Example: ISO year 2019 begins at 2018-12-31. And ISO year 2017 begins at 2017-01-02.
|
|
|
|
/// https://en.wikipedia.org/wiki/ISO_week_date
|
2018-09-17 00:42:39 +00:00
|
|
|
inline DayNum toFirstDayNumOfISOYear(DayNum d) const
|
|
|
|
{
|
2018-09-17 03:09:56 +00:00
|
|
|
auto iso_year = toISOYear(d);
|
|
|
|
|
|
|
|
DayNum first_day_of_year = years_lut[iso_year - DATE_LUT_MIN_YEAR];
|
2018-09-17 00:42:39 +00:00
|
|
|
auto first_day_of_week_of_year = lut[first_day_of_year].day_of_week;
|
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
return DayNum(first_day_of_week_of_year <= 4
|
|
|
|
? first_day_of_year + 1 - first_day_of_week_of_year
|
|
|
|
: first_day_of_year + 8 - first_day_of_week_of_year);
|
2018-09-17 00:42:39 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline DayNum toFirstDayNumOfISOYear(time_t t) const
|
|
|
|
{
|
|
|
|
return toFirstDayNumOfISOYear(toDayNum(t));
|
|
|
|
}
|
2018-09-17 00:42:39 +00:00
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline time_t toFirstDayOfISOYear(time_t t) const
|
|
|
|
{
|
|
|
|
return fromDayNum(toFirstDayNumOfISOYear(t));
|
|
|
|
}
|
2018-09-17 00:42:39 +00:00
|
|
|
|
|
|
|
/// ISO 8601 week number. Week begins at monday.
|
|
|
|
/// The week number 1 is the first week in year that contains 4 or more days (that's more than half).
|
2019-06-18 10:08:12 +00:00
|
|
|
inline unsigned toISOWeek(DayNum d) const
|
|
|
|
{
|
|
|
|
return 1 + DayNum(toFirstDayNumOfWeek(d) - toFirstDayNumOfISOYear(d)) / 7;
|
|
|
|
}
|
2019-06-09 15:19:15 +00:00
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline unsigned toISOWeek(time_t t) const
|
|
|
|
{
|
|
|
|
return toISOWeek(toDayNum(t));
|
|
|
|
}
|
2019-06-18 11:29:28 +00:00
|
|
|
|
2019-06-09 15:19:15 +00:00
|
|
|
/*
|
|
|
|
The bits in week_mode has the following meaning:
|
2020-02-17 19:09:56 +00:00
|
|
|
WeekModeFlag::MONDAY_FIRST (0) If not set Sunday is first day of week
|
|
|
|
If set Monday is first day of week
|
|
|
|
WeekModeFlag::YEAR (1) If not set Week is in range 0-53
|
2019-06-09 15:19:15 +00:00
|
|
|
|
|
|
|
Week 0 is returned for the the last week of the previous year (for
|
|
|
|
a date at start of january) In this case one can get 53 for the
|
|
|
|
first week of next year. This flag ensures that the week is
|
|
|
|
relevant for the given year. Note that this flag is only
|
2019-06-18 09:48:07 +00:00
|
|
|
releveant if WeekModeFlag::JANUARY is not set.
|
2019-06-09 15:19:15 +00:00
|
|
|
|
2020-02-17 19:09:56 +00:00
|
|
|
If set Week is in range 1-53.
|
2019-06-09 15:19:15 +00:00
|
|
|
|
|
|
|
In this case one may get week 53 for a date in January (when
|
|
|
|
the week is that last week of previous year) and week 1 for a
|
|
|
|
date in December.
|
|
|
|
|
2020-02-17 19:09:56 +00:00
|
|
|
WeekModeFlag::FIRST_WEEKDAY (2) If not set Weeks are numbered according
|
2019-06-09 15:19:15 +00:00
|
|
|
to ISO 8601:1988
|
2020-02-17 19:09:56 +00:00
|
|
|
If set The week that contains the first
|
2019-06-09 15:19:15 +00:00
|
|
|
'first-day-of-week' is week 1.
|
|
|
|
|
2020-02-17 19:09:56 +00:00
|
|
|
WeekModeFlag::NEWYEAR_DAY (3) If not set no meaning
|
|
|
|
If set The week that contains the January 1 is week 1.
|
2019-06-09 15:19:15 +00:00
|
|
|
Week is in range 1-53.
|
2019-06-18 09:48:07 +00:00
|
|
|
And ignore WeekModeFlag::YEAR, WeekModeFlag::FIRST_WEEKDAY
|
2019-06-09 15:19:15 +00:00
|
|
|
|
|
|
|
ISO 8601:1988 means that if the week containing January 1 has
|
|
|
|
four or more days in the new year, then it is week 1;
|
|
|
|
Otherwise it is the last week of the previous year, and the
|
|
|
|
next week is week 1.
|
|
|
|
*/
|
2019-06-18 09:48:07 +00:00
|
|
|
inline YearWeek toYearWeek(DayNum d, UInt8 week_mode) const
|
2019-06-09 15:19:15 +00:00
|
|
|
{
|
2019-06-18 09:48:07 +00:00
|
|
|
bool newyear_day_mode = week_mode & static_cast<UInt8>(WeekModeFlag::NEWYEAR_DAY);
|
2019-06-09 15:19:15 +00:00
|
|
|
week_mode = check_week_mode(week_mode);
|
2019-06-18 09:48:07 +00:00
|
|
|
bool monday_first_mode = week_mode & static_cast<UInt8>(WeekModeFlag::MONDAY_FIRST);
|
|
|
|
bool week_year_mode = week_mode & static_cast<UInt8>(WeekModeFlag::YEAR);
|
|
|
|
bool first_weekday_mode = week_mode & static_cast<UInt8>(WeekModeFlag::FIRST_WEEKDAY);
|
2019-06-09 15:19:15 +00:00
|
|
|
|
2019-06-18 09:48:07 +00:00
|
|
|
// Calculate week number of WeekModeFlag::NEWYEAR_DAY mode
|
2019-06-09 15:19:15 +00:00
|
|
|
if (newyear_day_mode)
|
|
|
|
{
|
2019-06-18 09:48:07 +00:00
|
|
|
return toYearWeekOfNewyearMode(d, monday_first_mode);
|
2019-06-09 15:19:15 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 09:48:07 +00:00
|
|
|
YearWeek yw(toYear(d), 0);
|
|
|
|
UInt16 days = 0;
|
|
|
|
UInt16 daynr = makeDayNum(yw.first, toMonth(d), toDayOfMonth(d));
|
|
|
|
UInt16 first_daynr = makeDayNum(yw.first, 1, 1);
|
2019-06-09 15:19:15 +00:00
|
|
|
|
|
|
|
// 0 for monday, 1 for tuesday ...
|
|
|
|
// get weekday from first day in year.
|
2019-06-18 09:48:07 +00:00
|
|
|
UInt16 weekday = calc_weekday(DayNum(first_daynr), !monday_first_mode);
|
2019-06-09 15:19:15 +00:00
|
|
|
|
2019-06-18 11:29:28 +00:00
|
|
|
if (toMonth(d) == 1 && toDayOfMonth(d) <= static_cast<UInt32>(7 - weekday))
|
2019-06-09 15:19:15 +00:00
|
|
|
{
|
|
|
|
if (!week_year_mode && ((first_weekday_mode && weekday != 0) || (!first_weekday_mode && weekday >= 4)))
|
2019-06-18 09:48:07 +00:00
|
|
|
return yw;
|
2019-06-09 15:19:15 +00:00
|
|
|
week_year_mode = 1;
|
2019-06-18 09:48:07 +00:00
|
|
|
(yw.first)--;
|
|
|
|
first_daynr -= (days = calc_days_in_year(yw.first));
|
2019-06-09 15:19:15 +00:00
|
|
|
weekday = (weekday + 53 * 7 - days) % 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((first_weekday_mode && weekday != 0) || (!first_weekday_mode && weekday >= 4))
|
|
|
|
days = daynr - (first_daynr + (7 - weekday));
|
|
|
|
else
|
|
|
|
days = daynr - (first_daynr - weekday);
|
|
|
|
|
|
|
|
if (week_year_mode && days >= 52 * 7)
|
|
|
|
{
|
2019-06-18 09:48:07 +00:00
|
|
|
weekday = (weekday + calc_days_in_year(yw.first)) % 7;
|
2019-06-09 15:19:15 +00:00
|
|
|
if ((!first_weekday_mode && weekday < 4) || (first_weekday_mode && weekday == 0))
|
|
|
|
{
|
2019-06-18 09:48:07 +00:00
|
|
|
(yw.first)++;
|
|
|
|
yw.second = 1;
|
|
|
|
return yw;
|
2019-06-09 15:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-18 09:48:07 +00:00
|
|
|
yw.second = days / 7 + 1;
|
|
|
|
return yw;
|
2018-09-17 00:42:39 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 09:48:07 +00:00
|
|
|
/// Calculate week number of WeekModeFlag::NEWYEAR_DAY mode
|
2019-06-09 15:19:15 +00:00
|
|
|
/// The week number 1 is the first week in year that contains January 1,
|
2019-06-18 09:48:07 +00:00
|
|
|
inline YearWeek toYearWeekOfNewyearMode(DayNum d, bool monday_first_mode) const
|
2019-05-05 07:54:51 +00:00
|
|
|
{
|
2019-06-18 09:48:07 +00:00
|
|
|
YearWeek yw(0, 0);
|
2019-06-09 15:19:15 +00:00
|
|
|
UInt16 offset_day = monday_first_mode ? 0U : 1U;
|
|
|
|
|
2019-05-05 07:54:51 +00:00
|
|
|
// Checking the week across the year
|
2019-06-18 09:48:07 +00:00
|
|
|
yw.first = toYear(DayNum(d + 7 - toDayOfWeek(DayNum(d + offset_day))));
|
2019-05-05 07:54:51 +00:00
|
|
|
|
2019-06-18 09:48:07 +00:00
|
|
|
DayNum first_day = makeDayNum(yw.first, 1, 1);
|
2019-06-09 15:19:15 +00:00
|
|
|
DayNum this_day = d;
|
2019-05-05 07:54:51 +00:00
|
|
|
|
2019-06-09 15:19:15 +00:00
|
|
|
if (monday_first_mode)
|
|
|
|
{
|
|
|
|
// Rounds down a date to the nearest Monday.
|
|
|
|
first_day = toFirstDayNumOfWeek(first_day);
|
|
|
|
this_day = toFirstDayNumOfWeek(d);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Rounds down a date to the nearest Sunday.
|
|
|
|
if (toDayOfWeek(first_day) != 7)
|
|
|
|
first_day = DayNum(first_day - toDayOfWeek(first_day));
|
|
|
|
if (toDayOfWeek(d) != 7)
|
|
|
|
this_day = DayNum(d - toDayOfWeek(d));
|
|
|
|
}
|
2019-06-18 09:48:07 +00:00
|
|
|
yw.second = (this_day - first_day) / 7 + 1;
|
|
|
|
return yw;
|
2019-05-05 07:54:51 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 09:48:07 +00:00
|
|
|
/**
|
|
|
|
* get first day of week with week_mode, return Sunday or Monday
|
|
|
|
*/
|
|
|
|
inline DayNum toFirstDayNumOfWeek(DayNum d, UInt8 week_mode) const
|
2019-05-05 07:54:51 +00:00
|
|
|
{
|
2019-06-18 09:48:07 +00:00
|
|
|
bool monday_first_mode = week_mode & static_cast<UInt8>(WeekModeFlag::MONDAY_FIRST);
|
|
|
|
if (monday_first_mode)
|
|
|
|
{
|
|
|
|
return toFirstDayNumOfWeek(d);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return (toDayOfWeek(d) != 7) ? DayNum(d - toDayOfWeek(d)) : d;
|
|
|
|
}
|
2019-05-05 07:54:51 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 15:19:15 +00:00
|
|
|
/*
|
2019-06-18 09:48:07 +00:00
|
|
|
* check and change mode to effective
|
|
|
|
*/
|
|
|
|
inline UInt8 check_week_mode(UInt8 mode) const
|
2019-05-05 07:54:51 +00:00
|
|
|
{
|
2019-06-18 09:48:07 +00:00
|
|
|
UInt8 week_format = (mode & 7);
|
|
|
|
if (!(week_format & static_cast<UInt8>(WeekModeFlag::MONDAY_FIRST)))
|
|
|
|
week_format ^= static_cast<UInt8>(WeekModeFlag::FIRST_WEEKDAY);
|
|
|
|
return week_format;
|
|
|
|
}
|
2019-06-09 15:19:15 +00:00
|
|
|
|
|
|
|
/*
|
2019-06-18 09:48:07 +00:00
|
|
|
* Calc weekday from d
|
|
|
|
* Returns 0 for monday, 1 for tuesday ...
|
|
|
|
*/
|
|
|
|
inline unsigned calc_weekday(DayNum d, bool sunday_first_day_of_week) const
|
2019-05-05 07:54:51 +00:00
|
|
|
{
|
2019-06-18 09:48:07 +00:00
|
|
|
if (!sunday_first_day_of_week)
|
|
|
|
return toDayOfWeek(d) - 1;
|
|
|
|
else
|
|
|
|
return toDayOfWeek(DayNum(d + 1)) - 1;
|
2019-05-05 07:54:51 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 15:19:15 +00:00
|
|
|
/* Calc days in one year. */
|
2019-06-18 09:48:07 +00:00
|
|
|
inline unsigned calc_days_in_year(UInt16 year) const
|
2019-05-05 07:54:51 +00:00
|
|
|
{
|
2019-06-09 15:19:15 +00:00
|
|
|
return ((year & 3) == 0 && (year % 100 || (year % 400 == 0 && year)) ? 366 : 365);
|
2019-05-05 07:54:51 +00:00
|
|
|
}
|
|
|
|
|
2017-10-29 01:13:28 +00:00
|
|
|
/// Number of month from some fixed moment in the past (year * 12 + month)
|
2019-06-18 10:08:12 +00:00
|
|
|
inline unsigned toRelativeMonthNum(DayNum d) const
|
|
|
|
{
|
|
|
|
return lut[d].year * 12 + lut[d].month;
|
|
|
|
}
|
2017-10-29 01:13:28 +00:00
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline unsigned toRelativeMonthNum(time_t t) const
|
|
|
|
{
|
|
|
|
return toRelativeMonthNum(toDayNum(t));
|
|
|
|
}
|
2017-12-22 01:54:29 +00:00
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline unsigned toRelativeQuarterNum(DayNum d) const
|
|
|
|
{
|
|
|
|
return lut[d].year * 4 + (lut[d].month - 1) / 3;
|
|
|
|
}
|
2017-12-22 01:54:29 +00:00
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline unsigned toRelativeQuarterNum(time_t t) const
|
|
|
|
{
|
|
|
|
return toRelativeQuarterNum(toDayNum(t));
|
|
|
|
}
|
2017-10-29 01:13:28 +00:00
|
|
|
|
2017-12-13 18:42:13 +00:00
|
|
|
/// We count all hour-length intervals, unrelated to offset changes.
|
2017-10-29 01:13:28 +00:00
|
|
|
inline time_t toRelativeHourNum(time_t t) const
|
|
|
|
{
|
|
|
|
if (offset_is_whole_number_of_hours_everytime)
|
|
|
|
return t / 3600;
|
|
|
|
|
|
|
|
/// Assume that if offset was fractional, then the fraction is the same as at the beginning of epoch.
|
2020-04-20 07:10:25 +00:00
|
|
|
/// NOTE This assumption is false for "Pacific/Pitcairn" and "Pacific/Kiritimati" time zones.
|
2017-10-29 01:13:28 +00:00
|
|
|
return (t + 86400 - offset_at_start_of_epoch) / 3600;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline time_t toRelativeHourNum(DayNum d) const
|
|
|
|
{
|
|
|
|
return toRelativeHourNum(lut[d].date);
|
|
|
|
}
|
2017-12-22 01:54:29 +00:00
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline time_t toRelativeMinuteNum(time_t t) const
|
|
|
|
{
|
|
|
|
return t / 60;
|
|
|
|
}
|
2017-10-29 01:13:28 +00:00
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline time_t toRelativeMinuteNum(DayNum d) const
|
|
|
|
{
|
|
|
|
return toRelativeMinuteNum(lut[d].date);
|
|
|
|
}
|
2017-12-22 01:54:29 +00:00
|
|
|
|
2019-02-11 11:59:17 +00:00
|
|
|
inline DayNum toStartOfYearInterval(DayNum d, UInt64 years) const
|
|
|
|
{
|
|
|
|
if (years == 1)
|
|
|
|
return toFirstDayNumOfYear(d);
|
|
|
|
return years_lut[(lut[d].year - DATE_LUT_MIN_YEAR) / years * years];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline DayNum toStartOfQuarterInterval(DayNum d, UInt64 quarters) const
|
|
|
|
{
|
|
|
|
if (quarters == 1)
|
|
|
|
return toFirstDayNumOfQuarter(d);
|
|
|
|
return toStartOfMonthInterval(d, quarters * 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline DayNum toStartOfMonthInterval(DayNum d, UInt64 months) const
|
|
|
|
{
|
|
|
|
if (months == 1)
|
|
|
|
return toFirstDayNumOfMonth(d);
|
|
|
|
const auto & date = lut[d];
|
|
|
|
UInt32 month_total_index = (date.year - DATE_LUT_MIN_YEAR) * 12 + date.month - 1;
|
|
|
|
return years_months_lut[month_total_index / months * months];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline DayNum toStartOfWeekInterval(DayNum d, UInt64 weeks) const
|
|
|
|
{
|
|
|
|
if (weeks == 1)
|
|
|
|
return toFirstDayNumOfWeek(d);
|
|
|
|
UInt64 days = weeks * 7;
|
|
|
|
// January 1st 1970 was Thursday so we need this 4-days offset to make weeks start on Monday.
|
|
|
|
return DayNum(4 + (d - 4) / days * days);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline time_t toStartOfDayInterval(DayNum d, UInt64 days) const
|
|
|
|
{
|
|
|
|
if (days == 1)
|
|
|
|
return toDate(d);
|
|
|
|
return lut[d / days * days].date;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline time_t toStartOfHourInterval(time_t t, UInt64 hours) const
|
|
|
|
{
|
|
|
|
if (hours == 1)
|
|
|
|
return toStartOfHour(t);
|
|
|
|
UInt64 seconds = hours * 3600;
|
|
|
|
t = t / seconds * seconds;
|
|
|
|
if (offset_is_whole_number_of_hours_everytime)
|
|
|
|
return t;
|
|
|
|
return toStartOfHour(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline time_t toStartOfMinuteInterval(time_t t, UInt64 minutes) const
|
|
|
|
{
|
|
|
|
if (minutes == 1)
|
|
|
|
return toStartOfMinute(t);
|
|
|
|
UInt64 seconds = 60 * minutes;
|
|
|
|
return t / seconds * seconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline time_t toStartOfSecondInterval(time_t t, UInt64 seconds) const
|
|
|
|
{
|
|
|
|
if (seconds == 1)
|
|
|
|
return t;
|
|
|
|
return t / seconds * seconds;
|
|
|
|
}
|
|
|
|
|
2018-05-25 13:29:15 +00:00
|
|
|
/// Create DayNum from year, month, day of month.
|
|
|
|
inline DayNum makeDayNum(UInt16 year, UInt8 month, UInt8 day_of_month) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-06-18 10:08:12 +00:00
|
|
|
if (unlikely(year < DATE_LUT_MIN_YEAR || year > DATE_LUT_MAX_YEAR || month < 1 || month > 12 || day_of_month < 1 || day_of_month > 31))
|
2019-09-26 15:12:40 +00:00
|
|
|
return DayNum(0); // TODO (nemkov, DateTime64 phase 2): implement creating real date for year outside of LUT range.
|
2017-11-15 22:58:50 +00:00
|
|
|
|
2018-05-25 13:29:15 +00:00
|
|
|
return DayNum(years_months_lut[(year - DATE_LUT_MIN_YEAR) * 12 + month - 1] + day_of_month - 1);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline time_t makeDate(UInt16 year, UInt8 month, UInt8 day_of_month) const
|
|
|
|
{
|
|
|
|
return lut[makeDayNum(year, month, day_of_month)].date;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/** Does not accept daylight saving time as argument: in case of ambiguity, it choose greater timestamp.
|
|
|
|
*/
|
2017-10-29 22:38:06 +00:00
|
|
|
inline time_t makeDateTime(UInt16 year, UInt8 month, UInt8 day_of_month, UInt8 hour, UInt8 minute, UInt8 second) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
size_t index = makeDayNum(year, month, day_of_month);
|
|
|
|
time_t time_offset = hour * 3600 + minute * 60 + second;
|
|
|
|
|
|
|
|
if (time_offset >= lut[index].time_at_offset_change)
|
|
|
|
time_offset -= lut[index].amount_of_offset_change;
|
|
|
|
|
|
|
|
return lut[index].date + time_offset;
|
|
|
|
}
|
|
|
|
|
2018-05-25 13:29:15 +00:00
|
|
|
inline const Values & getValues(DayNum d) const { return lut[d]; }
|
2017-10-29 01:13:28 +00:00
|
|
|
inline const Values & getValues(time_t t) const { return lut[findIndex(t)]; }
|
|
|
|
|
2017-07-21 14:22:53 +00:00
|
|
|
inline UInt32 toNumYYYYMM(time_t t) const
|
|
|
|
{
|
|
|
|
const Values & values = find(t);
|
|
|
|
return values.year * 100 + values.month;
|
|
|
|
}
|
|
|
|
|
2018-05-25 13:29:15 +00:00
|
|
|
inline UInt32 toNumYYYYMM(DayNum d) const
|
2017-07-21 14:22:53 +00:00
|
|
|
{
|
2017-10-29 00:51:40 +00:00
|
|
|
const Values & values = lut[d];
|
2017-07-21 14:22:53 +00:00
|
|
|
return values.year * 100 + values.month;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
inline UInt32 toNumYYYYMMDD(time_t t) const
|
|
|
|
{
|
|
|
|
const Values & values = find(t);
|
|
|
|
return values.year * 10000 + values.month * 100 + values.day_of_month;
|
|
|
|
}
|
|
|
|
|
2018-05-25 13:29:15 +00:00
|
|
|
inline UInt32 toNumYYYYMMDD(DayNum d) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-10-29 00:51:40 +00:00
|
|
|
const Values & values = lut[d];
|
2017-04-01 07:20:54 +00:00
|
|
|
return values.year * 10000 + values.month * 100 + values.day_of_month;
|
|
|
|
}
|
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline time_t YYYYMMDDToDate(UInt32 num) const
|
|
|
|
{
|
|
|
|
return makeDate(num / 10000, num / 100 % 100, num % 100);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline DayNum YYYYMMDDToDayNum(UInt32 num) const
|
|
|
|
{
|
|
|
|
return makeDayNum(num / 10000, num / 100 % 100, num % 100);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
inline UInt64 toNumYYYYMMDDhhmmss(time_t t) const
|
|
|
|
{
|
|
|
|
const Values & values = find(t);
|
2019-06-18 10:08:12 +00:00
|
|
|
return
|
|
|
|
toSecond(t)
|
|
|
|
+ toMinute(t) * 100
|
|
|
|
+ toHour(t) * 10000
|
|
|
|
+ UInt64(values.day_of_month) * 1000000
|
|
|
|
+ UInt64(values.month) * 100000000
|
|
|
|
+ UInt64(values.year) * 10000000000;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline time_t YYYYMMDDhhmmssToTime(UInt64 num) const
|
|
|
|
{
|
2019-06-18 10:08:12 +00:00
|
|
|
return makeDateTime(
|
|
|
|
num / 10000000000,
|
|
|
|
num / 100000000 % 100,
|
|
|
|
num / 1000000 % 100,
|
|
|
|
num / 10000 % 100,
|
|
|
|
num / 100 % 100,
|
|
|
|
num % 100);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-10-29 04:18:48 +00:00
|
|
|
/// Adding calendar intervals.
|
|
|
|
/// Implementation specific behaviour when delta is too big.
|
|
|
|
|
2017-10-29 22:38:06 +00:00
|
|
|
inline time_t addDays(time_t t, Int64 delta) const
|
2017-10-29 04:18:48 +00:00
|
|
|
{
|
2018-12-23 21:38:19 +00:00
|
|
|
DayNum index = findIndex(t);
|
2017-10-29 04:18:48 +00:00
|
|
|
time_t time_offset = toHour(t) * 3600 + toMinute(t) * 60 + toSecond(t);
|
|
|
|
|
|
|
|
index += delta;
|
|
|
|
|
|
|
|
if (time_offset >= lut[index].time_at_offset_change)
|
|
|
|
time_offset -= lut[index].amount_of_offset_change;
|
|
|
|
|
|
|
|
return lut[index].date + time_offset;
|
|
|
|
}
|
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline time_t addWeeks(time_t t, Int64 delta) const
|
|
|
|
{
|
|
|
|
return addDays(t, delta * 7);
|
|
|
|
}
|
2017-10-29 04:18:48 +00:00
|
|
|
|
2017-10-29 22:38:06 +00:00
|
|
|
inline UInt8 saturateDayOfMonth(UInt16 year, UInt8 month, UInt8 day_of_month) const
|
2017-10-29 04:18:48 +00:00
|
|
|
{
|
|
|
|
if (likely(day_of_month <= 28))
|
|
|
|
return day_of_month;
|
|
|
|
|
2017-10-29 22:38:06 +00:00
|
|
|
UInt8 days_in_month = daysInMonth(year, month);
|
2017-10-29 04:18:48 +00:00
|
|
|
|
|
|
|
if (day_of_month > days_in_month)
|
|
|
|
day_of_month = days_in_month;
|
|
|
|
|
|
|
|
return day_of_month;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If resulting month has less deys than source month, then saturation can happen.
|
|
|
|
/// Example: 31 Aug + 1 month = 30 Sep.
|
2017-10-29 22:38:06 +00:00
|
|
|
inline time_t addMonths(time_t t, Int64 delta) const
|
2017-10-29 04:18:48 +00:00
|
|
|
{
|
2018-05-25 13:29:15 +00:00
|
|
|
DayNum result_day = addMonths(toDayNum(t), delta);
|
2017-10-29 04:18:48 +00:00
|
|
|
|
|
|
|
time_t time_offset = toHour(t) * 3600 + toMinute(t) * 60 + toSecond(t);
|
|
|
|
|
|
|
|
if (time_offset >= lut[result_day].time_at_offset_change)
|
|
|
|
time_offset -= lut[result_day].amount_of_offset_change;
|
|
|
|
|
|
|
|
return lut[result_day].date + time_offset;
|
|
|
|
}
|
|
|
|
|
2018-05-25 13:29:15 +00:00
|
|
|
inline DayNum addMonths(DayNum d, Int64 delta) const
|
2017-10-29 04:18:48 +00:00
|
|
|
{
|
|
|
|
const Values & values = lut[d];
|
|
|
|
|
2017-12-06 04:16:01 +00:00
|
|
|
Int64 month = static_cast<Int64>(values.month) + delta;
|
|
|
|
|
|
|
|
if (month > 0)
|
|
|
|
{
|
|
|
|
auto year = values.year + (month - 1) / 12;
|
|
|
|
month = ((month - 1) % 12) + 1;
|
|
|
|
auto day_of_month = saturateDayOfMonth(year, month, values.day_of_month);
|
2017-10-29 04:18:48 +00:00
|
|
|
|
2017-12-06 04:16:01 +00:00
|
|
|
return makeDayNum(year, month, day_of_month);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto year = values.year - (12 - month) / 12;
|
|
|
|
month = 12 - (-month % 12);
|
|
|
|
auto day_of_month = saturateDayOfMonth(year, month, values.day_of_month);
|
|
|
|
|
|
|
|
return makeDayNum(year, month, day_of_month);
|
|
|
|
}
|
2017-10-29 04:18:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline time_t addQuarters(time_t t, Int64 delta) const
|
|
|
|
{
|
|
|
|
return addMonths(t, delta * 3);
|
|
|
|
}
|
2018-12-18 13:16:48 +00:00
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
inline DayNum addQuarters(DayNum d, Int64 delta) const
|
|
|
|
{
|
|
|
|
return addMonths(d, delta * 3);
|
|
|
|
}
|
2018-12-18 13:16:48 +00:00
|
|
|
|
2017-10-29 04:18:48 +00:00
|
|
|
/// Saturation can occur if 29 Feb is mapped to non-leap year.
|
2017-10-29 22:38:06 +00:00
|
|
|
inline time_t addYears(time_t t, Int64 delta) const
|
2017-10-29 04:18:48 +00:00
|
|
|
{
|
2018-05-25 13:29:15 +00:00
|
|
|
DayNum result_day = addYears(toDayNum(t), delta);
|
2017-10-29 04:18:48 +00:00
|
|
|
|
|
|
|
time_t time_offset = toHour(t) * 3600 + toMinute(t) * 60 + toSecond(t);
|
|
|
|
|
|
|
|
if (time_offset >= lut[result_day].time_at_offset_change)
|
|
|
|
time_offset -= lut[result_day].amount_of_offset_change;
|
|
|
|
|
|
|
|
return lut[result_day].date + time_offset;
|
|
|
|
}
|
|
|
|
|
2018-05-25 13:29:15 +00:00
|
|
|
inline DayNum addYears(DayNum d, Int64 delta) const
|
2017-10-29 04:18:48 +00:00
|
|
|
{
|
|
|
|
const Values & values = lut[d];
|
|
|
|
|
|
|
|
auto year = values.year + delta;
|
|
|
|
auto month = values.month;
|
|
|
|
auto day_of_month = values.day_of_month;
|
|
|
|
|
|
|
|
/// Saturation to 28 Feb can happen.
|
|
|
|
if (unlikely(day_of_month == 29 && month == 2))
|
|
|
|
day_of_month = saturateDayOfMonth(year, month, day_of_month);
|
|
|
|
|
|
|
|
return makeDayNum(year, month, day_of_month);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
inline std::string timeToString(time_t t) const
|
|
|
|
{
|
|
|
|
const Values & values = find(t);
|
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
std::string s {"0000-00-00 00:00:00"};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
s[0] += values.year / 1000;
|
|
|
|
s[1] += (values.year / 100) % 10;
|
|
|
|
s[2] += (values.year / 10) % 10;
|
|
|
|
s[3] += values.year % 10;
|
|
|
|
s[5] += values.month / 10;
|
|
|
|
s[6] += values.month % 10;
|
|
|
|
s[8] += values.day_of_month / 10;
|
|
|
|
s[9] += values.day_of_month % 10;
|
|
|
|
|
|
|
|
auto hour = toHour(t);
|
2017-10-29 00:51:40 +00:00
|
|
|
auto minute = toMinute(t);
|
|
|
|
auto second = toSecond(t);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
s[11] += hour / 10;
|
|
|
|
s[12] += hour % 10;
|
|
|
|
s[14] += minute / 10;
|
|
|
|
s[15] += minute % 10;
|
|
|
|
s[17] += second / 10;
|
|
|
|
s[18] += second % 10;
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string dateToString(time_t t) const
|
|
|
|
{
|
|
|
|
const Values & values = find(t);
|
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
std::string s {"0000-00-00"};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
s[0] += values.year / 1000;
|
|
|
|
s[1] += (values.year / 100) % 10;
|
|
|
|
s[2] += (values.year / 10) % 10;
|
|
|
|
s[3] += values.year % 10;
|
|
|
|
s[5] += values.month / 10;
|
|
|
|
s[6] += values.month % 10;
|
|
|
|
s[8] += values.day_of_month / 10;
|
|
|
|
s[9] += values.day_of_month % 10;
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-05-25 13:29:15 +00:00
|
|
|
inline std::string dateToString(DayNum d) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-10-29 00:51:40 +00:00
|
|
|
const Values & values = lut[d];
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-06-18 10:08:12 +00:00
|
|
|
std::string s {"0000-00-00"};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
s[0] += values.year / 1000;
|
|
|
|
s[1] += (values.year / 100) % 10;
|
|
|
|
s[2] += (values.year / 10) % 10;
|
|
|
|
s[3] += values.year % 10;
|
|
|
|
s[5] += values.month / 10;
|
|
|
|
s[6] += values.month % 10;
|
|
|
|
s[8] += values.day_of_month / 10;
|
|
|
|
s[9] += values.day_of_month % 10;
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
2015-09-29 19:21:02 +00:00
|
|
|
};
|
2019-01-23 14:18:19 +00:00
|
|
|
|
2019-01-23 15:24:14 +00:00
|
|
|
#if defined(__PPC__)
|
2019-06-18 10:08:12 +00:00
|
|
|
#if !__clang__
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
2019-01-23 14:26:05 +00:00
|
|
|
#endif
|