#include <time.h>
#include <stdio.h>
#include <stdlib.h>

#include "snowflake.h"

int main(int argc, char **argv)
{
    if (argc != 2)
    {
        printf("Usage: %s <id>\n", argv[0]);
        return -1;
    }

    uint64_t id = atoll(argv[1]);
    struct snowflake_meta meta = {};
    snowflake_deserialize(id, &meta);

    printf("id              : %lu\n", id);
    printf("base_id         : %u\n", meta.worker_base_id);
    printf("offset_id       : %u\n", meta.worker_offset_id);
    printf("device_id       : %u\n", meta.device_id);
    printf("thread_id       : %u\n", meta.thread_id);
    printf("time_cycle      : %lu sec (%lu year)\n", meta.time_cycle, meta.time_cycle / 3600 / 24 / 365);
    printf("time_relative   : %lu sec\n", meta.time_relative);
    printf("sequence        : %lu\n", meta.sequence);

    for (int i = 6; i < 10; i++)
    {
        char buff[64];
        time_t tt = meta.time_cycle * i + meta.time_relative;
        struct tm *tm = localtime(&tt);
        strftime(buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", tm);
        printf("\ttime_cycle * %d + time_relative => %s\n", i, buff);
    }

    return 0;
}