獲取網路時間並重新整理本地時間(原始碼1)

2021-08-29 02:21:43 字數 2054 閱讀 1639

#define win32_lean_and_mean

#include #include #include #include #include #include #pragma comment (lib,"ws2_32")

#define unicode

#define _unicode

#define _countof(array) (sizeof(array)/sizeof(array[0]))

#define hightime 21968699 // 21968708 // jan 1, 1900 filetime.hightime

#define lowtime 4259332096 // 1604626432 // jan 1, 1900 filetime.lowtime

using namespace std;

//ntp伺服器列表

struct nistsvr nistsvrsary = , null, null},

, _t("time-a.nist.gov"),_t("nist,蓋士堡,馬里蘭州") },

, _t("time-b.nist.gov"),_t("nist,蓋士堡,馬里蘭州") },

, _t("time-a.timefreq.bldrdoc.gov"),_t("nist,博耳德市,科羅拉多州") },

, _t("time-b.timefreq.bldrdoc.gov"),_t("nist,博耳德市,科羅拉多州") },

, _t("time-c.timefreq.bldrdoc.gov"),_t("nist,博耳德市,科羅拉多州") },

, _t("tutcnist.colorado.edu"),_t("科羅拉多大學,博耳德市") },

, _t("time.nist.gov"),_t("ncar,博耳德市,科羅拉多州") },

, _t("time-nw.nist.gov"),_t("microsoft,雷蒙德,華盛頓州") },

, _t("nist1.nyc.certifiedtime.com"),_t("abovnet,紐約市") },

};//所選擇的ntp伺服器

static int choice = 0;

void usage()

}bool updatesystime(dword dwtime)

; uibasetime = ((uint64) hightime << 32) + lowtime;

uicurtime = (uint64)dwtime * (uint64)10000000;

uiresult = uibasetime + uicurtime;

filetimetosystemtime((lpfiletime)&uiresult, &st);

return setsystemtime(&st);

}bool gettimefromserver(dword *lpdwtime)

if(4 == iread)

else

printf( " error getting time!\n");

}else

printf( " ***connection failed***\n");

closesocket(ssock);

} return breturn;

}int main(int argc,char *argv)

if(argc<2)

else

choice = atoi(&argv[1][strlen(argv[1])-1]);

dword dwtime;

//取伺服器時間

if (gettimefromserver(&dwtime))

else

} wsacleanup();

return 0;

}

以上****與網路。親測可用。

注:設定本地時間或許不重要,但獲取網路時間是很重的,比如發布乙個程式,限制使用者使用到期時間,這個時候我們可以獲取網路時間,如果是獲取本地時間的話就很容易被別人更改本地時間從而跳出限制,獲取網路時間就相對安全一點。

獲取網路時間並重新整理本地時間(原始碼2)

因為上次那個獲取網路時間失效了 使用的是37埠 獲取網路時間並重新整理本地時間 原始碼1 所以網上又找了乙份 使用的是123埠,雖然沒有測試成功,這裡也做下記錄,估計是本機環境問題,過兩天再進行測試一下。getntptime.cpp 定義控制台應用程式的入口點。include stdafx.h us...

Vue 獲取當前時間並實時重新整理

前言 獲取當前系統時間,並在頁面上顯示並實時重新整理,和系統時間保持一致。內容 1 在data中宣告變數 data 2 定義獲取時間的方法gettime,並在created 宣告週期裡面呼叫,在例項建立前呼叫 created 3 具體方法如下 methods else this.month chec...

python 獲取網路時間及修改本地時間

本本經常時間錯亂,偶閒暇之作,專為同步本本時間。以前有朋友提到日期時間設定裡面的有些時間伺服器。限於不同的網路,有時候這些時間伺服器不一定能用。因此這次選擇的是www.baidu.com這個大家都能用吧。接下來上python 8 r.getheaders 獲取所有的http頭 9 ts r.geth...