Jump to content

Sistem Tarih Bilgisini Güncellemek!


blue_life
 Share

Recommended Posts

Bir arkadaşımızın istediği üzerine aşağıdaki kodları, autoit forumundan derledim.
Kısaca internette bağlanarak pool.ntp.org sunucusundan tarih bilgisi alınıyor ve sistem tarih bilgisi güncelleniyor.

[codebox]#include <Date.au3>
;~ #include <Array.au3>

;~ Local $bUp_time = _Blue_Update_DATE(False) ;Tarih bilgisi alınır ve sistem tarih bilgisi güncellenmez
Local $bUp_time = _Blue_Update_DATE(True) ;Tarih bilgisi alınır ve sistem tarih bilgisi güncellenir
If Not @error Then
MsgBox(64, "Tarih Bilgisi Güncellendi!", $bUp_time[0] & "/" & $bUp_time[1] & "/" & $bUp_time[2] & " " & $bUp_time[3] & ":" & $bUp_time[4] & ":" & $bUp_time[5])
Else
MsgBox(16, "Tarih Bilgisi Güncellenemedi!", "Tarih Bilgisi Güncellenemedi!")
EndIf
Exit

Func _Blue_Update_DATE($bUpdate = True, $ntpServer = "pool.ntp.org") ;de.pool.ntp.org
Local $Blue_Return_Array[6]
Local $BlueData, $BlueData2, $bTimer = 5 ;5 sec
Local $BlueError = False
UDPStartup()
Local $socket = UDPOpen(TCPNameToIP($ntpServer), 123)
If @error Then
$BlueError = True
Else
UDPSend($socket, Binary("0x1B0E01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"))
If @error Then
$BlueError = True
Else
Local $bTimerInit = TimerInit()
While $BlueData = ""
$BlueData = UDPRecv($socket, 100)
If $bTimer < Round(TimerDiff($bTimerInit) / 1000) Then ExitLoop
Sleep(100)
WEnd
$bTimerInit = ""
If $BlueData = "" Then $BlueError = True
EndIf
EndIf
UDPCloseSocket($socket)
UDPShutdown()

If $BlueError = True Then Return SetError(1, 0, $Blue_Return_Array)
$BlueData = StringMid($BlueData, 83, 8)

$BlueData2 = StringRight($BlueData, 1)
$BlueData = StringTrimRight($BlueData, 1)
$BlueData = _DateAdd("s", Dec($BlueData) * 16 + Dec($BlueData2), "1900/01/01 00:00:00")
If @error Then Return SetError(2, 0, $Blue_Return_Array)
$BlueData = StringRegExpReplace($BlueData, "[^0-9]", "")

$Blue_Return_Array[0] = StringMid($BlueData, 1, 4) ;Year
$Blue_Return_Array[1] = StringMid($BlueData, 5, 2) ;Mon
$Blue_Return_Array[2] = StringMid($BlueData, 7, 2) ;Mday

$Blue_Return_Array[3] = StringMid($BlueData, 9, 2) ;Hour
$Blue_Return_Array[4] = StringMid($BlueData, 11, 2) ;Min
$Blue_Return_Array[5] = StringMid($BlueData, 13, 2) ;Sec

Local $EncodeST
If $bUpdate = True Then
$EncodeST = _Date_Time_EncodeSystemTime($Blue_Return_Array[1], $Blue_Return_Array[2], $Blue_Return_Array[0], $Blue_Return_Array[3], $Blue_Return_Array[4], $Blue_Return_Array[5])
If Not _Date_Time_SetSystemTime(DllStructGetPtr($EncodeST)) Then Return SetError(3, 0, $Blue_Return_Array)
EndIf
Return $Blue_Return_Array
EndFunc ;==>_Blue_Update_DATE[/codebox]
[hr]
Edit: Udfleri kullanmak istemeyenler ve exe boyutunu düşürmek isteyenler aşağıdaki kodları kullanabilir.

[codebox];~ #include <Date.au3>
;~ #include <Array.au3>

;~ Local $bUp_time = _Blue_Update_DATE(False) ;Tarih bilgisi alınır ve sistem tarih bilgisi güncellenmez
Local $bUp_time = _Blue_Update_DATE(True) ;Tarih bilgisi alınır ve sistem tarih bilgisi güncellenir
If Not @error Then
MsgBox(64, "Tarih Bilgisi Güncellendi!", $bUp_time[0] & "/" & $bUp_time[1] & "/" & $bUp_time[2] & " " & $bUp_time[3] & ":" & $bUp_time[4] & ":" & $bUp_time[5])
Else
MsgBox(16, "Tarih Bilgisi Güncellenemedi!", "Tarih Bilgisi Güncellenemedi!")
EndIf
Exit

