timemanager.h
#pragma once
#ifndef TIMEMANAGER_H_
#define TIMEMANAGER_H_
#include <iostream>
#include <sys/timeb.h>
#include <string>
#include <chrono>
#include <time.h>
#include<mutex>
#ifdef _WIN32
#include<Windows.h>
#else
#include<sys/time.h>
#endif
using namespace std;
namespace TimeManagerTool
{
#define HOUR 60
#define HOUROFSECONDS 3600
#define DAYOFSECONDS 86400
#define HOUROFMILLISECOND 3600000
#define MINOFMILLISECOND 60000
#define BASE 1000
#define YEAROFLEN 4
#define MONOFLEN 6
#define HOUROFLEN 11
#define MINOFLEN 13
class TimeManager {
public:
struct DateTime {
int Millisecond;
int sec;
int min;
int hour;
int day;
int mon;
int year;
int timezone;
};
/**
单例模式,线程安全
*/
static TimeManager* InitInstance();
/**
更新登陆时间戳,以及结构体信息,精度毫秒
*/
void update();
void init();
~TimeManager() {}
public:
/**
得到UTC时间,精度秒
*/
time_t GetCurTimeSec();
/**
得到当前UTC时间,精度毫秒
*/
time_t GetCurTimeMs();
/**
得到当前时区时间,精度秒
*/
time_t GetCurZoneTimeSec();
/**
得到当前时区时间,精度毫秒
*/
time_t GetCurZoneTimeMs();
/**
tm结构体转换为时间戳
@return time_t:精度为秒的时间戳
*/
time_t tmStructToTimeStamp(tm datatotimestamp);
/**
时间戳转换为tm结构体,时间戳精度为秒
@param timestamp:时间戳不可改变
*/
tm TimeStamTotmStruct(const time_t timestamp);
/**
UTC时间转当前系统时区时间
@param qwStandardTimeMS:精度为秒与毫秒
@return time_t:精度为毫秒
*/
time_t UTCtoZoneTime(const time_t qwStandardTimeMS);
/**
当前时区时间转UTC时间
@param qwZoneTimeMS:精度为秒与毫秒
@return time_t:精度为毫秒
*/
time_t ZoneTimeToUTC(const time_t qwZoneTimeMS);
/**
系统所属时区
*/
long GetTimeZone();
/**
得到下一次刷新时间
@param clock:刷新时间,单位为小时的整数
@return time_t:精度为秒
*/
time_t RefreshTime(tm day, int clock);
/**
设置当天更新时间
*/
void setRefreshTime(int clock);
/**
获取今日开始时间,精度为秒
*/
time_t GetTodayBeginTime();
/**
返回当天过刷新时间总秒数
@param timestamp:时间戳不可改变
*/
int secofday(const time_t timestamp, int clock);
/**
获取当日已过的毫秒数,从0点开始
*/
time_t GetDayMS();
/**
得到带时区的结构体
*/
DateTime GetDateTime();
/**
判断现在时间与传入时间是否换天
*/
bool IsNewDay(time_t qwTime, int clock);
/**
判断现在时间与传入时间是否换周
*/
bool IsNewWeek(time_t qwTime);
/**
判断现在时间与传入时间是否换月
*/
bool IsNewM