Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ The official config loader for [Tableau](https://github.com/tableauio/tableau).
- C++17: `cmake -S . -B build`
- C++20: `cmake -S . -B build -DCMAKE_CXX_STANDARD=20`
- clang: `cmake -S . -B build -DCMAKE_CXX_COMPILER=clang++`
- Build: `cmake --build build --parallel 10`
- Build: `cmake --build build -j16`
- Run: `./bin/loader`

### Dev at Windows
Expand All @@ -36,7 +36,7 @@ The official config loader for [Tableau](https://github.com/tableauio/tableau).
- CMake:
- C++17: `cmake -S . -B build -G "NMake Makefiles"`
- C++20: `cmake -S . -B build -G "NMake Makefiles" -DCMAKE_CXX_STANDARD=20`
- Build: `cmake --build build --parallel 10`
- Build: `cmake --build build`
- Run: `.\bin\loader.exe`

### References
Expand Down
20 changes: 16 additions & 4 deletions cmd/protoc-gen-cpp-tableau-loader/embed/logger.pc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,22 @@ void DefaultWrite(std::ostream* os, const SourceLocation& loc, const LevelInfo&

std::ostream& operator<<(std::ostream& os, const Now&) {
auto now = std::chrono::system_clock::now();
auto now_time_t = std::chrono::system_clock::to_time_t(now);
auto now_us = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()) % 1000000;
return os << std::put_time(std::localtime(&now_time_t), "%F %T") << "." << std::setw(6) << std::setfill('0')
<< now_us.count();
#if __cplusplus >= 202002L
auto zt = std::chrono::zoned_time(std::chrono::current_zone(), std::chrono::floor<std::chrono::microseconds>(now));
return os << std::format("{:%F %T}", zt);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also should shows 6 decimal places (microseconds):

    std::cout << chrono::format("{:%F %T}", zt); // prints up to seconds, no fraction
    
    // To print fractional seconds:
    // The standard chrono format supports floating-point seconds with `%S` by specifying precision:
    std::cout << chrono::format("{:%F %H:%M:%S.%6f}", zt); // shows 6 decimal places (microseconds)

Copy link
Copy Markdown
Collaborator Author

@Kybxd Kybxd Aug 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decimals are added by default when printing std::chrono::zoned_time by "%S" with an accuracy of nanoseconds, so std::chrono::floor is used here to limit it to an accuracy of microseconds

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test result:
企业微信截图_1756106376500

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

#else
static thread_local std::tm tm;
auto now_t = std::chrono::system_clock::to_time_t(now);
#ifdef _WIN32
localtime_s(&tm, &now_t);
#else
localtime_r(&now_t, &tm);
#endif
auto duration = now.time_since_epoch();
auto secs = std::chrono::duration_cast<std::chrono::seconds>(duration);
auto micros = std::chrono::duration_cast<std::chrono::microseconds>(duration - secs);
return os << std::put_time(&tm, "%F %T") << "." << std::setw(6) << std::setfill('0') << micros.count();
#endif
}

} // namespace log
Expand Down
2 changes: 1 addition & 1 deletion init.bat
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ cmake -S . -B build ^
-DCMAKE_POLICY_VERSION_MINIMUM="3.5"

REM Compile the code
cmake --build build --parallel 10
cmake --build build

endlocal
2 changes: 1 addition & 1 deletion init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ cmake -S . -B build \
-DCMAKE_BUILD_TYPE=Debug

# Compile the code
cmake --build build --parallel 10
cmake --build build -j16
20 changes: 16 additions & 4 deletions test/cpp-tableau-loader/src/protoconf/logger.pc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,22 @@ void DefaultWrite(std::ostream* os, const SourceLocation& loc, const LevelInfo&

std::ostream& operator<<(std::ostream& os, const Now&) {
auto now = std::chrono::system_clock::now();
auto now_time_t = std::chrono::system_clock::to_time_t(now);
auto now_us = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()) % 1000000;
return os << std::put_time(std::localtime(&now_time_t), "%F %T") << "." << std::setw(6) << std::setfill('0')
<< now_us.count();
#if __cplusplus >= 202002L
auto zt = std::chrono::zoned_time(std::chrono::current_zone(), std::chrono::floor<std::chrono::microseconds>(now));
return os << std::format("{:%F %T}", zt);
#else
static thread_local std::tm tm;
auto now_t = std::chrono::system_clock::to_time_t(now);
#ifdef _WIN32
localtime_s(&tm, &now_t);
#else
localtime_r(&now_t, &tm);
#endif
auto duration = now.time_since_epoch();
auto secs = std::chrono::duration_cast<std::chrono::seconds>(duration);
auto micros = std::chrono::duration_cast<std::chrono::microseconds>(duration - secs);
return os << std::put_time(&tm, "%F %T") << "." << std::setw(6) << std::setfill('0') << micros.count();
#endif
}

} // namespace log
Expand Down