Func _Blue_Update_DATE($bUpdate = True, $ntpServer = "pool.ntp.org") ;de.pool.ntp.org
Local $Blue_Return_Array[6]
Local $BlueData, $BlueData2, $bTimer = 5 ;5 sec
Local $BlueError = False
UDPStartup()
Local $socket = UDPOpen(TCPNameToIP($ntpServer), 123)
If @error Then
$BlueError = True
Else
UDPSend($socket, Binary("0x1B0E01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"))
If @error Then
$BlueError = True
Else
Local $bTimerInit = TimerInit()
While $BlueData = ""
$BlueData = UDPRecv($socket, 100)
If $bTimer < Round(TimerDiff($bTimerInit) / 1000) Then ExitLoop
Sleep(100)
WEnd
$bTimerInit = ""
If $BlueData = "" Then $BlueError = True
EndIf
EndIf
UDPCloseSocket($socket)
UDPShutdown()

If $BlueError = True Then Return SetError(1, 0, $Blue_Return_Array)
$BlueData = StringMid($BlueData, 83, 8)

$BlueData2 = StringRight($BlueData, 1)
$BlueData = StringTrimRight($BlueData, 1)
$BlueData = __DateAdd("s", Dec($BlueData) * 16 + Dec($BlueData2), "1900/01/01 00:00:00")
If @error Then Return SetError(2, 0, $Blue_Return_Array)
$BlueData = StringRegExpReplace($BlueData, "[^0-9]", "")

$Blue_Return_Array[0] = StringMid($BlueData, 1, 4) ;Year
$Blue_Return_Array[1] = StringMid($BlueData, 5, 2) ;Mon
$Blue_Return_Array[2] = StringMid($BlueData, 7, 2) ;Mday

$Blue_Return_Array[3] = StringMid($BlueData, 9, 2) ;Hour
$Blue_Return_Array[4] = StringMid($BlueData, 11, 2) ;Min
$Blue_Return_Array[5] = StringMid($BlueData, 13, 2) ;Sec

Local $EncodeST
If $bUpdate = True Then
$EncodeST = __Date_Time_EncodeSystemTime($Blue_Return_Array[1], $Blue_Return_Array[2], $Blue_Return_Array[0], $Blue_Return_Array[3], $Blue_Return_Array[4], $Blue_Return_Array[5])
If Not __Date_Time_SetSystemTime(DllStructGetPtr($EncodeST)) Then Return SetError(3, 0, $Blue_Return_Array)
EndIf
Return $Blue_Return_Array
EndFunc ;==>_Blue_Update_DATE

Func __Date_Time_EncodeFileTime($iMonth, $iDay, $iYear, $iHour = 0, $iMinute = 0, $iSecond = 0, $iMSeconds = 0)
Local $tSystemTime = __Date_Time_EncodeSystemTime($iMonth, $iDay, $iYear, $iHour, $iMinute, $iSecond, $iMSeconds)
Return __Date_Time_SystemTimeToFileTime(DllStructGetPtr($tSystemTime))
EndFunc ;==>__Date_Time_EncodeFileTime

Func __Date_Time_SystemTimeToFileTime($pSystemTime)
Local $tFileTime = DllStructCreate("dword Lo;dword Hi")
Local $aResult = DllCall("kernel32.dll", "bool", "SystemTimeToFileTime", "ptr", $pSystemTime, "ptr", DllStructGetPtr($tFileTime))
If @error Then Return SetError(@error, @extended, 0)
Return SetExtended($aResult[0], $tFileTime)
EndFunc ;==>__Date_Time_SystemTimeToFileTime

Func __Date_Time_SetSystemTime($pSystemTime)
Local $aResult = DllCall("kernel32.dll", "bool", "SetSystemTime", "ptr", $pSystemTime)
If @error Then Return SetError(@error, @extended, False)
Return $aResult[0]
EndFunc ;==>__Date_Time_SetSystemTime

