name22

[AutoIt] Get Unix Timestamp, NtQuerySystemTime

Jul 20th, 2013
590
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $hNtDll = DllOpen("Ntdll.dll")
  2.  
  3. $iSysTime = _NtQuerySystemTime($hNtdll)
  4. $iUnixTime = _RtlTimeToSecondsSince1970($iSysTime, $hNtdll)
  5.  
  6. ConsoleWrite("System time: " & $iSysTime & @CRLF & _
  7.             "... in Unix time: " & $iUnixTime & @CRLF & _
  8.             "Another Unix Timestamp: " & _GetUnixTimestamp() & @CRLF)
  9.  
  10. DllClose($hNtDll)
  11.  
  12.  
  13. ; #FUNCTION# ====================================================================================================================
  14. ; Name ..........: _NtQuerySystemTime
  15. ; Description ...: Returns the system time in a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).
  16. ; Syntax ........: _NtQuerySystemTime([$_hNtdll = Default])
  17. ; Parameters ....: $_hNtdll             - [optional] Handle to an opened Ntdll.dll file.
  18. ; Return values .: Success - System time in a 64-bit integer.
  19. ;                  Failure - -1 and sets @error to 1.
  20. ; Author ........: name22 (www.autoit.de)
  21. ; Modified ......:
  22. ; Remarks .......:
  23. ; Related .......: _RtlTimeToSecondsSince1970
  24. ; Link ..........: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724512%28v=vs.85%29.aspx
  25. ; Example .......: Yes
  26. ; ===============================================================================================================================
  27. Func _NtQuerySystemTime($_hNtdll = Default)
  28.     If $_hNtdll = Default Then $_hNtdll = "Ntdll.dll"
  29.  
  30.     $_aRet = DllCall($_hNtdll, "INT", "NtQuerySystemTime", "INT64*", "")
  31.     If @error Then Return SetError(1, 0, -1)
  32.  
  33.     Return $_aRet[1]
  34. EndFunc
  35.  
  36. ; #FUNCTION# ====================================================================================================================
  37. ; Name ..........: _RtlTimeToSecondsSince1970
  38. ; Description ...: Converts the specified 64-bit system time to the number of seconds since the beginning of January 1, 1970.
  39. ; Syntax ........: _RtlTimeToSecondsSince1970($_iTime[, $_hNtdll = Default])
  40. ; Parameters ....: $_iTime              - 64-bit integer specifying the time in 100 nanosecond intervals since January 1, 1601 (UTC).
  41. ;                  $_hNtdll             - [optional] Handle to an opened Ntdll.dll file
  42. ; Return values .: None
  43. ; Author ........: name22 (www.autoit.de)
  44. ; Modified ......:
  45. ; Remarks .......:
  46. ; Related .......: _NtQuerySystemTime
  47. ; Link ..........: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724928%28v=vs.85%29.aspx
  48. ; Example .......: Yes
  49. ; ===============================================================================================================================
  50. Func _RtlTimeToSecondsSince1970($_iTime, $_hNtdll = Default)
  51.     If $_hNtdll = Default Then $_hNtdll = "Ntdll.dll"
  52.  
  53.     $_aRet = DllCall($_hNtdll, "BOOL", "RtlTimeToSecondsSince1970", "INT64*", $_iTime, "INT64*", "")
  54.     If @error Then Return SetError(1, 0, -1)
  55.  
  56.     Return $_aRet[2]
  57. EndFunc
  58.  
  59. ; #FUNCTION# ====================================================================================================================
  60. ; Name ..........: _GetUnixTimestamp
  61. ; Description ...: Returns the Unix Timestamp (or the number of seconds since January 1, 1970 according to system time).
  62. ; Syntax ........: _GetUnixTimestamp()
  63. ; Parameters ....:
  64. ; Return values .: Unix Timestamp.
  65. ; Author ........: name22 (www.autoit.de)
  66. ; Modified ......:
  67. ; Remarks .......: Not really necessary, since the code fits into one line. Mostly an example for retrieving the unix timestamp.
  68. ; Related .......: _NtQuerySystemTime, _RtlTimeToSecondsSince1970
  69. ; Link ..........: http://en.wikipedia.org/wiki/Unix_time
  70. ; Example .......: Yes
  71. ; ===============================================================================================================================
  72. Func _GetUnixTimestamp()
  73.     Return _RtlTimeToSecondsSince1970(_NtQuerySystemTime())
  74. EndFunc
RAW Paste Data