123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- //
- // RUNetTime.h
- // red_utils
- //
- // Created by chunlin zhang on 2024/6/18.
- //
- #ifndef RUNetTime_h
- #define RUNetTime_h
- #include "cocos2d.h"
- #include "RUDefine.h"
- NS_RU_BEGIN
- class NetTime : public cocos2d::Ref{
- private:
- NetTime();
- ~NetTime();
-
- public:
- /// 获取联网时间的管理单例
- static NetTime* getInstance();
-
- /// 应用程序从后台返回调用,用于同步服务器时间(如果需要使用这个类,请在游戏开始时和从后台返回前台时调用这个函数)
- void enterForeground();
-
- /// 获取当前时间(返回一个时间戳,如果没有和服务器同步时间则返回0)
- int getCurrentTime();
-
- /// 获取当前时间,考虑用户时区
- int getCurrentTimeWithTimeZone();
-
- /// 获取用户设备的本地时间
- int getUserDeviceTime();
-
- public://一些工具函数
- /// 获取所给时间戳所在星期中某一天的某时刻时间戳
- /// @param curTime 需要定位的所在时间戳
- /// @param weekDay 需要定位周几
- /// @param hour 需要定位那一天的几点钟
- int getTimeInThisWeek(int curTime, int weekDay, int hour);
-
- /// 获取所给时间戳所在月份中的某一天某时刻的时间戳
- /// @param curTime 需要定位的所在时间戳
- /// @param day 需要定位的天
- /// @param hour 需要定位那一天的几点钟
- int getTimeInTheMonth(int curTime, int day, int hour);
-
- /// 获取所给时间戳当天某一时刻的时间戳
- int getTimeInCurDay(int curTime, int hour);
- /// 获取所给时间戳下一天某一时刻的时间戳
- int getTimeInNextDay(int curTime, int hour);
-
- /// 获取某个月有多少秒
- int getMonthlySecond(long curTime);
-
- private:
- /// 初始化时间管理类
- void initNetTime();
-
- private://时区相关函数
- /// 记录用户的时区
- void recordUserTimeZoneOffset();
-
- /// 获取用户的时区
- int getUserTimeZoneOffset();
-
- /// 根据时区转换时间函数
- /// @param utcTime 世界标准时间戳
- /// @param timeZoneOffset 相对于标准时间戳的秒数偏移(例如东八区偏移8小时,所以这个值为3600*8 = 28800)
- int convertToUserTimezone(int utcTime, int timeZoneOffset);
-
- private:
- float _timeStamp = 0.0f;//计时时间戳,用来维护同步之后的时间
-
- int _systemUpTimeAnchor = 0;//系统时间锚点,指同步服务器时间时标记的系统时间
- int _netTimeAnchor = 0;//服务器时间锚点,指同步服务器时间时标记的服务器时间戳
- };
- #define SceheduleKey_RUNetTime_Sceheduler "SceheduleKey_RUNetTime_Sceheduler" //用来维护本地计时的计时器键值
- #define SceheduleKey_RUNetTime_checkNetSync "SceheduleKey_RUNetTime_checkNetSync" //同步检查,如果没同步到每隔一段时间重新同步
- #define UserDefaultKey_RUNetTime_TimeZoneRecord "UserDefaultKey_RUNetTime_TimeZoneRecord" //用来保存是否记录了时区偏移
- #define UserDefaultKey_RUNetTime_TimeZoneOffset "UserDefaultKey_RUNetTime_TimeZoneOffset" //用来记录用户时区偏移(单位:秒)的键值
- NS_RU_END
- #endif /* RUNetTime_h */
|