<commit_before>// Copyright 2016 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

#include <iomanip>
#include <limits>
#include <ostream>

namespace cctz {
namespace detail {

// Normalized civil-time fields: Y-M-D HH:MM:SS.
struct fields {
  int y;
  int m;
  int d;
  int hh;
  int mm;
  int ss;
};

struct year_tag {};
struct month_tag {};
struct day_tag {};
struct hour_tag {};
struct minute_tag {};
struct second_tag {};

////////////////////////////////////////////////////////////////////////

// Field normalization.

namespace impl {

// The month lengths in non-leap and leap years respectively.
constexpr signed char k_dpm[2][12] = {
  { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
  { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
};

// The number of days in the 100 years starting in the mod-400 index year,
// stored as a 36524-deficit value (i.e., 0 == 36524, 1 == 36525).
constexpr signed char k_dpC[400] = {
  1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};

// The number of days in the 4 years starting in the mod-400 index year,
// stored as a 1460-deficit value (i.e., 0 == 1460, 1 == 1461).
constexpr signed char k_dp4y[400] = {
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};

constexpr bool is_leap(int y) {
  return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0);
}
constexpr int dpm(int y, int m) {
  return k_dpm[is_leap(y + (m > 2))][m - 1];
}
constexpr int year_index(int y, int m) {
  return (((y + (m > 2)) % 400) + 400) % 400;
}
constexpr int dpC(int y, int m) { return 36524 + k_dpC[year_index(y, m)]; }
constexpr int dp4y(int y, int m) { return 1460 + k_dp4y[year_index(y, m)]; }
constexpr int dpy(int y, int m) { return is_leap(y + (m > 2)) ? 366 : 365; }

constexpr fields n_ymn(int y, int m, int d, int n, int hh, int mm, int ss) {
  return (d <= n)
             ? fields{y, m, d, hh, mm, ss}
             : (m == 12) ? n_ymn(y + 1, 1, d - n, dpm(y + 1, 1), hh, mm, ss)
                         : n_ymn(y, m + 1, d - n, dpm(y, m + 1), hh, mm, ss);
}
constexpr fields n_1yn(int y, int m, int d, int n, int hh, int mm, int ss) {
  return (d > n) ? n_1yn(y + 1, m, d - n, dpy(y + 1, m), hh, mm, ss)
                 : n_ymn(y, m, d, dpm(y, m), hh, mm, ss);
}
constexpr fields n_4yn(int y, int m, int d, int n, int hh, int mm, int ss) {
  return (d > n) ? n_4yn(y + 4, m, d - n, dp4y(y + 4, m), hh, mm, ss)
                 : n_1yn(y, m, d, dpy(y, m), hh, mm, ss);
}

constexpr fields n_Cn(int y, int m, int d, int n, int hh, int mm, int ss) {
  return (d > n) ? n_Cn(y + 100, m, d - n, dpC(y + 100, m), hh, mm, ss)
                 : n_4yn(y, m, d, dp4y(y, m), hh, mm, ss);
}
constexpr fields n_C(int y, int m, int d, int hh, int mm, int ss) {
  return n_Cn(y, m, d, dpC(y, m), hh, mm, ss);
}

constexpr fields n_C4d2(int y, int m, int d, int hh, int mm, int ss) {
  return (d < 0) ? n_C(y - 400, m, d + 146097, hh, mm, ss)
                 : (d == 0) ? n_C(y - 400, m, 146097, hh, mm, ss)
                            : n_C(y, m, d, hh, mm, ss);
}
constexpr fields n_C4d(int y, int m, int d, int c, int hh, int mm, int ss) {
  return n_C4d2(y + (d / 146097) * 400, m, d % 146097 + c, hh, mm, ss);
}
constexpr fields n_C4c2(int y, int m, int d, int c, int hh, int mm, int ss) {
  return (c < 0) ? n_C4d(y - 400, m, d, c + 146097, hh, mm, ss)
                 : n_C4d(y, m, d, c, hh, mm, ss);
}
constexpr fields n_C4c(int y, int m, int d, int c, int hh, int mm, int ss) {
  return n_C4c2(y + (c / 146097) * 400, m, d, c % 146097, hh, mm, ss);
}

constexpr fields n_m_n(int y, int cy, int m, int d, int cd, int hh, int mm,
                       int ss) {
  return (m < 0) ? n_C4c(y + cy - 1, m + 12, d, cd, hh, mm, ss)
                 : (m == 0) ? n_C4c(y + cy - 1, 12, d, cd, hh, mm, ss)
                            : n_C4c(y + cy, m, d, cd, hh, mm, ss);
}
constexpr fields n_m(int y, int m, int d, int hh, int mm, int ss) {
  return n_m_n(y, m / 12, m % 12, d, 0, hh, mm, ss);
}
constexpr fields n_m_c(int y, int m, int d, int c, int hh, int mm, int ss) {
  return n_m_n(y, m / 12, m % 12, d, c, hh, mm, ss);
}

constexpr fields n_hh_n(int y, int m, int d, int c, int hh, int mm, int ss) {
  return (hh < 0) ? n_m_c(y, m, d, c - 1, hh + 24, mm, ss)
                  : n_m_c(y, m, d, c, hh, mm, ss);
}
constexpr fields n_hh(int y, int m, int d, int hh, int mm, int ss) {
  return n_hh_n(y, m, d, hh / 24, hh % 24, mm, ss);
}
constexpr fields n_hh_c2(int y, int m, int d, int c, int hh, int mm, int ss) {
  return n_hh_n(y, m, d, hh / 24 + c, hh % 24, mm, ss);
}
constexpr fields n_hh_c(int y, int m, int d, int hh, int c, int mm, int ss) {
  return n_hh_c2(y, m, d, hh / 24 + c / 24, hh % 24 + c % 24, mm, ss);
}

constexpr fields n_mm_n(int y, int m, int d, int hh, int c, int mm, int ss) {
  return (mm < 0) ? n_hh_c(y, m, d, hh, c - 1, mm + 60, ss)
                  : n_hh_c(y, m, d, hh, c, mm, ss);
}
constexpr fields n_mm(int y, int m, int d, int hh, int mm, int ss) {
  return n_mm_n(y, m, d, hh, mm / 60, mm % 60, ss);
}
constexpr fields n_mm_c2(int y, int m, int d, int hh, int c, int mm, int ss) {
  return n_mm_n(y, m, d, hh, mm / 60 + c, mm % 60, ss);
}
constexpr fields n_mm_c(int y, int m, int d, int hh, int mm, int c, int ss) {
  return n_mm_c2(y, m, d, hh, mm / 60 + c / 60, mm % 60 + c % 60, ss);
}

constexpr fields n_ss_n(int y, int m, int d, int hh, int mm, int c, int ss) {
  return (ss < 0) ? n_mm_c(y, m, d, hh, mm, c - 1, ss + 60)
                  : n_mm_c(y, m, d, hh, mm, c, ss);
}
constexpr fields n_ss(int y, int m, int d, int hh, int mm, int ss) {
  return n_ss_n(y, m, d, hh, mm, ss / 60, ss % 60);
}

}  // namespace impl

////////////////////////////////////////////////////////////////////////

namespace impl {

// Map a (normalized) Y/M/D to the number of days before/after 1970-01-01.
// Will overflow outside of the range [-5877641-06-23 ... 5881580-07-11].
inline constexpr int doy(int m, int d) {
  return (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1;
}
inline constexpr int doe(int yoe, int m, int d) {
  return yoe * 365 + yoe / 4 - yoe / 100 + doy(m, d);
}
inline constexpr int era_eymd_ord(int era, int eyear, int m, int d) {
  return era * 146097 + doe(eyear - era * 400, m, d) - 719468;
}
inline constexpr int eymd_ord(int eyear, int m, int d) {
  return era_eymd_ord((eyear >= 0 ? eyear : eyear - 399) / 400, eyear, m, d);
}
inline constexpr int ymd_ord(int y, int m, int d) {
  return eymd_ord(m <= 2 ? y - 1 : y, m, d);
}

}  // namespace impl

////////////////////////////////////////////////////////////////////////

// Aligns the (normalized) fields struct to the indicated field.
inline constexpr fields align(second_tag, fields f) {
  return f;
}
inline constexpr fields align(minute_tag, fields f) {
  return fields{f.y, f.m, f.d, f.hh, f.mm, 0};
}
inline constexpr fields align(hour_tag, fields f) {
  return fields{f.y, f.m, f.d, f.hh, 0, 0};
}
inline constexpr fields align(day_tag, fields f) {
  return fields{f.y, f.m, f.d, 0, 0, 0};
}
inline constexpr fields align(month_tag, fields f) {
  return fields{f.y, f.m, 1, 0, 0, 0};
}
inline constexpr fields align(year_tag, fields f) {
  return fields{f.y, 1, 1, 0, 0, 0};
}

////////////////////////////////////////////////////////////////////////

// Increments the indicated (normalized) field by "n".
inline constexpr fields step(second_tag, fields f, int n) {
  return impl::n_ss(f.y, f.m, f.d, f.hh, f.mm + n / 60, f.ss + n % 60);
}
inline constexpr fields step(minute_tag, fields f, int n) {
  return impl::n_mm(f.y, f.m, f.d, f.hh + n / 60, f.mm + n % 60, f.ss);
}
inline constexpr fields step(hour_tag, fields f, int n) {
  return impl::n_hh(f.y, f.m, f.d + n / 24, f.hh + n % 24, f.mm, f.ss);
}
inline constexpr fields step(day_tag, fields f, int n) {
  return impl::n_C4c(f.y, f.m, f.d, n, f.hh, f.mm, f.ss);
}
inline constexpr fields step(month_tag, fields f, int n) {
  return impl::n_m(f.y + n / 12, f.m + n % 12, f.d, f.hh, f.mm, f.ss);
}
inline constexpr fields step(year_tag, fields f, int n) {
  return fields{f.y + n, f.m, f.d, f.hh, f.mm, f.ss};
}

////////////////////////////////////////////////////////////////////////

// Returns the difference between fields structs using the indicated unit.
constexpr int difference(year_tag, fields f1, fields f2) {
  return f1.y - f2.y;<commit_msg>Remove the redundant inline from the constexpr functions.<commit_after>// Copyright 2016 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

#include <iomanip>
#include <limits>
#include <ostream>

namespace cctz {
namespace detail {

// Normalized civil-time fields: Y-M-D HH:MM:SS.
struct fields {
  int y;
  int m;
  int d;
  int hh;
  int mm;
  int ss;
};

struct year_tag {};
struct month_tag {};
struct day_tag {};
struct hour_tag {};
struct minute_tag {};
struct second_tag {};

////////////////////////////////////////////////////////////////////////

// Field normalization.

namespace impl {

// The month lengths in non-leap and leap years respectively.
constexpr signed char k_dpm[2][12] = {
  { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
  { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
};

// The number of days in the 100 years starting in the mod-400 index year,
// stored as a 36524-deficit value (i.e., 0 == 36524, 1 == 36525).
constexpr signed char k_dpC[400] = {
  1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};

// The number of days in the 4 years starting in the mod-400 index year,
// stored as a 1460-deficit value (i.e., 0 == 1460, 1 == 1461).
constexpr signed char k_dp4y[400] = {
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};

constexpr bool is_leap(int y) {
  return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0);
}
constexpr int dpm(int y, int m) {
  return k_dpm[is_leap(y + (m > 2))][m - 1];
}
constexpr int year_index(int y, int m) {
  return (((y + (m > 2)) % 400) + 400) % 400;
}
constexpr int dpC(int y, int m) { return 36524 + k_dpC[year_index(y, m)]; }
constexpr int dp4y(int y, int m) { return 1460 + k_dp4y[year_index(y, m)]; }
constexpr int dpy(int y, int m) { return is_leap(y + (m > 2)) ? 366 : 365; }

constexpr fields n_ymn(int y, int m, int d, int n, int hh, int mm, int ss) {
  return (d <= n)
             ? fields{y, m, d, hh, mm, ss}
             : (m == 12) ? n_ymn(y + 1, 1, d - n, dpm(y + 1, 1), hh, mm, ss)
                         : n_ymn(y, m + 1, d - n, dpm(y, m + 1), hh, mm, ss);
}
constexpr fields n_1yn(int y, int m, int d, int n, int hh, int mm, int ss) {
  return (d > n) ? n_1yn(y + 1, m, d - n, dpy(y + 1, m), hh, mm, ss)
                 : n_ymn(y, m, d, dpm(y, m), hh, mm, ss);
}
constexpr fields n_4yn(int y, int m, int d, int n, int hh, int mm, int ss) {
  return (d > n) ? n_4yn(y + 4, m, d - n, dp4y(y + 4, m), hh, mm, ss)
                 : n_1yn(y, m, d, dpy(y, m), hh, mm, ss);
}

constexpr fields n_Cn(int y, int m, int d, int n, int hh, int mm, int ss) {
  return (d > n) ? n_Cn(y + 100, m, d - n, dpC(y + 100, m), hh, mm, ss)
                 : n_4yn(y, m, d, dp4y(y, m), hh, mm, ss);
}
constexpr fields n_C(int y, int m, int d, int hh, int mm, int ss) {
  return n_Cn(y, m, d, dpC(y, m), hh, mm, ss);
}

constexpr fields n_C4d2(int y, int m, int d, int hh, int mm, int ss) {
  return (d < 0) ? n_C(y - 400, m, d + 146097, hh, mm, ss)
                 : (d == 0) ? n_C(y - 400, m, 146097, hh, mm, ss)
                            : n_C(y, m, d, hh, mm, ss);
}
constexpr fields n_C4d(int y, int m, int d, int c, int hh, int mm, int ss) {
  return n_C4d2(y + (d / 146097) * 400, m, d % 146097 + c, hh, mm, ss);
}
constexpr fields n_C4c2(int y, int m, int d, int c, int hh, int mm, int ss) {
  return (c < 0) ? n_C4d(y - 400, m, d, c + 146097, hh, mm, ss)
                 : n_C4d(y, m, d, c, hh, mm, ss);
}
constexpr fields n_C4c(int y, int m, int d, int c, int hh, int mm, int ss) {
  return n_C4c2(y + (c / 146097) * 400, m, d, c % 146097, hh, mm, ss);
}

constexpr fields n_m_n(int y, int cy, int m, int d, int cd, int hh, int mm,
                       int ss) {
  return (m < 0) ? n_C4c(y + cy - 1, m + 12, d, cd, hh, mm, ss)
                 : (m == 0) ? n_C4c(y + cy - 1, 12, d, cd, hh, mm, ss)
                            : n_C4c(y + cy, m, d, cd, hh, mm, ss);
}
constexpr fields n_m(int y, int m, int d, int hh, int mm, int ss) {
  return n_m_n(y, m / 12, m % 12, d, 0, hh, mm, ss);
}
constexpr fields n_m_c(int y, int m, int d, int c, int hh, int mm, int ss) {
  return n_m_n(y, m / 12, m % 12, d, c, hh, mm, ss);
}

constexpr fields n_hh_n(int y, int m, int d, int c, int hh, int mm, int ss) {
  return (hh < 0) ? n_m_c(y, m, d, c - 1, hh + 24, mm, ss)
                  : n_m_c(y, m, d, c, hh, mm, ss);
}
constexpr fields n_hh(int y, int m, int d, int hh, int mm, int ss) {
  return n_hh_n(y, m, d, hh / 24, hh % 24, mm, ss);
}
constexpr fields n_hh_c2(int y, int m, int d, int c, int hh, int mm, int ss) {
  return n_hh_n(y, m, d, hh / 24 + c, hh % 24, mm, ss);
}
constexpr fields n_hh_c(int y, int m, int d, int hh, int c, int mm, int ss) {
  return n_hh_c2(y, m, d, hh / 24 + c / 24, hh % 24 + c % 24, mm, ss);
}

constexpr fields n_mm_n(int y, int m, int d, int hh, int c, int mm, int ss) {
  return (mm < 0) ? n_hh_c(y, m, d, hh, c - 1, mm + 60, ss)
                  : n_hh_c(y, m, d, hh, c, mm, ss);
}
constexpr fields n_mm(int y, int m, int d, int hh, int mm, int ss) {
  return n_mm_n(y, m, d, hh, mm / 60, mm % 60, ss);
}
constexpr fields n_mm_c2(int y, int m, int d, int hh, int c, int mm, int ss) {
  return n_mm_n(y, m, d, hh, mm / 60 + c, mm % 60, ss);
}
constexpr fields n_mm_c(int y, int m, int d, int hh, int mm, int c, int ss) {
  return n_mm_c2(y, m, d, hh, mm / 60 + c / 60, mm % 60 + c % 60, ss);
}

constexpr fields n_ss_n(int y, int m, int d, int hh, int mm, int c, int ss) {
  return (ss < 0) ? n_mm_c(y, m, d, hh, mm, c - 1, ss + 60)
                  : n_mm_c(y, m, d, hh, mm, c, ss);
}
constexpr fields n_ss(int y, int m, int d, int hh, int mm, int ss) {
  return n_ss_n(y, m, d, hh, mm, ss / 60, ss % 60);
}

}  // namespace impl

////////////////////////////////////////////////////////////////////////

namespace impl {

// Map a (normalized) Y/M/D to the number of days before/after 1970-01-01.
// Will overflow outside of the range [-5877641-06-23 ... 5881580-07-11].
constexpr int doy(int m, int d) {
  return (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1;
}
constexpr int doe(int yoe, int m, int d) {
  return yoe * 365 + yoe / 4 - yoe / 100 + doy(m, d);
}
constexpr int era_eymd_ord(int era, int eyear, int m, int d) {
  return era * 146097 + doe(eyear - era * 400, m, d) - 719468;
}
constexpr int eymd_ord(int eyear, int m, int d) {
  return era_eymd_ord((eyear >= 0 ? eyear : eyear - 399) / 400, eyear, m, d);
}
constexpr int ymd_ord(int y, int m, int d) {
  return eymd_ord(m <= 2 ? y - 1 : y, m, d);
}

}  // namespace impl

////////////////////////////////////////////////////////////////////////

// Aligns the (normalized) fields struct to the indicated field.
constexpr fields align(second_tag, fields f) {
  return f;
}
constexpr fields align(minute_tag, fields f) {
  return fields{f.y, f.m, f.d, f.hh, f.mm, 0};
}
constexpr fields align(hour_tag, fields f) {
  return fields{f.y, f.m, f.d, f.hh, 0, 0};
}
constexpr fields align(day_tag, fields f) {
  return fields{f.y, f.m, f.d, 0, 0, 0};
}
constexpr fields align(month_tag, fields f) {
  return fields{f.y, f.m, 1, 0, 0, 0};
}
constexpr fields align(year_tag, fields f) {
  return fields{f.y, 1, 1, 0, 0, 0};
}

////////////////////////////////////////////////////////////////////////

// Increments the indicated (normalized) field by "n".
constexpr fields step(second_tag, fields f, int n) {
  return impl::n_ss(f.y, f.m, f.d, f.hh, f.mm + n / 60, f.ss + n % 60);
}
constexpr fields step(minute_tag, fields f, int n) {
  return impl::n_mm(f.y, f.m, f.d, f.hh + n / 60, f.mm + n % 60, f.ss);
}
constexpr fields step(hour_tag, fields f, int n) {
  return impl::n_hh(f.y, f.m, f.d + n / 24, f.hh + n % 24, f.mm, f.ss);
}
constexpr fields step(day_tag, fields f, int n) {
  return impl::n_C4c(f.y, f.m, f.d, n, f.hh, f.mm, f.ss);
}
constexpr fields step(month_tag, fields f, int n) {
  return impl::n_m(f.y + n / 12, f.m + n % 12, f.d, f.hh, f.mm, f.ss);
}
constexpr fields step(year_tag, fields f, int n) {
  return fields{f.y + n, f.m, f.d, f.hh, f.mm, f.ss};
}

////////////////////////////////////////////////////////////////////////

// Returns the difference between fields structs using the indicated unit.
constexpr int difference(year_tag, fields f1, fields f2) {
  return f1.y - f2.y;
{'fraction_non_alphanumeric': 0.2251064783680789, 'fraction_numerical': 0.1096615108720018, 'mean_word_length': 2.1666666666666665, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4982020', 'n_tokens_mistral': 12710, 'n_tokens_neox': 10464, 'n_words': 4265}