[clang] [clang] replaced the usage of `asctime` with `strftime` (PR #99075)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 17 07:46:51 PDT 2024


================
@@ -1721,11 +1721,13 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
     Diag(Tok.getLocation(), diag::warn_pp_date_time);
     // MSVC, ICC, GCC, VisualAge C++ extension.  The generated string should be
     // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
-    const char *Result;
+    const char *Result = "??? ??? ?? ??:??:?? ????";
+    char TimeString[64];
     if (getPreprocessorOpts().SourceDateEpoch) {
       time_t TT = *getPreprocessorOpts().SourceDateEpoch;
       std::tm *TM = std::gmtime(&TT);
-      Result = asctime(TM);
+      if (strftime(TimeString, sizeof TimeString, "%a %b %e %T %Y", TM) != 0)
----------------
AaronBallman wrote:

Hmm, so the last remaining problem is that of locale. I didn't realize that `asctime()` is locale independent, which means that switching to `strftime()` will change the output for some users. :-(

We could use `setlocale()` before the call to `strftime()` and then reset the locale after the call, but another approach would be to refactor `ComputeDATE_TIME` which does all the formatting manually and in a locale-independent way. Given what a pain locales are in practice, I sort of think the refactoring is actually a better approach. WDYT @cor3ntin?



https://github.com/llvm/llvm-project/pull/99075


More information about the cfe-commits mailing list