Modify rule S6229 Add example with locale (CPP-5045)

- Add an example with a local locale
- Add links to documentation
- Correct a few typos
- I did not add an independent example using the global locale but added a comment
This commit is contained in:
Loïc Joly 2024-03-22 18:00:53 +01:00 committed by GitHub
parent ceb93e2399
commit 9608f67c32
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,10 +3,10 @@
The ``++chrono++`` library, introduced in {cpp}20, provides support for calendars, time zones, and i/o formatting and parsing operations on time-related objects.
``++chrono++`` is a better alternative to the C/POSIX functions that operate on ``++time_t++``, ``++tm++``, or ``++timespec++`` types. In comparison to C facilities, it provides a better integration with other components of the {cpp} standard library: (``++iostreams++`` and ``++format++``). Also, it supports compile-time computation and it is thread safe.
``++chrono++`` is a better alternative to the C/POSIX functions that operate on ``++time_t++``, ``++tm++``, or ``++timespec++`` types. In comparison to C facilities, it provides better integration with other components of the {cpp} standard library (``++iostreams++`` and ``++format++``). Also, it supports compile-time computation and it is thread-safe.
This rule raises an issue on any use of C/POSIX functions that can be replaced with one of the ``++std::chrono++`` components:
This rule raises an issue C/POSIX functions that can be replaced with one of the ``++std::chrono++`` components:
* querying for current time (``++time++``, ``++timespec_get++``, ``++clock_gettime++``)
* date to time-point conversion (``++mktime++``, ``++gmtime++``, ``++localtime++``)
@ -42,17 +42,24 @@ std::optional<int> yearOfTimePoint(std::chrono::system_clock::time_point tp) {
return date->tm_year + 1900;
}
std::string toIsoString(std::chrono::system_clock::time_point tp) {
std::string toString(std::chrono::system_clock::time_point tp) {
std::time_t t = std::chrono::system_clock::to_time_t(tp);
std::tm* date = std::gmtime(&t); // Noncompliant
if (!date)
throw InvalidDate();
std::string buffer(100, ' ');
std::size_t written = std::strftime(buffer.data(), buffer.size(), "%F", date);
std::size_t written = std::strftime(&buffer[0], buffer.size(), "%A %c", date);
buffer.resize(written);
return buffer;
}
std::string toFrenchString(std::chrono::system_clock::time_point tp) {
auto oldLocale = std::locale::global(std::locale("fr_FR.UTF-8"));
std::string result = toString(tp);
std::locale::global(oldLocale);
return result;
}
----
=== Compliant solution
@ -78,8 +85,19 @@ std::optional<std::chrono::year> yearOfTimePoint(std::chrono::system_clock::time
return date.year();
}
std::string toIsoString(std::chrono::system_clock::time_point tp) {
return std::format("{:%F}", tp);
std::string toString(std::chrono::system_clock::time_point tp) {
return std::format("{:%A %c}", tp); // Or "{:L%A %c}" if you want to use the global locale
}
std::string toFrenchString(std::chrono::system_clock::time_point tp) {
return std::format(std::locale("fr_FR.UTF-8"), "{:L%A %c}", tp);
}
----
== Resources
=== Documentation
* {cpp} reference - https://en.cppreference.com/w/cpp/chrono[Date and time utilities]
* {cpp} reference - https://en.cppreference.com/w/cpp/chrono/system_clock/formatter#Format_specification[Formatting `std::chrono::sys_time`]