前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >unity3d:时间管理(1):服务器时间同步

unity3d:时间管理(1):服务器时间同步

作者头像
立羽
发布2023-08-24 14:44:19
发布2023-08-24 14:44:19
41200
代码可运行
举报
文章被收录于专栏:Unity3d程序开发Unity3d程序开发
运行总次数:0
代码可运行
代码语言:javascript
代码运行次数:0
复制
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SyncTime : MonoBehaviour {

    public static DateTime UtcNow { get { return DateTime.UtcNow.AddSeconds(DeltaTime); } }
    //public static DateTime Now { get { return DateTime.Now.AddSeconds(DeltaTime); } }

    private static double DeltaTime = 0;
    private static double ServerTime = 0; //服务器现在的时间戳
    private static double ValidStartGameTime = 0; //游戏启动的时间

    //同步服务器时间
    public static void Sync(long time)
    {
        ValidStartGameTime = Time.realtimeSinceStartup;
        
        DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Unspecified).AddSeconds(time);
        DeltaTime = (dt - DateTime.UtcNow).TotalSeconds;
        ServerTime = time;
    }

    //剩余时间
    public static TimeSpan GetLeftTime(long validTime)
    { 
        DateTime date1970 = new DateTime(1970, 1, 1, 0, 0, 0);
        date1970 = date1970.ToLocalTime();
        //TimeSpan ts = date1970.AddSeconds((double)validTime).Subtract(SyncTime.UtcNow);

        TimeSpan ts = date1970.AddSeconds((double)validTime).Subtract(GetSystemTime());
        return ts;
    }

    //当前服务器时间
    public static DateTime GetSystemTime()
    {
        DateTime date1970 = new DateTime(1970, 1, 1, 0, 0, 0);
        date1970 = date1970.ToLocalTime();
        DateTime dateTime = date1970.AddSeconds((double)ServerTime).AddSeconds((double)(Time.realtimeSinceStartup - ValidStartGameTime));
        return dateTime;
    }


    // 时间戳 转换为时间 毫秒
    public static DateTime MilliStampToDateTime(string timeStamp)
    {
        DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
        long mTime = long.Parse(timeStamp + "0000");
        //long mTime = long.Parse(timeStamp);
        TimeSpan toNow = new TimeSpan(mTime);
        //Debug.Log("\n 当前时间为:" + startTime.Add(toNow).ToString("yyyy/MM/dd HH:mm:ss:ffff"));
        Debug.Log("\n 当前时间为:" + startTime.Add(toNow).ToString("yyyy/MM/dd HH:mm:ss"));
        return startTime.Add(toNow);
    }

    // 时间转时间戳  毫秒
    public static string DateTimeToStampMilli(DateTime now)
    {
        DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区
        long timeStamp = (long)(now - startTime).TotalMilliseconds; // 相差毫秒数
        //long timeStamp = (long)(now - startTime).TotalSeconds; // 相差毫秒数
        Debug.Log("\n 当前 时间戳为:" + timeStamp);
        return timeStamp.ToString();
    }

    public static long DateTime2Stamp(DateTime now)
    {
        System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
        long timeStamp = (long)(now - startTime).TotalSeconds; // 相差秒数
        return timeStamp;
    }

    public static DateTime Stamp2DataTime(long stamp)
    {
        System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
        DateTime dt = startTime.AddSeconds(stamp);
        return dt;
    }

    public static long Server2Stamp(string dateTime)
    {
        long ret = 0;
        string[] bufBig = dateTime.Split('T');

        string[] bufDate = bufBig[0].Split('-');

        string[] bufTime = bufBig[1].Split(':');

        string[] second = bufTime[2].Split('.'); 
        DateTime date = new DateTime(int.Parse(bufDate[0]), int.Parse(bufDate[1]), int.Parse(bufDate[2]), int.Parse(bufTime[0]), int.Parse(bufTime[1]), int.Parse(second[0]));

        ret = DateTime2Stamp(date);
        return ret;
    }

}

1.游戏启动,请求到服务器时间 可能各个语言的DateTime 的时间格式对不上,这里就先手动string转C#的datatime

代码语言:javascript
代码运行次数:0
复制
SyncTime.Sync(SyncTime.Server2Stamp(ret.time));

2.当前服务器时间:时间戳格式

代码语言:javascript
代码运行次数:0
复制
long curServerTime = SyncTime.DateTime2Stamp(SyncTime.GetSystemTime());

3.得到当前服务器时间距离截止时间戳的剩余时间

代码语言:javascript
代码运行次数:0
复制
TimeSpan left = SyncTime.GetLeftTime(m_endTime);
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-12-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档