package com.njcn.csharmonic.offline.mincfg;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * Description:
 * Date: 2024/7/15  19:18【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
public class RTC_Timer_MS {
    public short year;
    public short month;
    public short day;
    public short hour;
    public short minute;
    public short second;
    public short millisecond;

    public static int GetSize() {
        return 7 * Short.BYTES;
    }

    public boolean setStructBuf(byte[] bArray, int nHaveRead) {
        int nByteSize = bArray.length;
        if (nByteSize < GetSize()) {
            return false;
        }

        ByteBuffer byteBuffer = ByteBuffer.wrap(bArray).order(ByteOrder.LITTLE_ENDIAN);
        int iStep = nHaveRead;

        year = byteBuffer.getShort(iStep);
        iStep += Short.BYTES;
        month = byteBuffer.getShort(iStep);
        iStep += Short.BYTES;
        day = byteBuffer.getShort(iStep);
        iStep += Short.BYTES;
        hour = byteBuffer.getShort(iStep);
        iStep += Short.BYTES;
        minute = byteBuffer.getShort(iStep);
        iStep += Short.BYTES;
        second = byteBuffer.getShort(iStep);
        iStep += Short.BYTES;
        millisecond = byteBuffer.getShort(iStep);

        return true;
    }

    public int GetStructBuf(byte[] bArray, int nHaveRead) {
        int nSize = bArray.length;
        if (nSize < (nHaveRead + GetSize())) {
            return 0;
        }

        ByteBuffer byteBuffer = ByteBuffer.wrap(bArray).order(ByteOrder.LITTLE_ENDIAN);
        int iStep = nHaveRead;

        byteBuffer.putShort(iStep, year);
        iStep += Short.BYTES;
        byteBuffer.putShort(iStep, month);
        iStep += Short.BYTES;
        byteBuffer.putShort(iStep, day);
        iStep += Short.BYTES;
        byteBuffer.putShort(iStep, hour);
        iStep += Short.BYTES;
        byteBuffer.putShort(iStep, minute);
        iStep += Short.BYTES;
        byteBuffer.putShort(iStep, second);
        iStep += Short.BYTES;
        byteBuffer.putShort(iStep, millisecond);

        return GetSize();
    }
}


