Repository: fmtlib/fmt
File: include/fmt/chrono.h (C/C++)

Code before:
  }
  auto tm_yday() const noexcept -> unsigned {
    FMT_ASSERT(tm_.tm_yday >= 0 && tm_.tm_yday <= 365,
               "tm_yday not in range [0, 365]");
    return to_unsigned(tm_.tm_yday);
  }

  auto tm_hour12() const noexcept -> unsigned {
    auto hour = tm_hour() % 12;
    return hour == 0 ? 12 : hour;
  }

  FMT_CONSTEXPR const Char* tm_day_of_week_name() const noexcept {
    auto wday = tm_wday();
    return (wday >= 0 && wday <= 6) ? day_of_week[wday] : invalid;
  }
  FMT_CONSTEXPR const Char* tm_mon_name() const noexcept {
    auto mon = tm_mon();
    return (mon >= 0 && mon <= 11) ? month[mon] : invalid;
  }

  // POSIX and the C Standard are unclear or inconsistent about what %C and %y
  // do if the year is negative or exceeds 9999. Use the convention that %C
  // concatenated with %y yields the same output as %Y, and that %Y contains at
  // least 4 characters, with more only if necessary.
  auto split_year_lower(const long long year) const noexcept -> unsigned {
    auto l = year % 100;
    if (l < 0) l = -l;  // l in [0, 99]
    return static_cast<unsigned>(l);
  }

  // Algorithm:
  // https://en.wikipedia.org/wiki/ISO_week_date#Calculating_the_week_number_from_a_month_and_day_of_the_month_or_ordinal_date
  auto iso_year_weeks(const long long curr_year) const noexcept -> unsigned {
    const long long prev_year = curr_year - 1;
    const unsigned curr_p = static_cast<unsigned>(
        (curr_year + curr_year / 4 - curr_year / 100 + curr_year / 400) %
        days_per_week);
    const unsigned prev_p = static_cast<unsigned>(
        (prev_year + prev_year / 4 - prev_year / 100 + prev_year / 400) %
        days_per_week);
    return 52u + ((curr_p == 4 || prev_p == 3) ? 1u : 0u);
  }
  auto iso_week_num(unsigned tm_yday, unsigned tm_wday) const noexcept
      -> unsigned {
    return (tm_yday + 11 - (tm_wday == 0 ? days_per_week : tm_wday)) /
           days_per_week;
  }
  auto tm_iso_week_year() const noexcept -> long long {
    const auto year = tm_year();
    const unsigned w = iso_week_num(tm_yday(), tm_wday());

Review comment (suggestion):
I don't think `const` adds anything useful here, just makes code more verbose. Let's remove and keep the return type as `int`.

Code after:

  auto tm_hour12() const noexcept -> int {
    const auto h = tm_hour();
    const auto z = h < 12 ? h : h - 12;
    return z == 0 ? 12 : z;
  }

  // POSIX and the C Standard are unclear or inconsistent about what %C and %y
  // do if the year is negative or exceeds 9999. Use the convention that %C
  // concatenated with %y yields the same output as %Y, and that %Y contains at
  // least 4 characters, with more only if necessary.
  auto split_year_lower(long long year) const noexcept -> int {
    auto l = year % 100;
    if (l < 0) l = -l;  // l in [0, 99]
    return static_cast<int>(l);
  }

  // Algorithm:
  // https://en.wikipedia.org/wiki/ISO_week_date#Calculating_the_week_number_from_a_month_and_day_of_the_month_or_ordinal_date
  auto iso_year_weeks(long long curr_year) const noexcept -> int {
    const auto prev_year = curr_year - 1;
    const auto curr_p =
        (curr_year + curr_year / 4 - curr_year / 100 + curr_year / 400) %
        days_per_week;
    const auto prev_p =
        (prev_year + prev_year / 4 - prev_year / 100 + prev_year / 400) %
        days_per_week;
    return 52 + ((curr_p == 4 || prev_p == 3) ? 1 : 0);
  }
  auto iso_week_num(int tm_yday, int tm_wday) const noexcept -> int {
    return (tm_yday + 11 - (tm_wday == 0 ? days_per_week : tm_wday)) /
           days_per_week;
  }
  auto tm_iso_week_year() const noexcept -> long long {
    const auto year = tm_year();
    const auto w = iso_week_num(tm_yday(), tm_wday());
    if (w < 1) return year - 1;
    if (w > iso_year_weeks(year)) return year + 1;
    return year;
  }
  auto tm_iso_week_of_year() const noexcept -> int {
    const auto year = tm_year();
    const auto w = iso_week_num(tm_yday(), tm_wday());
    if (w < 1) return iso_year_weeks(year - 1);
    if (w > iso_year_weeks(year)) return 1;
    return w;
  }

  void write1(int value) {
    *out_++ = static_cast<char>('0' + to_unsigned(value) % 10);
  }