通过连接服务器的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 _DEBUGOutputDebugString(tm1.Format("这是sql语句now()得到的时间:%Y-%m-%d %H:%M:%S\n"));
#endifSYSTEMTIME st;tm1.GetAsSystemTime(st);
#if _DEBUGOutputDebugString(tm1.Format("转换成SYSTEMTIME类型的时间格式:%Y-%m-%d %H:%M:%S\n"));
#endifCTime server_tm(st);CTime now_tm = CTime::GetCurrentTime();
#if _DEBUGOutputDebugString(server_tm.Format("服务器时间:%Y-%m-%d %H:%M:%S\n"));OutputDebugString(now_tm.Format("本地时间:%Y-%m-%d %H:%M:%S\n"));
#endifinfo.m_TimeOffset = now_tm.GetTime() - server_tm.GetTime(); // 记录下本地时间比服务器时间快多少(差值)【秒】
#if _DEBUGCTime 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 _DEBUGOutputDebugString(info.nowtm.Format("本地当前时间:%Y-%m-%d %H:%M:%S\n"));
#endifinfo.nowtm = info.nowtm.GetTime() - info.m_TimeOffset;
#if _DEBUGOutputDebugString(info.nowtm.Format("修正后服务器的当前时间:%Y-%m-%d %H:%M:%S\n"));
#endifinfo.m_DateBegin = info.nowtm + CTimeSpan(0, 0, 0, 30);info.m_TimeBegin = info.m_DateBegin;
#if _DEBUGOutputDebugString(info.m_TimeBegin.Format("开始时间=开始日期:%Y-%m-%d %H:%M:%S\n"));
#endifCTime 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 _DEBUGOutputDebugString(ctm.Format("开始时间:%Y-%m-%d %H:%M:%S\n"));
#endifctm += sp;
#if _DEBUGOutputDebugString(ctm.Format("开始时间+时间段:%Y-%m-%d %H:%M:%S\n"));
#endifif (ctm < info.nowtm){AfxMessageBox("预计启动时间应至少超过当前时间");return;}else{
#if _DEBUGOutputDebugString(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 _DEBUGOutputDebugString(m_TimeSpan.Format("TimeSpan时间跨度:%D天%H小时%M分%S秒\n"));
#endifinfo.m_DateEnd = info.m_DateBegin + m_TimeSpan;info.m_TimeEnd = info.m_DateEnd;
#if _DEBUGOutputDebugString(info.m_TimeEnd.Format("结束日期:%Y-%m-%d %H:%M:%S\n"));
#endifCTime 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 _DEBUGOutputDebugString(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"));
#endifinfo.m_DuringTimeSpan = info.m_DateEnd - info.m_DateBegin;
#if _DEBUGOutputDebugString(info.m_DuringTimeSpan.Format("xxxTimeSpan时间跨度:%D天%H小时%M分%S秒\n"));
#endif
}

通过mysqlnow()函数校正本地(windows)时间与服务器(linux)时间相关推荐

  1. 华为服务器系统时间调整,服务器系统时间怎么修改

    服务器系统时间怎么修改 内容精选 换一换 华为云帮助中心,为用户提供产品简介.价格说明.购买指南.用户指南.API参考.最佳实践.常见问题.视频帮助等技术文档,帮助您快速上手使用华为云服务. 云耀云服 ...

  2. 计算机网络时间不一致,您的计算机的本地时间与服务器的时间不一致怎么办

    计算机时钟与 Internet 时间服务器同步不了的解决步骤: (小窍门:将日期和时间调整为接近当前,将可以避免电脑时间不能同步或进行同步时出错的问题) (在开始下面的设置之前,建议将"自动 ...

  3. linux 设置时间为昨天,Linux —— 时间(tzselect、timedatactl命令,查看和修改时区,修改时区为东八区)...

    一.设置linux修改时区 首先查看当前时区 #date -R 显示结果为: [root@localhost 20190528]# date -R Wed, 29 May 2019 19:56:23 ...

  4. linux时间子系统之,linux时间子系统(四)

    2.3 系统调用 timekeeper提供一系列的系统调用,使得用户空间可以获取想要的时间.下面简单的介绍一下clock_gettime系统调用 SYSCALL_DEFINE2(clock_getti ...

  5. Linux时间矫正流程,Linux 时间矫正命令

    c++虚函数调用及使用 #include using namespace std; class A { public: ; }; class B: public A { public: ; ... V ...

  6. PHP显示的时间与服务器上时间不同

    在写代码的时候有时候需要用到时间,明明php.ini中已经设置date.timezone = PRC ,但是显示的时间还是与服务器上不同,这时只需要在代码中添加一行代码即可: date_default ...

  7. Linux时间函数札记

    关于gmtime.gmtime_r.localtime.localtime_r 测试环境:vmware 7 + Redhat5.5,系统时间使用UTC,时区为上海. 1.函数功能介绍 使用man gm ...

  8. js取服务器系统时间,JS中new Date()用法及获取服务器时间

    1.获取服务器时间: var now = new Date($.ajax({async: false}).getResponseHeader("Date")); 2.new Dat ...

  9. 【Linux学习笔记】一、Linux 时间问题

    Linux时间问题 理解Linux时间问题 1.Windows 和Linux对时间理解的不同 Windows和 Ubuntu它们在默认情况下看待硬件时间的方式不一样 *Windows把系统BOIS时间 ...

最新文章

  1. python怎么学最快-怎么快速自学python
  2. 如何基于 String 实现同步锁?
  3. 【白话机器学习】算法理论+实战之K-Means聚类算法
  4. 数据结构和算法之排序五:选择排序
  5. 前端:屏蔽F12审查元素,禁止修改页面代码
  6. WORD关于论文格式设置
  7. Ubuntu系统下ntp服务器搭建2
  8. 两路共享LSTM时序数据预测实战+界面可视化应用
  9. python语言由psf组织所有、这是一个商业组织_智慧职教云课堂APP店长实务答案搜题公众号...
  10. JS格式化中国标准时间
  11. 二、T100库存杂收、杂发、报废管理篇
  12. S3C2440的裸奔--内存篇(非MMU)--------转的网友 泠瑛 的博客文章在此谢过!
  13. MC/DC覆盖的计算总结
  14. 现代C++之手写智能指针
  15. 文琼书斋 v3.1 绿色
  16. BJOI2019 奥术神杖
  17. GDPR合规|数据控制者与数据处理者区别大吗?分别承担什么法律责任?
  18. Vasicek短期利率模型
  19. kvm 安装 windows 虚拟机
  20. asr语音识别,js页面demo,websocket实时语音

热门文章

  1. 海康威视、大华监控摄像头rtsp地址规则
  2. 服务器的系统盘存储,云服务器 系统盘 存储盘
  3. 一步一步学习Servlet之Cookie使用
  4. HDU-2203-亲和串(kmp)
  5. 西瓜书《支持向量机SVM》 原始形式推导+拉格朗日乘数法的SVM形式+SVM对偶形式推导+SMO算法推导
  6. Python基础----Matplotlib
  7. CALayer学习--contentsCenter属性
  8. windows2003修改远程桌面连接数
  9. 微软取消被指下流的Windows 10更新方法
  10. 月薪2500到年薪20万+,阿迪经历了些什么?