package com.utic.center.common.utils;

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

public class Timespec {

    protected long tvSec;
    protected long tvNSec;

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

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

}
