package com.tsi.app.common.utils;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.concurrent.TimeUnit;

public class Timespec {

    protected long tv_sec;
    protected long tv_nsec;

    public Timespec(ByteBuffer buffer) {
        buffer.order(ByteOrder.nativeOrder());
        this.tv_sec = buffer.getLong();
        this.tv_nsec = buffer.getLong();
    }
    public Timespec(long sec, long nsec) {
        this.tv_sec = sec;
        this.tv_nsec = nsec;
    }
    public Timespec() {
        init();
    }

    public void init() {
        this.tv_sec = TimeUnit.SECONDS.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
        this.tv_nsec = System.nanoTime();
    }
    public void tv_nsec(long nsec) {
        this.tv_nsec = nsec;
    }
    public void tv_sec(long sec) {
        this.tv_sec = sec;
    }
    public void set(long nsec, long sec) {
        this.tv_nsec = nsec;
        this.tv_sec = sec;
    }
    public byte[] tv_sec() {
        ByteBuffer buffer = ByteBuffer.allocate(8);
        buffer.putLong(this.tv_sec);
        return buffer.array();
    }
    public byte[] tv_nsec() {
        ByteBuffer buffer = ByteBuffer.allocate(8);
        buffer.putLong(this.tv_nsec);
        return buffer.array();
    }
    public byte[] bytes() {
        ByteBuffer buffer = ByteBuffer.allocate(16);
        buffer.order(ByteOrder.LITTLE_ENDIAN);
        buffer.putLong(this.tv_sec);
        buffer.putLong(this.tv_nsec);
        return buffer.array();
    }
    public long timestamp() {
        return this.tv_nsec;
    }
    public long times() {
        return this.tv_sec;
    }

}
