通過mysqlnow()函式校正本地(windows)時間與伺服器(linux)時間
通過連線伺服器的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 }
相關推薦
通過mysqlnow()函式校正本地(windows)時間與伺服器(linux)時間
通過連線伺服器的mysql伺服器,通過select查詢記錄集,其中記錄集的一列是now(),然後就看程式碼吧 class CInfo { public: time_t m_TimeOffset; CTime nowtm;// 修正到伺服器時間 CTime m_D
PHP顯示的時間與伺服器上時間不同
在寫程式碼的時候有時候需要用到時間,明明php.ini中已經設定date.timezone = PRC ,但是顯示的時間還是與伺服器上不同,這時只需要在程式碼中新增一行程式碼即可:date_default_timezone_set('PRC'); echo date("Y-m
Windows server 2012 NTP配置,實現成員服務器及客戶端時間與域控制器時間同步
ges 域控制器 pad 右鍵 多個 控制器 lint 命令 解決方法 近期,發現公司域成員用戶時間與域服務器時間不同步 ,要實現:1、域內成員服務器及域內客戶端與域內NTP服務器同步 ,2、域NTP服務器當做客戶端與外網NTP服務器同步(配置Windows 時間服務以使用
【常見問題1】通過遠端連線進行本地Windows和遠端裝置的檔案拷貝
工作中遇到的問題,之前一直是直接從伺服器直接拖入拖出,今天發現tools沒法正常使用,就找到此方法:從遠端裝置(伺服器)上拷貝檔案到本地Windows。 遠端連線一臺裝置,無法拷貝檔案到本地的解決方法: 在連線之前點選左下方顯示選項 之後出現介面,選擇本地資源選項
AD域中客戶端時間與服務器時間不同步的解決辦法
發現 反饋 交流 取域 tcl 宿主機 src config syn http://yuntcloud.blog.51cto.com/1173839/1240948 公司考勤用的是國內一家OA軟件裏的考勤模塊,個人在電腦上在規定時間內登記的形式,客戶端都是采取域用戶
服務器時間與互聯網時間不一致,出現的問題
color 互聯 .com nbsp source install jin exchange 服務器時間 Create queue exchange-webchat-development Failed: ali_mns response status error,code
CentOS設置系統時間與網絡時間同步
centos設置系統時間與網絡時間同步1.? 安裝ntpdate工具#?yum?-y?install?ntp?ntpdate2.? 設置系統時間與網絡時間同步#?ntpdate?cn.pool.ntp.org3.? 將系統時間寫入硬件時間#?hwclock?--systohcCentOS設置系統時間與網絡時間
Django中MySQL資料庫時間與真實插入時間相差8小時的解決
class BaseModel(models.Model): """模型類基類""" create_time = models.DateTimeField(auto_now_add=True, verbose_name="新增時間") &nb
APP(Android版)客戶端與伺服器端時間校準
APP開發人員經常會遇見一個bug就是,APP顯示的時間不準,或者說APP時間與伺服器時間不一致,會導致資料請求、資料顯示等各種問題。這時候我們就需要一種機制來解決時間不一致的問題。 解決方案如下: 1.伺服器端永遠使用UTC時間,包括引數和返回值,不要使用Date格式,而是使用UT
Hadoop-Mapreduce本地Windows和服務端Linux除錯
Mapreduce本地Windows和服務端Linux除錯 本地windows除錯 本地測試環境(windows): 在windows的hadoop目錄bin目錄有一個winutils.exe 在windows
轉:LoadRunner響應時間與使用者體驗時間不一致問題的深入分析
LoadRunner執行web_url()語句時,請求資源的先後順序不依賴程式碼書寫順序,導致很難直接確定執行web_url()的開始時間,但可以藉助LoadRunner的分析工具模組頁面診斷器(Web Page Diagnostics)獲取事務開始時刻和結束結束。在Web Page Diagnostics
通過VMware虛擬機器在windows 10 上安裝Linux Ubuntu
Linux Part 1. 通過VMware虛擬機器安裝Linux (Ubuntu) 瞭解Linux Linux大家都很熟了,就不介紹了, [網上很多介紹Linux的文章寫的都很好的] 因為
VC++中ADO方式訪問資料庫datetime欄位(不帶毫秒時間與帶毫秒時間)
//取得列名 bstrColName = m_pRSet->GetFields()->Item[nCol]->GetName() ; strColname = (char*)bstrColName ; //取得當前行當前列值 varCounter.lVal =
Jmeter -網路時間與伺服器時間的區分
在 LR 中是有一個“網頁細分圖”的,通過這個圖,你可以比較容易的區分哪些請求的響應時間最長,如果響應時間過程,是消耗在server處理的時候,還是消耗在網路傳輸過程中——也就是所謂的 Server time 和 Network time。 在jmeter中,
配置ntp客戶端與伺服器端時間的同步
1,實驗機器介紹 Ip地址 伺服器1 192.168.245.128 伺服器2 192.168.245.130 客戶端1 192.1
客戶端與伺服器端時間保持一致
一、問題描述 需要解決的問題很簡單,就是如何在頁面上比較準確的顯示伺服器時間。目前比較常用的方法就是根據基準時間使用setTimeout或 setInterval來計算最新的時間,這樣的問題在於setTimeout與setInterval的時間精度比較低,經測試一分鐘大概能相差幾秒 (與電腦效能以及執行
移植ntp服務至嵌入式開發板 與伺服器同步時間
開發板: helper2416 CPU: 三星2416 linux核心版本:3.2.6 2. 編譯: # cd ntp-4.2.8p2/ # ./configure --host=arm-linux --with-yielding-select=yes# make &
php取得當前時間函式(取到的是伺服器的時間)
http://www.php100.com/html/php/hanshu/2013/0905/4551.html php取得當前時間函式文章提供了php的幾種獲取當前時間的函式,date,time等哦,同時告訴我如何解決時區問題哦。 php教程取得當前時間函式 文
mac 本地檔案與伺服器Linux檔案之間的相互傳輸
可以簡單用scp 命令來實現 檢視scp幫助:sup -h 輸出資訊如下:usage: scp [-12346BCEpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_op
Javascript實現商品秒殺倒計時(時間與伺服器時間同步)
現在有很多網站都在做秒殺商品,而這其中有一個很重要的環節就是倒計時。 關於倒計時,有下面幾點需要注意: 1.應該使用伺服器時間而不是本地時間(本地時間存在時區不同、使用者自行設定等問題)。 2.要考慮網路傳輸的耗時。 3.獲取時間時可直接從AJAX的響應頭中讀取(通過get