Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

我把fmtlog改了一下,可以写普通文件了. #9

Closed
JaydenFish opened this issue Aug 24, 2021 · 1 comment
Closed

我把fmtlog改了一下,可以写普通文件了. #9

JaydenFish opened this issue Aug 24, 2021 · 1 comment

Comments

@JaydenFish
Copy link

写普通文件就不需要TSCNS了.
前端延迟只需要1cycle, 我都惊呆了!

ps: TSCNS应该提炼出来, 再完善一下, 和fmt结合, 提供时间日期格式化功能.

@MengRao
Copy link
Owner

MengRao commented Aug 25, 2021

TSCNS本来就是一个独立的项目:https://github.com/MengRao/tscns
另外用fmt格式化ns可以自己实现:

static const int64_t SEC = 1000000000;
extern int64_t tzoffset_ns;

struct FmtNs
{
  FmtNs(int64_t ns_)
    : ns(ns_) {}
  int64_t ns;
};

template<>
struct fmt::formatter<FmtNs> : fmt::formatter<std::string_view>
{
  template<typename FormatContext>
  auto format(FmtNs ts, FormatContext& ctx) {
    uint64_t t = ts.ns + tzoffset_ns;
    Str<18> str;
    ((Str<9>*)&str[9])->fromi(t % SEC); // ns
    t /= SEC;
    str[8] = '.';
    ((Str<2>*)&str[6])->fromi(t % 60); // sec
    t /= 60;
    str[5] = ':';
    ((Str<2>*)&str[3])->fromi(t % 60); // min
    t /= 60;
    str[2] = ':';
    ((Str<2>*)&str[0])->fromi(t % 24); // hour

    return format_to(ctx.out(), "{}", str.tosv());
  }
};
int64_t ns = tscns.rdns();
fmt::print("ns: {}", FmtNs(ns));

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

No branches or pull requests

2 participants