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

via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 22 11:19:43 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)
----------------
cor3ntin wrote:

@AaronBallman the `std::print` thing was illistrative, this works in C++17 (well, 11)

```cpp
    std::stringstream buf;
    // this is the line that ensure we are not affected by locales 
    buf.imbue(std::locale("C"));
    std::time_t t = std::time(nullptr);
    std::tm tm = *std::localtime(&t);
    buf << std::put_time(&tm, "%c");
    std::string str = buf.str(); // do whatever with that string
```


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


More information about the cfe-commits mailing list