通过mysqlnow()函数校正本地(windows)时间与服务器(linux)时间
2016-08-24 10:26
549 查看
通过连接服务器的mysql服务器,通过select查询记录集,其中记录集的一列是now(),然后就看代码吧
class CInfo
{
public:
time_t m_TimeOffset;
CTime nowtm;// 修正到服务器时间
CTime m_DateBegin;
CTime m_TimeBegin;
CTime m_DateEnd;
CTime m_TimeEnd;
CTimeSpan m_DuringTimeSpan;// 这期间的时间跨度
CString m_strDescription; // 将时间转换成字符串,作为描述信息
int m_uiGroupID;
int m_uiUserID;
int id;
};
CInfo info;
void GetTimeDiffer();
void calcTime();
void main()
{
GetTimeDiffer();
calcTime();
}
void GetTimeDiffer()
{
CMySQL mysql("127.0.0.1", "user", "password", "dbname", 3306);
mydata = mysql.GetConnect();
CString sql1;
CString uname = "1";
std::string str_md5_pwd= calcMD5("1");
// 直接可以在444的mysql上执行
// select UserID, tbl_user_info.GID, GName, now() from tbl_user_info, tbl_group_info where tbl_user_info.GID = tbl_group_info.GID and UserName='1' and Password = 'c4ca4238a0b923820dcc509a6f75849b';
sql1.Format("select UserID, tbl_user_info.GID, GName, now() from tbl_user_info, tbl_group_info where tbl_user_info.GID = tbl_group_info.GID and UserName='%s' and Password = '%s';", uname, str_md5_pwd.c_str());
if (mysql_query(mydata, sql1) != 0)
{
return;
}
MYSQL_RES *result = mysql_store_result(mydata);
MYSQL_ROW currow = NULL;
CString m_strGroupName;
CString m_strUserName;
while ((currow = mysql_fetch_row(result)) != NULL)
{
CString gp = currow[2];
if (gp != "管理组" && gp != "教师组")
{
break;
}
info.m_uiUserID = atoi(currow[0]);
info.m_uiGroupID = atoi(currow[1]);
m_strGroupName = currow[2];
m_strUserName = uname;
// 注意禁止用UNIX_TIMESTAMP从mysql中直接查询秒数,因为那个秒数是相对服务器所设时区的,拿到本地再做CTime处理可能会导致时区偏差
// 因此直接处理来自服务器的字符串格式的时间(未知时区),这样就不用考虑时区问题(我们只看字面时间差),这样就可以完全与服务器时间同步
CString ser_time = currow[3];
COleDateTime tm1;
tm1.ParseDateTime(ser_time);
#if _DEBUG
OutputDebugString(tm1.Format("这是sql语句now()得到的时间:%Y-%m-%d %H:%M:%S\n"));
#endif
SYSTEMTIME st;
tm1.GetAsSystemTime(st);
#if _DEBUG
OutputDebugString(tm1.Format("转换成SYSTEMTIME类型的时间格式:%Y-%m-%d %H:%M:%S\n"));
#endif
CTime server_tm(st);
CTime now_tm = CTime::GetCurrentTime();
#if _DEBUG
OutputDebugString(server_tm.Format("服务器时间:%Y-%m-%d %H:%M:%S\n"));
OutputDebugString(now_tm.Format("本地时间:%Y-%m-%d %H:%M:%S\n"));
#endif
info.m_TimeOffset = now_tm.GetTime() - server_tm.GetTime(); // 记录下本地时间比服务器时间快多少(差值)【秒】
#if _DEBUG
CTime oct=info.m_TimeOffset;
// 用time_t表示的时间(日历时间)是从一个时间点(例如:1970年1月1日0时0分0秒)到此时的秒数
OutputDebugString(oct.Format("本地与服务器之间相差的秒数:%Y-%m-%d %H:%M:%S\n"));
#endif
}
}
void calcTime()
{
// 修正到服务器时间
info.nowtm = CTime::GetCurrentTime();
#if _DEBUG
OutputDebugString(info.nowtm.Format("本地当前时间:%Y-%m-%d %H:%M:%S\n"));
#endif
info.nowtm = info.nowtm.GetTime() - info.m_TimeOffset;
#if _DEBUG
OutputDebugString(info.nowtm.Format("修正后服务器的当前时间:%Y-%m-%d %H:%M:%S\n"));
#endif
info.m_DateBegin = info.nowtm + CTimeSpan(0, 0, 0, 30);
info.m_TimeBegin = info.m_DateBegin;
#if _DEBUG
OutputDebugString(info.m_TimeBegin.Format("开始时间=开始日期:%Y-%m-%d %H:%M:%S\n"));
#endif
CTime ctm(info.m_DateBegin.GetYear(), info.m_DateBegin.GetMonth(), info.m_DateBegin.GetDay(), 0, 0, 0);
CTimeSpan sp(0, info.m_TimeBegin.GetHour(), info.m_TimeBegin.GetMinute(), info.m_TimeBegin.GetSecond());
#if _DEBUG
OutputDebugString(ctm.Format("开始时间:%Y-%m-%d %H:%M:%S\n"));
#endif
ctm += sp;
#if _DEBUG
OutputDebugString(ctm.Format("开始时间+时间段:%Y-%m-%d %H:%M:%S\n"));
#endif
if (ctm < info.nowtm)
{
AfxMessageBox("预计启动时间应至少超过当前时间");
return;
}
else
{
#if _DEBUG
OutputDebugString(ctm.Format("对比时间:%Y-%m-%d %H:%M:%S\n"));
OutputDebugString(info.nowtm.Format("对比时间:%Y-%m-%d %H:%M:%S\n"));
#endif
}
info.m_DateBegin = ctm;
info.m_TimeBegin = ctm;
CTimeSpan m_TimeSpan;
m_TimeSpan = CTimeSpan(0, 1, 0, 0); // 默认1小时
#if _DEBUG
OutputDebugString(m_TimeSpan.Format("TimeSpan时间跨度:%D天%H小时%M分%S秒\n"));
#endif
info.m_DateEnd = info.m_DateBegin + m_TimeSpan;
info.m_TimeEnd = info.m_DateEnd;
#if _DEBUG
OutputDebugString(info.m_TimeEnd.Format("结束日期:%Y-%m-%d %H:%M:%S\n"));
#endif
CTime tm2(info.m_DateEnd.GetYear(), info.m_DateEnd.GetMonth(), info.m_DateEnd.GetDay(), 0, 0, 0);
CTimeSpan sp2(0, info.m_TimeEnd.GetHour(), info.m_TimeEnd.GetMinute(), info.m_TimeEnd.GetSecond());
info.m_DateEnd = tm2 + sp2;
info.m_TimeEnd = info.m_DateEnd;
#if _DEBUG
OutputDebugString(tm2.Format("xxx:%Y-%m-%d %H:%M:%S\n"));
OutputDebugString(info.m_DateEnd.Format("xxx:%Y-%m-%d %H:%M:%S\n"));
OutputDebugString(sp2.Format("xxxTimeSpan时间跨度:%D天%H小时%M分%S秒\n"));
#endif
info.m_DuringTimeSpan = info.m_DateEnd - info.m_DateBegin;
#if _DEBUG
OutputDebugString(info.m_DuringTimeSpan.Format("xxxTimeSpan时间跨度:%D天%H小时%M分%S秒\n"));
#endif
}
class CInfo
{
public:
time_t m_TimeOffset;
CTime nowtm;// 修正到服务器时间
CTime m_DateBegin;
CTime m_TimeBegin;
CTime m_DateEnd;
CTime m_TimeEnd;
CTimeSpan m_DuringTimeSpan;// 这期间的时间跨度
CString m_strDescription; // 将时间转换成字符串,作为描述信息
int m_uiGroupID;
int m_uiUserID;
int id;
};
CInfo info;
void GetTimeDiffer();
void calcTime();
void main()
{
GetTimeDiffer();
calcTime();
}
void GetTimeDiffer()
{
CMySQL mysql("127.0.0.1", "user", "password", "dbname", 3306);
mydata = mysql.GetConnect();
CString sql1;
CString uname = "1";
std::string str_md5_pwd= calcMD5("1");
// 直接可以在444的mysql上执行
// select UserID, tbl_user_info.GID, GName, now() from tbl_user_info, tbl_group_info where tbl_user_info.GID = tbl_group_info.GID and UserName='1' and Password = 'c4ca4238a0b923820dcc509a6f75849b';
sql1.Format("select UserID, tbl_user_info.GID, GName, now() from tbl_user_info, tbl_group_info where tbl_user_info.GID = tbl_group_info.GID and UserName='%s' and Password = '%s';", uname, str_md5_pwd.c_str());
if (mysql_query(mydata, sql1) != 0)
{
return;
}
MYSQL_RES *result = mysql_store_result(mydata);
MYSQL_ROW currow = NULL;
CString m_strGroupName;
CString m_strUserName;
while ((currow = mysql_fetch_row(result)) != NULL)
{
CString gp = currow[2];
if (gp != "管理组" && gp != "教师组")
{
break;
}
info.m_uiUserID = atoi(currow[0]);
info.m_uiGroupID = atoi(currow[1]);
m_strGroupName = currow[2];
m_strUserName = uname;
// 注意禁止用UNIX_TIMESTAMP从mysql中直接查询秒数,因为那个秒数是相对服务器所设时区的,拿到本地再做CTime处理可能会导致时区偏差
// 因此直接处理来自服务器的字符串格式的时间(未知时区),这样就不用考虑时区问题(我们只看字面时间差),这样就可以完全与服务器时间同步
CString ser_time = currow[3];
COleDateTime tm1;
tm1.ParseDateTime(ser_time);
#if _DEBUG
OutputDebugString(tm1.Format("这是sql语句now()得到的时间:%Y-%m-%d %H:%M:%S\n"));
#endif
SYSTEMTIME st;
tm1.GetAsSystemTime(st);
#if _DEBUG
OutputDebugString(tm1.Format("转换成SYSTEMTIME类型的时间格式:%Y-%m-%d %H:%M:%S\n"));
#endif
CTime server_tm(st);
CTime now_tm = CTime::GetCurrentTime();
#if _DEBUG
OutputDebugString(server_tm.Format("服务器时间:%Y-%m-%d %H:%M:%S\n"));
OutputDebugString(now_tm.Format("本地时间:%Y-%m-%d %H:%M:%S\n"));
#endif
info.m_TimeOffset = now_tm.GetTime() - server_tm.GetTime(); // 记录下本地时间比服务器时间快多少(差值)【秒】
#if _DEBUG
CTime oct=info.m_TimeOffset;
// 用time_t表示的时间(日历时间)是从一个时间点(例如:1970年1月1日0时0分0秒)到此时的秒数
OutputDebugString(oct.Format("本地与服务器之间相差的秒数:%Y-%m-%d %H:%M:%S\n"));
#endif
}
}
void calcTime()
{
// 修正到服务器时间
info.nowtm = CTime::GetCurrentTime();
#if _DEBUG
OutputDebugString(info.nowtm.Format("本地当前时间:%Y-%m-%d %H:%M:%S\n"));
#endif
info.nowtm = info.nowtm.GetTime() - info.m_TimeOffset;
#if _DEBUG
OutputDebugString(info.nowtm.Format("修正后服务器的当前时间:%Y-%m-%d %H:%M:%S\n"));
#endif
info.m_DateBegin = info.nowtm + CTimeSpan(0, 0, 0, 30);
info.m_TimeBegin = info.m_DateBegin;
#if _DEBUG
OutputDebugString(info.m_TimeBegin.Format("开始时间=开始日期:%Y-%m-%d %H:%M:%S\n"));
#endif
CTime ctm(info.m_DateBegin.GetYear(), info.m_DateBegin.GetMonth(), info.m_DateBegin.GetDay(), 0, 0, 0);
CTimeSpan sp(0, info.m_TimeBegin.GetHour(), info.m_TimeBegin.GetMinute(), info.m_TimeBegin.GetSecond());
#if _DEBUG
OutputDebugString(ctm.Format("开始时间:%Y-%m-%d %H:%M:%S\n"));
#endif
ctm += sp;
#if _DEBUG
OutputDebugString(ctm.Format("开始时间+时间段:%Y-%m-%d %H:%M:%S\n"));
#endif
if (ctm < info.nowtm)
{
AfxMessageBox("预计启动时间应至少超过当前时间");
return;
}
else
{
#if _DEBUG
OutputDebugString(ctm.Format("对比时间:%Y-%m-%d %H:%M:%S\n"));
OutputDebugString(info.nowtm.Format("对比时间:%Y-%m-%d %H:%M:%S\n"));
#endif
}
info.m_DateBegin = ctm;
info.m_TimeBegin = ctm;
CTimeSpan m_TimeSpan;
m_TimeSpan = CTimeSpan(0, 1, 0, 0); // 默认1小时
#if _DEBUG
OutputDebugString(m_TimeSpan.Format("TimeSpan时间跨度:%D天%H小时%M分%S秒\n"));
#endif
info.m_DateEnd = info.m_DateBegin + m_TimeSpan;
info.m_TimeEnd = info.m_DateEnd;
#if _DEBUG
OutputDebugString(info.m_TimeEnd.Format("结束日期:%Y-%m-%d %H:%M:%S\n"));
#endif
CTime tm2(info.m_DateEnd.GetYear(), info.m_DateEnd.GetMonth(), info.m_DateEnd.GetDay(), 0, 0, 0);
CTimeSpan sp2(0, info.m_TimeEnd.GetHour(), info.m_TimeEnd.GetMinute(), info.m_TimeEnd.GetSecond());
info.m_DateEnd = tm2 + sp2;
info.m_TimeEnd = info.m_DateEnd;
#if _DEBUG
OutputDebugString(tm2.Format("xxx:%Y-%m-%d %H:%M:%S\n"));
OutputDebugString(info.m_DateEnd.Format("xxx:%Y-%m-%d %H:%M:%S\n"));
OutputDebugString(sp2.Format("xxxTimeSpan时间跨度:%D天%H小时%M分%S秒\n"));
#endif
info.m_DuringTimeSpan = info.m_DateEnd - info.m_DateBegin;
#if _DEBUG
OutputDebugString(info.m_DuringTimeSpan.Format("xxxTimeSpan时间跨度:%D天%H小时%M分%S秒\n"));
#endif
}
相关文章推荐
- Linux内网服务器从Windows通过NTP同步时间
- Java通过Ntp服务器获取网络时间修改本地服务器时间
- windows通过putty访问一台linux服务器
- 当Linux服务器的时间不对的时候,可以使用ntpdate工具来校正时间
- windows、linux通过ftp从ftp服务器上传和下载
- ssh如何通过跳板机直接访问到后端服务器(Mac&Linux&Windows解决方案)
- 【原创】JAVA 通过SSH访问Windows(或Linux)服务器
- windows和linux平台下的通用时间测试函数
- 记录在windows下和linux下搭建svn服务器,并通过mac,windows端连接。
- Linux之通过NTP协议实现服务器时间同步的源码
- 用本地linux通过ssh连接远程服务器
- linux上配置subversion服务器端安装配置并使用svn,windows本地检出,设置同步更新服务器的钩子
- 用本地linux通过ssh连接远程服务器
- Windows作为NTP同步时间的服务器时,需要的设置(Linux时间同步)
- 时间函数 与 64位表示 (Windows/Linux)
- linux 服务器时间函数
- linux挂在samba服务器到本地(用于备份文件到nas或者windows的文件服务器)
- [linux]通过ssh远程设定各服务器时间,从而实现集群时间同步