Func __Date_Time_EncodeSystemTime($iMonth, $iDay, $iYear, $iHour = 0, $iMinute = 0, $iSecond = 0, $iMSeconds = 0)
Local $tSystemTime = DllStructCreate("word Year;word Month;word Dow;word Day;word Hour;word Minute;word Second;word MSeconds")
DllStructSetData($tSystemTime, "Month", $iMonth)
DllStructSetData($tSystemTime, "Day", $iDay)
DllStructSetData($tSystemTime, "Year", $iYear)
DllStructSetData($tSystemTime, "Hour", $iHour)
DllStructSetData($tSystemTime, "Minute", $iMinute)
DllStructSetData($tSystemTime, "Second", $iSecond)
DllStructSetData($tSystemTime, "MSeconds", $iMSeconds)
Return $tSystemTime
EndFunc ;==>__Date_Time_EncodeSystemTime

Func __DateAdd($sType, $iValToAdd, $sDate)
Local $asTimePart[4]
Local $asDatePart[4]
Local $iJulianDate
; Verify that $sType is Valid
$sType = StringLeft($sType, 1)
If StringInStr("D,M,Y,w,h,n,s", $sType) = 0 Or $sType = "" Then
Return SetError(1, 0, 0)
EndIf
; Verify that Value to Add is Valid
If Not StringIsInt($iValToAdd) Then
Return SetError(2, 0, 0)
EndIf
; Verify If InputDate is valid
If Not __DateIsValid($sDate) Then
Return SetError(3, 0, 0)
EndIf
; split the date and time into arrays
__DateTimeSplit($sDate, $asDatePart, $asTimePart)

; ====================================================
; adding days then get the julian date
; add the number of day
; and convert back to Gregorian
If $sType = "d" Or $sType = "w" Then
If $sType = "w" Then $iValToAdd = $iValToAdd * 7
$iJulianDate = __DateToDayValue($asDatePart[1], $asDatePart[2], $asDatePart[3]) + $iValToAdd
__DayValueToDate($iJulianDate, $asDatePart[1], $asDatePart[2], $asDatePart[3])
EndIf
; ====================================================
; adding Months
If $sType = "m" Then
$asDatePart[2] = $asDatePart[2] + $iValToAdd
; pos number of months
While $asDatePart[2] > 12
$asDatePart[2] = $asDatePart[2] - 12
$asDatePart[1] = $asDatePart[1] + 1
WEnd
; Neg number of months
While $asDatePart[2] < 1
$asDatePart[2] = $asDatePart[2] + 12
$asDatePart[1] = $asDatePart[1] - 1
WEnd
EndIf
; ====================================================
; adding Years
If $sType = "y" Then
$asDatePart[1] = $asDatePart[1] + $iValToAdd
EndIf
; ====================================================
; adding Time value
If $sType = "h" Or $sType = "n" Or $sType = "s" Then
Local $iTimeVal = __TimeToTicks($asTimePart[1], $asTimePart[2], $asTimePart[3]) / 1000
If $sType = "h" Then $iTimeVal = $iTimeVal + $iValToAdd * 3600
If $sType = "n" Then $iTimeVal = $iTimeVal + $iValToAdd * 60
If $sType = "s" Then $iTimeVal = $iTimeVal + $iValToAdd
; calculated days to add
Local $Day2Add = Int($iTimeVal / (24 * 60 * 60))
$iTimeVal = $iTimeVal - $Day2Add * 24 * 60 * 60
If $iTimeVal < 0 Then
$Day2Add = $Day2Add - 1
$iTimeVal = $iTimeVal + 24 * 60 * 60
EndIf
$iJulianDate = __DateToDayValue($asDatePart[1], $asDatePart[2], $asDatePart[3]) + $Day2Add
; calculate the julian back to date
__DayValueToDate($iJulianDate, $asDatePart[1], $asDatePart[2], $asDatePart[3])
; caluculate the new time
__TicksToTime($iTimeVal * 1000, $asTimePart[1], $asTimePart[2], $asTimePart[3])
EndIf
; ====================================================
; check if the Input day is Greater then the new month last day.
; if so then change it to the last possible day in the month
Local $iNumDays = __DaysInMonth($asDatePart[1])
;
If $iNumDays[$asDatePart[2]] < $asDatePart[3] Then $asDatePart[3] = $iNumDays[$asDatePart[2]]
; ========================
; Format the return date
; ========================
; Format the return date
$sDate = $asDatePart[1] & '/' & StringRight("0" & $asDatePart[2], 2) & '/' & StringRight("0" & $asDatePart[3], 2)
; add the time when specified in the input
If $asTimePart[0] > 0 Then
If $asTimePart[0] > 2 Then
$sDate = $sDate & " " & StringRight("0" & $asTimePart[1], 2) & ':' & StringRight("0" & $asTimePart[2], 2) & ':' & StringRight("0" & $asTimePart[3], 2)
Else
$sDate = $sDate & " " & StringRight("0" & $asTimePart[1], 2) & ':' & StringRight("0" & $asTimePart[2], 2)
EndIf
EndIf
;
Return ($sDate)
EndFunc ;==>__DateAdd

