Get Local Time as FILETIME for comparing with files

From NSIS Wiki
Jump to navigationJump to search
Author: Vytautas (talk, contrib)


Description

The following functions show how to get the current local time in the FileTime format to enable comparison with file attributes, e.g. Creation, Last Access and Modification times of a file. The two functions rely on a SYSTEMTIME structure so you should include the following line in your script '!include "${NSISDIR}\Contrib\System\sysfunc.nsh"'

GetLocalAsFileTime

Description

The first function returns the actual FILETIME structure in two integers on the stack.

The Function

!include "${NSISDIR}\Examples\System\System.nsh"
Function GetLocalAsFileTime
; Returns on the stack Low & High Integers of the FILETIME Structure of
; current local time
  Push $0
  Push $1
  Push $2
  System::Call '*${stSYSTEMTIME}.r1' ; Allocate SYSTEMTIME
  System::Call 'kernel32::GetLocalTime(pr1)'
  System::Call 'kernel32::SystemTimeToFileTime(pr1s,*l.r3)'
  System::Free ; Free SYSTEMTIME
  System::Int64Op $3 & 0xffffffff
  Pop $1
!if "${NSIS_PTR_SIZE}" > 0
  System::Int64Op $3 >>> 32
!else
  System::Int64Op $3 >> 32
  System::Int64Op 0xffffffff &
!endif
  Pop $2
  Push $1 ; $1 contains the low  order Int32
  Push $2 ; $2 contains the high order Int32
  Exch 4
  Exch
  Exch 3
  Exch
  Exch 2
  Pop $2
  Pop $1
  Pop $0
FunctionEnd

GetLocalAsInt64

Description

The second function goes one step further and converts the FILETIME structure into Int64 for easier manipulation.

The Function

!include "${NSISDIR}\Examples\System\System.nsh"
Function GetLocalAsInt64
; Returns on the stack Int64 of the FILETIME Structure of current local time
  Push $1
  System::Call '*${stSYSTEMTIME}.r1' ; Allocate SYSTEMTIME
  System::Call 'kernel32::GetLocalTime(pr1)'
  System::Call 'kernel32::SystemTimeToFileTime(pr1,*l.s)'
  System::Free $1 ; Free SYSTEMTIME
  Exch
  Pop $1
FunctionEnd

The Script

And the following script shows how to use these functions.

!include "${NSISDIR}\Contrib\System\sysfunc.nsh"
 
  ...
  Insert the Functions somewhere in the script
  ...
 
  Call GetLocalAsFileTime
  pop $R1  ;Now Contains the low  order of FileTime
  pop $R2  ;Now Contains the high order of FileTime
 
  Call GetLocalAsInt64
  pop $R3  ;Now Contains the Int64 of FileTime

These functions were made with a lot of help from kichik and brainsucker. Hope you find these functions of some help.

Vytautas