summaryrefslogtreecommitdiff
path: root/discord/snowflake.cpp
blob: 70dee2e335dc481e62d2ab57437bdfd1517e48e5 (plain)
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
#include "snowflake.hpp"
#include <ctime>
#include <iomanip>

Snowflake::Snowflake()
    : m_num(Invalid) {}

Snowflake::Snowflake(uint64_t n)
    : m_num(n) {}

Snowflake::Snowflake(const std::string &str) {
    if (str.size())
        m_num = std::stoull(str);
    else
        m_num = Invalid;
}
Snowflake::Snowflake(const Glib::ustring &str) {
    if (str.size())
        m_num = std::strtoull(str.c_str(), nullptr, 10);
    else
        m_num = Invalid;
};

bool Snowflake::IsValid() const {
    return m_num != Invalid;
}

std::string Snowflake::GetLocalTimestamp() const {
    const time_t secs_since_epoch = (m_num / 4194304000) + 1420070400;
    const std::tm tm = *localtime(&secs_since_epoch);
    std::stringstream ss;
    const static std::locale locale("");
    ss.imbue(locale);
    ss << std::put_time(&tm, "%X %x");
    return ss.str();
}

void from_json(const nlohmann::json &j, Snowflake &s) {
    if (j.is_string()) {
        std::string tmp;
        j.get_to(tmp);
        s.m_num = std::stoull(tmp);
    } else {
        j.get_to(s.m_num);
    }
}

void to_json(nlohmann::json &j, const Snowflake &s) {
    j = std::to_string(s);
}