这几天在用MASM32按Time Protocol(RFC868)协议编写网络对时程序,现在基本完成了。将其中一些有用的函数代码整理了一下。
我们需要将本地系统时间转换为UTC格式发送给时间服务器,相关转换代码如下:
BaseTimeLow equ 0D53E8000h
BaseTimeHigh equ 19DB1DEh
;:::::::::::::::::::::::::::::::::::::::::::::::::::
FileTime2DateStamp proc lpFileTime: dword
;:::::::::::::::::::::::::::::::::::::::::::::::::::
mov ecx, lpFileTime
mov eax, [ecx]
mov edx, [ecx+4]
sub eax, BaseTimeLow
sbb edx, BaseTimeHigh
mov ecx, 10000000
div ecx
RET
FileTime2DateStamp endp
;::::::::::::::::::::::::::::::::::::::::
GetLocalTimeStamp proc
;::::::::::::::::::::::::::::::::::::::::
local stSystemTime: SYSTEMTIME
local stFileTime: FILETIME
invoke GetSystemTime, addr stSystemTime
invoke SystemTimeToFileTime, addr stSystemTime, addr stFileTime
invoke FileTime2DateStamp, addr stFileTime
ret
GetLocalTimeStamp endp
时间服务器返回的是从1900年1月1日午夜到现在的秒数,将其转换为本地时间的相关代码如下:
;::::::::::::::::::::::::::::::::::::::::::::::::::::
_MemCpy proc lpD, lpS, dwSize
;::::::::::::::::::::::::::::::::::::::::::::::::::::
mov esi, lpS
mov edi, lpD
mov ecx, dwSize
rep movsb
ret
_MemCpy endp
g_szFmtDateTime db "%d-%d-%d %d:%d:%d", 0
;::::::::::::::::::::::::::::::::::::::::::::::::::::
printDateTime proc dwTimeStamp: dword
;::::::::::::::::::::::::::::::::::::::::::::::::::::
local buf[30]: byte
local stSysTime: SYSTEMTIME
local stFileTime: FILETIME
local stLocFileTime: FILETIME
local li: LARGE_INTEGER
invoke RtlZeroMemory, addr stSysTime, sizeof stSysTime
mov stSysTime.wYear, 1900
mov stSysTime.wMonth, 1
mov stSysTime.wDay, 1
invoke SystemTimeToFileTime, addr stSysTime, addr stFileTime
invoke _MemCpy, addr li, addr stFileTime, sizeof stFileTime
mov eax, dwTimeStamp
mov edx, 10000000
mul edx; edx=10000000
lea edi, li.QuadPart
add [edi], eax
adc [edi+4], edx
invoke _MemCpy, addr stFileTime, addr li, sizeof li
invoke FileTimeToLocalFileTime, addr stFileTime, addr stLocFileTime
lea edi, stSysTime;push edi
invoke FileTimeToSystemTime, addr stLocFileTime, edi ;edi=addr stSysTime
;pop edi
movzx eax, (SYSTEMTIME ptr [edi]).wYear
movzx ebx, (SYSTEMTIME ptr [edi]).wMonth
movzx ecx, (SYSTEMTIME ptr [edi]).wDay
movzx edx, (SYSTEMTIME ptr [edi]).wHour
movzx esi, (SYSTEMTIME ptr [edi]).wMinute
movzx edi, (SYSTEMTIME ptr [edi]).wSecond
invoke wsprintf, addr buf, addr g_szFmtDateTime, eax, ebx, ecx,edx, esi, edi
invoke StdOut, addr buf
ret
printDateTime endp