aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Parsers/Kusto/Formatters.cpp
blob: f12af47944507e2498c58a67eae960b7f6566531 (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
#include "Formatters.h"

#include <format>

namespace DB
{
std::string formatKQLTimespan(const Int64 ticks)
{
    static constexpr Int64 TICKS_PER_SECOND = 10000000;
    static constexpr auto TICKS_PER_MINUTE = TICKS_PER_SECOND * 60;
    static constexpr auto TICKS_PER_HOUR = TICKS_PER_MINUTE * 60;
    static constexpr auto TICKS_PER_DAY = TICKS_PER_HOUR * 24;

    const auto abs_ticks = std::abs(ticks);
    std::string result = ticks < 0 ? "-" : "";
    if (abs_ticks >= TICKS_PER_DAY)
        result.append(std::format("{}.", abs_ticks / TICKS_PER_DAY));

    result.append(std::format(
        "{:02}:{:02}:{:02}", (abs_ticks / TICKS_PER_HOUR) % 24, (abs_ticks / TICKS_PER_MINUTE) % 60, (abs_ticks / TICKS_PER_SECOND) % 60));

    if (const auto fractional_second = abs_ticks % TICKS_PER_SECOND)
        result.append(std::format(".{:07}", fractional_second));

    return result;
}
}