package com.xiang.xservice.application.script.fwd.utils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * @Author: xiang
 * @Date: 2025-09-30 14:58
 */
public class TimeSyncUtils {
    private static volatile long offset = 0;

    /**
     * 初始化/更新偏移量
     */

    public static void syncWithServer(long serverTime) {
        long localTime = System.currentTimeMillis();
        offset = serverTime - localTime;
    }

    /**
     * 获取同步后的时间戳
     * @return
     */
    public static long currentTimeMillis() {
        return System.currentTimeMillis() + offset;
    }

    /**
     * 获取同步后的 LocalDateTime
     * @return
     */
    public static LocalDateTime now() {
        return LocalDateTime.ofInstant(
                Instant.ofEpochMilli(currentTimeMillis()),
                ZoneId.systemDefault()
        );
    }
}
