#ifndef LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_ABS_TIMEOUT_H
#define LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_ABS_TIMEOUT_H
#include "hdr/time_macros.h"
#include "hdr/types/struct_timespec.h"
#include "src/__support/CPP/expected.h"
#include "src/__support/macros/config.h"
#include "src/__support/time/units.h"
namespace LIBC_NAMESPACE_DECL {
namespace internal {
class AbsTimeout {
timespec timeout;
bool realtime_flag;
LIBC_INLINE constexpr explicit AbsTimeout(timespec ts, bool realtime)
: timeout(ts), realtime_flag(realtime) {}
public:
enum class Error { Invalid, BeforeEpoch };
LIBC_INLINE const timespec &get_timespec() const { return timeout; }
LIBC_INLINE bool is_realtime() const { return realtime_flag; }
LIBC_INLINE static constexpr cpp::expected<AbsTimeout, Error>
from_timespec(timespec ts, bool realtime) {
using namespace time_units;
if (ts.tv_nsec < 0 || ts.tv_nsec >= 1_s_ns)
return cpp::unexpected(Error::Invalid);
if (ts.tv_sec < 0)
return cpp::unexpected(Error::BeforeEpoch);
return AbsTimeout{ts, realtime};
}
};
}
}
#endif