Func __DateIsValid($sDate)
Local $asDatePart[4], $asTimePart[4]
; split the Date and Time portion
Local $sDateTime = StringSplit($sDate, " T")
; split the date portion
If $sDateTime[0] > 0 Then $asDatePart = StringSplit($sDateTime[1], "/-.")
; Ensure the date contains 3 sections YYYY MM DD
If UBound($asDatePart) <> 4 Then Return (0)
If $asDatePart[0] <> 3 Then Return (0)
; verify valid input date values
; Make sure the Date parts contains numeric
If Not StringIsInt($asDatePart[1]) Then Return (0)
If Not StringIsInt($asDatePart[2]) Then Return (0)
If Not StringIsInt($asDatePart[3]) Then Return (0)
$asDatePart[1] = Number($asDatePart[1])
$asDatePart[2] = Number($asDatePart[2])
$asDatePart[3] = Number($asDatePart[3])
; check if all contain valid values
Local $iNumDays = __DaysInMonth($asDatePart[1])
If $asDatePart[1] < 1000 Or $asDatePart[1] > 2999 Then Return (0)
If $asDatePart[2] < 1 Or $asDatePart[2] > 12 Then Return (0)
If $asDatePart[3] < 1 Or $asDatePart[3] > $iNumDays[$asDatePart[2]] Then Return (0)
; split the Time portion
If $sDateTime[0] > 1 Then
$asTimePart = StringSplit($sDateTime[2], ":")
If UBound($asTimePart) < 4 Then ReDim $asTimePart[4]
Else
Dim $asTimePart[4]
EndIf
; check Time portion
If $asTimePart[0] < 1 Then Return (1) ; No time specified so date must be correct
If $asTimePart[0] < 2 Then Return (0) ; need at least HH:MM when something is specified
If $asTimePart[0] = 2 Then $asTimePart[3] = "00" ; init SS when only HH:MM is specified
; Make sure the Time parts contains numeric
If Not StringIsInt($asTimePart[1]) Then Return (0)
If Not StringIsInt($asTimePart[2]) Then Return (0)
If Not StringIsInt($asTimePart[3]) Then Return (0)
; check if all contain valid values
$asTimePart[1] = Number($asTimePart[1])
$asTimePart[2] = Number($asTimePart[2])
$asTimePart[3] = Number($asTimePart[3])
If $asTimePart[1] < 0 Or $asTimePart[1] > 23 Then Return (0)
If $asTimePart[2] < 0 Or $asTimePart[2] > 59 Then Return (0)
If $asTimePart[3] < 0 Or $asTimePart[3] > 59 Then Return (0)
; we got here so date/time must be good
Return 1
EndFunc ;==>__DateIsValid

Func __DateTimeSplit($sDate, ByRef $asDatePart, ByRef $iTimePart)
; split the Date and Time portion
Local $sDateTime = StringSplit($sDate, " T")
; split the date portion
If $sDateTime[0] > 0 Then $asDatePart = StringSplit($sDateTime[1], "/-.")
; split the Time portion
If $sDateTime[0] > 1 Then
$iTimePart = StringSplit($sDateTime[2], ":")
If UBound($iTimePart) < 4 Then ReDim $iTimePart[4]
Else
Dim $iTimePart[4]
EndIf
; Ensure the arrays contain 4 values
If UBound($asDatePart) < 4 Then ReDim $asDatePart[4]
; update the array to contain numbers not strings
For $x = 1 To 3
If StringIsInt($asDatePart[$x]) Then
$asDatePart[$x] = Number($asDatePart[$x])
Else
$asDatePart[$x] = -1
EndIf
If StringIsInt($iTimePart[$x]) Then
$iTimePart[$x] = Number($iTimePart[$x])
Else
$iTimePart[$x] = 0
EndIf
Next
Return 1
EndFunc ;==>__DateTimeSplit

