aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/core/tablet_flat/util_fmt_basic.h
blob: e5c790342a2885952d32b14a77665803efad7b83 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#pragma once

#include "util_fmt_desc.h"
#include <util/datetime/base.h>

namespace NKikimr {
namespace NFmt {

    struct TDelay {
        TDelay(TDuration val): Val(val) { }

        TOut& Do(TOut &out) const
        {
            const ui64 grid[] =  {
                    60ull,          /* minute                   */
                    3600ull,        /* hour                     */
                    86400ull,       /* day                      */
                    604800ull,      /* week                     */
                    8553600ull,     /* 99 days                  */
                    31557600ull,    /* Julian 365.25 days year  */
                    3155760000ull,  /* Julian 100 years century */
            };

            if (Val == TDuration::Max()) return out << "undef~";

            const auto secs = Val.Seconds();

            if (secs < grid[0]) return Small(out, secs);
            if (secs < grid[1]) return Large(out, secs, 'm', grid[0]);
            if (secs < grid[2]) return Large(out, secs, 'h', grid[1]);
            if (secs < grid[4]) return Large(out, secs, 'd', grid[2]);
            if (secs < grid[5]) return Large(out, secs, 'w', grid[3]);
            if (secs < grid[6]) return Large(out, secs, 'y', grid[5]);

            return Large(out, secs, 'c', grid[6]);
        }

    protected:
        inline TOut& Small(TOut &out, ui64 secs) const
        {
            char ln_[8];

            auto ms = Val.MilliSeconds() - secs * 1000;

            snprintf(ln_, sizeof(ln_), "%zu.%.03zu", secs, ms);

            return ln_[5] = 's', out.Write(ln_, 6), out;
        }

        TOut& Large(TOut &out, double sec, char suff, ui64 base) const
        {
            char ln_[16];

            snprintf(ln_, sizeof(ln_), "%.03f", sec / base);

            return ln_[5] = suff, out.Write(ln_, 6), out;
        }

        TDuration Val;
    };

    inline TOut& operator<<(TOut &out, const NFmt::TDelay &print)
    {
        return print.Do(out);
    }

}
}