Skip to content

cpp(localtime): cross-platform optimization for printing localtime#125

Merged
wenchy merged 7 commits intomasterfrom
localtime
Aug 25, 2025
Merged

cpp(localtime): cross-platform optimization for printing localtime#125
wenchy merged 7 commits intomasterfrom
localtime

Conversation

@Kybxd
Copy link
Copy Markdown
Collaborator

@Kybxd Kybxd commented Aug 23, 2025

@wenchy wenchy changed the title feat: cross-platform optimization for printing localtime cpp(localtime): cross-platform optimization for printing localtime Aug 23, 2025
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();
using namespace std::chrono;
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.

Bad practice for middle-ware software developing.
DO NOT using namespace to avoid naming conflicting.

See https://google.github.io/styleguide/cppguide.html#Namespaces

Do not use using-directives (e.g., using namespace foo). Do not use inline namespaces.

Copy link
Copy Markdown
Collaborator Author

@Kybxd Kybxd Aug 23, 2025

Choose a reason for hiding this comment

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

use namespace sc = std::chrono; instead for simplification.

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.

This is a bad indirection redefinition, and hurts readability. It's okay to just use std::chrono

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.

reverted

<< 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

@Kybxd Kybxd requested a review from wenchy August 25, 2025 07:20
<< 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.

LGTM

@wenchy wenchy merged commit 60f1b19 into master Aug 25, 2025
@wenchy wenchy deleted the localtime branch August 25, 2025 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cpp(localtime): unsafe function

2 participants