Skip to content

Commit

Permalink
Use predefined names and formats for C-locale
Browse files Browse the repository at this point in the history
  • Loading branch information
phprus committed Oct 20, 2021
1 parent 7fa072c commit 1d04758
Showing 1 changed file with 92 additions and 9 deletions.
101 changes: 92 additions & 9 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -1396,7 +1396,30 @@ template <typename OutputIt, typename Char> class tm_writer {
private:
static constexpr int days_per_week = 7;

static constexpr const Char day_of_week[][10] = {
{'S', 'u', 'n', 'd', 'a', 'y', '\0', '\0', '\0', '\0'},
{'M', 'o', 'n', 'd', 'a', 'y', '\0', '\0', '\0', '\0'},
{'T', 'u', 'e', 's', 'd', 'a', 'y', '\0', '\0', '\0'},
{'W', 'e', 'd', 'n', 'e', 's', 'd', 'a', 'y', '\0'},
{'T', 'h', 'u', 'r', 's', 'd', 'a', 'y', '\0', '\0'},
{'F', 'r', 'i', 'd', 'a', 'y', '\0', '\0', '\0', '\0'},
{'S', 'a', 't', 'u', 'r', 'd', 'a', 'y', '\0', '\0'}};
static constexpr const Char month[][10] = {
{'J', 'a', 'n', 'u', 'a', 'r', 'y', '\0', '\0', '\0'},
{'F', 'e', 'b', 'r', 'u', 'a', 'r', 'y', '\0', '\0'},
{'M', 'a', 'r', 'c', 'h', '\0', '\0', '\0', '\0', '\0'},
{'A', 'p', 'r', 'i', 'l', '\0', '\0', '\0', '\0', '\0'},
{'M', 'a', 'y', '\0', '\0', '\0', '\0', '\0', '\0', '\0'},
{'J', 'u', 'n', 'e', '\0', '\0', '\0', '\0', '\0', '\0'},
{'J', 'u', 'l', 'y', '\0', '\0', '\0', '\0', '\0', '\0'},
{'A', 'u', 'g', 'u', 's', 't', '\0', '\0', '\0', '\0'},
{'S', 'e', 'p', 't', 'e', 'm', 'b', 'e', 'r', '\0'},
{'O', 'c', 't', 'o', 'b', 'e', 'r', '\0', '\0', '\0'},
{'N', 'o', 'v', 'e', 'm', 'b', 'e', 'r', '\0', '\0'},
{'D', 'e', 'c', 'e', 'm', 'b', 'e', 'r', '\0', '\0'}};

const std::locale& loc_;
const bool is_classic_;
OutputIt out_;
const std::tm& tm_;

Expand Down Expand Up @@ -1482,16 +1505,33 @@ template <typename OutputIt, typename Char> class tm_writer {

public:
explicit tm_writer(const std::locale& loc, OutputIt out, const std::tm& tm)
: loc_(loc), out_(out), tm_(tm) {}
: loc_(loc),
is_classic_(loc_ == std::locale::classic()),
out_(out),
tm_(tm) {}

OutputIt out() const { return out_; }

FMT_CONSTEXPR void on_text(const Char* begin, const Char* end) {
out_ = copy_str<Char>(begin, end, out_);
}

void on_abbr_weekday() { format_localized('a'); }
void on_full_weekday() { format_localized('A'); }
void on_abbr_weekday() {
FMT_ASSERT(tm_.tm_wday >= 0 && tm_.tm_wday < 7,
"tm_wday not in range [0, 6]");
if (is_classic_)
out_ = write(out_, basic_string_view<Char>(day_of_week[tm_.tm_wday], 3));
else
format_localized('a');
}
void on_full_weekday() {
FMT_ASSERT(tm_.tm_wday >= 0 && tm_.tm_wday < 7,
"tm_wday not in range [0, 6]");
if (is_classic_)
out_ = write(out_, day_of_week[tm_.tm_wday]);
else
format_localized('A');
}
void on_dec0_weekday(numeric_system ns) {
if (ns != numeric_system::standard) return format_localized('w', 'O');
write1(tm_.tm_wday);
Expand All @@ -1501,17 +1541,48 @@ template <typename OutputIt, typename Char> class tm_writer {
write1(tm_.tm_wday == 0 ? days_per_week : tm_.tm_wday);
}

void on_abbr_month() { format_localized('b'); }
void on_full_month() { format_localized('B'); }
void on_abbr_month() {
FMT_ASSERT(tm_.tm_mon >= 0 && tm_.tm_mon < 12,
"tm_mon not in range [0, 11]");
if (is_classic_)
out_ = write(out_, basic_string_view<Char>(month[tm_.tm_mon], 3));
else
format_localized('b');
}
void on_full_month() {
FMT_ASSERT(tm_.tm_mon >= 0 && tm_.tm_mon < 12,
"tm_mon not in range [0, 11]");
if (is_classic_)
out_ = write(out_, month[tm_.tm_mon]);
else
format_localized('B');
}

void on_datetime(numeric_system ns) {
format_localized('c', ns == numeric_system::standard ? '\0' : 'E');
if (is_classic_) {
on_abbr_weekday();
*out_++ = ' ';
on_abbr_month();
*out_++ = ' ';
on_day_of_month_space(numeric_system::standard);
*out_++ = ' ';
on_iso_time();
*out_++ = ' ';
on_year(numeric_system::standard);
} else
format_localized('c', ns == numeric_system::standard ? '\0' : 'E');
}
void on_loc_date(numeric_system ns) {
format_localized('x', ns == numeric_system::standard ? '\0' : 'E');
if (is_classic_)
on_us_date();
else
format_localized('x', ns == numeric_system::standard ? '\0' : 'E');
}
void on_loc_time(numeric_system ns) {
format_localized('X', ns == numeric_system::standard ? '\0' : 'E');
if (is_classic_)
on_iso_time();
else
format_localized('X', ns == numeric_system::standard ? '\0' : 'E');
}
void on_us_date() {
char buf[8];
Expand Down Expand Up @@ -1636,13 +1707,25 @@ template <typename OutputIt, typename Char> class tm_writer {
out_ = copy_str<Char>(std::begin(buf), std::end(buf), out_);
}

void on_am_pm() { format_localized('p'); }
void on_am_pm() {
if (is_classic_) {
*out_++ = tm_.tm_hour < 12 ? 'A' : 'P';
*out_++ = 'M';
} else
format_localized('p');
}

// These apply to chrono durations but not tm.
void on_duration_value() {}
void on_duration_unit() {}
};

template <typename OutputIt, typename Char>
constexpr const Char tm_writer<OutputIt, Char>::day_of_week[][10];

template <typename OutputIt, typename Char>
constexpr const Char tm_writer<OutputIt, Char>::month[][10];

FMT_END_DETAIL_NAMESPACE

template <typename Char, typename Duration>
Expand Down

0 comments on commit 1d04758

Please sign in to comment.