Func __DateToDayValue($iYear, $iMonth, $iDay)
; Verify If InputDate is valid
If Not __DateIsValid(StringFormat("%04d/%02d/%02d", $iYear, $iMonth, $iDay)) Then
Return SetError(1, 0, "")
EndIf
If $iMonth < 3 Then
$iMonth = $iMonth + 12
$iYear = $iYear - 1
EndIf
Local $i_aFactor = Int($iYear / 100)
Local $i_bFactor = Int($i_aFactor / 4)
Local $i_cFactor = 2 - $i_aFactor + $i_bFactor
Local $i_eFactor = Int(1461 * ($iYear + 4716) / 4)
Local $i_fFactor = Int(153 * ($iMonth + 1) / 5)
Local $iJulianDate = $i_cFactor + $iDay + $i_eFactor + $i_fFactor - 1524.5
Return ($iJulianDate)
EndFunc ;==>__DateToDayValue

Func __DayValueToDate($iJulianDate, ByRef $iYear, ByRef $iMonth, ByRef $iDay)
; check for valid input date
If $iJulianDate < 0 Or Not IsNumber($iJulianDate) Then
Return SetError(1, 0, 0)
EndIf
; calculte the date
Local $i_zFactor = Int($iJulianDate + 0.5)
Local $i_wFactor = Int(($i_zFactor - 1867216.25) / 36524.25)
Local $i_xFactor = Int($i_wFactor / 4)
Local $i_aFactor = $i_zFactor + 1 + $i_wFactor - $i_xFactor
Local $i_bFactor = $i_aFactor + 1524
Local $i_cFactor = Int(($i_bFactor - 122.1) / 365.25)
Local $i_dFactor = Int(365.25 * $i_cFactor)
Local $i_eFactor = Int(($i_bFactor - $i_dFactor) / 30.6001)
Local $i_fFactor = Int(30.6001 * $i_eFactor)
$iDay = $i_bFactor - $i_dFactor - $i_fFactor
; (must get number less than or equal to 12)
If $i_eFactor - 1 < 13 Then
$iMonth = $i_eFactor - 1
Else
$iMonth = $i_eFactor - 13
EndIf
If $iMonth < 3 Then
$iYear = $i_cFactor - 4715 ; (if Month is January or February)
Else
$iYear = $i_cFactor - 4716 ;(otherwise)
EndIf
$iYear = StringFormat("%04d", $iYear)
$iMonth = StringFormat("%02d", $iMonth)
$iDay = StringFormat("%02d", $iDay)
Return $iYear & "/" & $iMonth & "/" & $iDay
EndFunc ;==>__DayValueToDate

Func __TicksToTime($iTicks, ByRef $iHours, ByRef $iMins, ByRef $iSecs)
If Number($iTicks) > 0 Then
$iTicks = Int($iTicks / 1000)
$iHours = Int($iTicks / 3600)
$iTicks = Mod($iTicks, 3600)
$iMins = Int($iTicks / 60)
$iSecs = Mod($iTicks, 60)
; If $iHours = 0 then $iHours = 24
Return 1
ElseIf Number($iTicks) = 0 Then
$iHours = 0
$iTicks = 0
$iMins = 0
$iSecs = 0
Return 1
Else
Return SetError(1, 0, 0)
EndIf
EndFunc ;==>__TicksToTime

Func __TimeToTicks($iHours = @HOUR, $iMins = @MIN, $iSecs = @SEC)
If StringIsInt($iHours) And StringIsInt($iMins) And StringIsInt($iSecs) Then
Local $iTicks = 1000 * ((3600 * $iHours) + (60 * $iMins) + $iSecs)
Return $iTicks
Else
Return SetError(1, 0, 0)
EndIf
EndFunc ;==>__TimeToTicks

Func __DaysInMonth($iYear)
Local $aiDays[13] = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
If __DateIsLeapYear($iYear) Then $aiDays[2] = 29
Return $aiDays
EndFunc ;==>__DaysInMonth

Func __DateIsLeapYear($iYear)
If StringIsInt($iYear) Then
Select
Case Mod($iYear, 4) = 0 And Mod($iYear, 100) <> 0
Return 1
Case Mod($iYear, 400) = 0
Return 1
Case Else
Return 0
EndSelect
EndIf
Return SetError(1, 0, 0)
EndFunc ;==>__DateIsLeapYear[/codebox]

Edited by blue_life
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...