/*
 * SPDX-FileCopyrightText: 2025 Copyright contributors to the cppTango project
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */
#ifndef TANGO_SERVER_TANGOTIMESTAMP_H
#define TANGO_SERVER_TANGOTIMESTAMP_H

#include <tango/common/defines.h>
#include <tango/common/tango_const.h>

#include <chrono>

#ifdef _TG_WINDOWS_
  #include <sys/timeb.h>
  #include <winsock2.h> // struct timeval
#endif

namespace Tango
{

struct TangoTimestamp : public std::chrono::system_clock::time_point
{
    using std::chrono::system_clock::time_point::time_point;

    TangoTimestamp(std::chrono::system_clock::time_point t) :
        std::chrono::system_clock::time_point(t)
    {
    }
#ifdef _TG_WINDOWS_
    TangoTimestamp(struct _timeb tb) :
        time_point(std::chrono::duration_cast<std::chrono::system_clock::duration>(
            std::chrono::seconds(tb.time) + std::chrono::milliseconds(tb.millitm)))
    {
    }
#endif
    TangoTimestamp(struct timeval tv) :
        time_point(std::chrono::duration_cast<std::chrono::system_clock::duration>(
            std::chrono::seconds(tv.tv_sec) + std::chrono::microseconds(tv.tv_usec)))
    {
    }

    TangoTimestamp(TimeVal t) :
        time_point(std::chrono::duration_cast<std::chrono::system_clock::duration>(
            (t.tv_nsec != 0) ? (std::chrono::seconds(t.tv_sec) + std::chrono::nanoseconds(t.tv_nsec))
                             : (std::chrono::seconds(t.tv_sec) + std::chrono::microseconds(t.tv_usec))))
    {
    }

    TangoTimestamp(time_t t) :
        time_point(std::chrono::duration_cast<std::chrono::system_clock::duration>(std::chrono::seconds(t)))
    {
    }
};

} // namespace Tango

#endif
