GetTime: Difference between revisions
From NSIS Wiki
Jump to navigationJump to search
m (Adding new author and category links.) |
Instructor (talk | contribs) No edit summary |
||
(5 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
{{PageAuthor|Instructor}} | {{PageAuthor|Instructor}} | ||
{{User:Instructor/Headers/Template}} | |||
== Function Description == | |||
<highlight-nsis> | |||
<highlight-nsis> | ____________________________________________________________________________ | ||
GetTime v1. | GetTime v1.5 | ||
____________________________________________________________________________ | ____________________________________________________________________________ | ||
Line 17: | Line 15: | ||
Features: | Features: | ||
1. Get local time. | 1. Get local or system time. | ||
2. Get file time (access, creation and modification). | 2. Get file time (access, creation and modification). | ||
Line 24: | Line 22: | ||
${GetTime} "[File]" "[Option]" $var1 $var2 $var3 $var4 $var5 $var6 $var7 | ${GetTime} "[File]" "[Option]" $var1 $var2 $var3 $var4 $var5 $var6 $var7 | ||
"[File]" ; Ignored if "L" | "[File]" ; Ignored if "L" or "LS" | ||
; | ; | ||
"[Option]" ; [Options] | "[Option]" ; [Options] | ||
Line 31: | Line 29: | ||
; C Creation file time | ; C Creation file time | ||
; M Modification file time | ; M Modification file time | ||
; LS System time (UTC) | |||
; AS last Access file time (UTC) | |||
; CS Creation file time (UTC) | |||
; MS Modification file time (UTC) | |||
; | ; | ||
$var1 ; Result1: day | $var1 ; Result1: day | ||
Line 44: | Line 46: | ||
-Error flag if file isn't exist | -Error flag if file isn't exist | ||
-Error flag if syntax error | -Error flag if syntax error | ||
</highlight-nsis> | |||
Example (Get local time): | <b>Example (Get local time):</b> | ||
Section | <highlight-nsis>Section | ||
${GetTime} "" "L" $0 $1 $2 $3 $4 $5 $6 | ${GetTime} "" "L" $0 $1 $2 $3 $4 $5 $6 | ||
; $0="01" day | ; $0="01" day | ||
Line 60: | Line 63: | ||
MessageBox MB_OK 'Date=$0/$1/$2 ($3)$\nTime=$4:$5:$6' | MessageBox MB_OK 'Date=$0/$1/$2 ($3)$\nTime=$4:$5:$6' | ||
SectionEnd | SectionEnd | ||
</highlight-nsis> | |||
Example (Get file time): | <b>Example (Get file time):</b> | ||
Section | <highlight-nsis>Section | ||
${GetTime} "$WINDIR\Explorer.exe" "C" $0 $1 $2 $3 $4 $5 $6 | ${GetTime} "$WINDIR\Explorer.exe" "C" $0 $1 $2 $3 $4 $5 $6 | ||
; $0="12" day | ; $0="12" day | ||
Line 76: | Line 80: | ||
MessageBox MB_OK "Error" IDOK +2 | MessageBox MB_OK "Error" IDOK +2 | ||
MessageBox MB_OK 'Date=$0/$1/$2 ($3)$\nTime=$4:$5:$6' | MessageBox MB_OK 'Date=$0/$1/$2 ($3)$\nTime=$4:$5:$6' | ||
SectionEnd | SectionEnd | ||
</highlight-nsis> | |||
;- | <b>Example (Get system time):</b> | ||
<highlight-nsis>Section | |||
${GetTime} "" "LS" $0 $1 $2 $3 $4 $5 $6 | |||
; $0="01" day | |||
; $1="04" month | |||
; $2="2005" year | |||
; $3="Friday" day of week name | |||
; $4="11" hour | |||
; $5="05" minute | |||
; $6="50" seconds | |||
MessageBox MB_OK 'Date=$0/$1/$2 ($3)$\nTime=$4:$5:$6' | |||
SectionEnd | |||
</highlight-nsis> | |||
<b>Example (Convert time to 12-hour format AM/PM):</b> | |||
<highlight-nsis>Section | |||
${GetTime} "" "L" $0 $1 $2 $3 $4 $5 $6 | |||
StrCmp $4 0 0 +3 | |||
StrCpy $4 12 | |||
goto +3 | |||
StrCmp $4 12 +5 | |||
IntCmp $4 12 0 0 +3 | |||
StrCpy $7 AM | |||
goto +3 | |||
IntOp $4 $4 - 12 | |||
StrCpy $7 PM | |||
MessageBox MB_OK 'Date=$0/$1/$2 ($3)$\nTime=$4:$5:$6 $7' | |||
SectionEnd | |||
</highlight-nsis> | |||
== Function Code == | |||
<highlight-nsis> | |||
Function GetTime | Function GetTime | ||
!define GetTime `!insertmacro GetTimeCall` | !define GetTime `!insertmacro GetTimeCall` | ||
Line 106: | Line 146: | ||
Push $5 | Push $5 | ||
Push $6 | Push $6 | ||
Push $7 | |||
ClearErrors | ClearErrors | ||
StrCmp $1 'L' gettime | StrCmp $1 'L' gettime | ||
System::Call '*(i,l,l,l,i,i,i,i,&t260,&t14) i . | StrCmp $1 'A' getfile | ||
System::Call 'kernel32::FindFirstFileA(t,i)i(r0, | StrCmp $1 'C' getfile | ||
StrCmp $1 'M' getfile | |||
System::Call 'kernel32::FindClose(i)i( | StrCmp $1 'LS' gettime | ||
StrCmp $1 'AS' getfile | |||
StrCmp $1 'CS' getfile | |||
StrCmp $1 'MS' getfile | |||
goto error | |||
getfile: | |||
IfFileExists $0 0 error | |||
System::Call /NOUNLOAD '*(i,l,l,l,i,i,i,i,&t260,&t14) i .r6' | |||
System::Call /NOUNLOAD 'kernel32::FindFirstFileA(t,i)i(r0,r6) .r2' | |||
System::Call /NOUNLOAD 'kernel32::FindClose(i)i(r2)' | |||
gettime: | gettime: | ||
System::Call '*(&i2,&i2,&i2,&i2,&i2,&i2,&i2,&i2) i . | System::Call /NOUNLOAD '*(&i2,&i2,&i2,&i2,&i2,&i2,&i2,&i2) i .r7' | ||
StrCmp $1 'L' 0 filetime | StrCmp $1 'L' 0 systemtime | ||
System::Call 'kernel32:: | System::Call /NOUNLOAD 'kernel32::GetLocalTime(i)i(r7)' | ||
goto convert | |||
systemtime: | |||
StrCmp $1 'LS' 0 filetime | |||
System::Call /NOUNLOAD 'kernel32::GetSystemTime(i)i(r7)' | |||
goto convert | goto convert | ||
filetime: | filetime: | ||
System::Call '*$ | System::Call /NOUNLOAD '*$6(i,l,l,l,i,i,i,i,&t260,&t14)i(,.r4,.r3,.r2)' | ||
System::Free /NOUNLOAD $6 | |||
StrCmp $1 'A' 0 +3 | StrCmp $1 'A' 0 +3 | ||
StrCpy $ | StrCpy $2 $3 | ||
goto | goto tolocal | ||
StrCmp $1 'C' 0 +3 | StrCmp $1 'C' 0 +3 | ||
StrCpy $4 $ | StrCpy $2 $4 | ||
goto | goto tolocal | ||
StrCmp $1 ' | StrCmp $1 'M' tolocal | ||
System::Call 'kernel32::FileTimeToLocalFileTime(*l,*l)i( | |||
System::Call 'kernel32::FileTimeToSystemTime(*l,i)i(r3, | StrCmp $1 'AS' tosystem | ||
StrCmp $1 'CS' 0 +3 | |||
StrCpy $3 $4 | |||
goto tosystem | |||
StrCmp $1 'MS' 0 +3 | |||
StrCpy $3 $2 | |||
goto tosystem | |||
tolocal: | |||
System::Call /NOUNLOAD 'kernel32::FileTimeToLocalFileTime(*l,*l)i(r2,.r3)' | |||
tosystem: | |||
System::Call /NOUNLOAD 'kernel32::FileTimeToSystemTime(*l,i)i(r3,r7)' | |||
convert: | convert: | ||
System::Call '*$ | System::Call /NOUNLOAD '*$7(&i2,&i2,&i2,&i2,&i2,&i2,&i2,&i2)i(.r5,.r6,.r4,.r0,.r3,.r2,.r1,)' | ||
System::Free $7 | |||
IntCmp $0 9 0 0 +2 | IntCmp $0 9 0 0 +2 | ||
Line 168: | Line 235: | ||
error: | error: | ||
SetErrors | |||
StrCpy $0 '' | StrCpy $0 '' | ||
StrCpy $1 '' | StrCpy $1 '' | ||
Line 175: | Line 243: | ||
StrCpy $5 '' | StrCpy $5 '' | ||
StrCpy $6 '' | StrCpy $6 '' | ||
end: | end: | ||
Pop $7 | |||
Exch $6 | Exch $6 | ||
Exch | Exch |
Latest revision as of 07:20, 7 February 2006
Author: Instructor (talk, contrib) |
Page for NSIS 2.07 and below users
You can use the latest version of headers (recommended) or the following function code (put the function code in your script before calling it)
Function Description
____________________________________________________________________________ GetTime v1.5 ____________________________________________________________________________ Thanks Takhir (Script "StatTest") and deguix (Function "FileModifiedDate") Features: 1. Get local or system time. 2. Get file time (access, creation and modification). Syntax: ${GetTime} "[File]" "[Option]" $var1 $var2 $var3 $var4 $var5 $var6 $var7 "[File]" ; Ignored if "L" or "LS" ; "[Option]" ; [Options] ; L Local time ; A last Access file time ; C Creation file time ; M Modification file time ; LS System time (UTC) ; AS last Access file time (UTC) ; CS Creation file time (UTC) ; MS Modification file time (UTC) ; $var1 ; Result1: day $var2 ; Result2: month $var3 ; Result3: year $var4 ; Result4: day of week name $var5 ; Result5: hour $var6 ; Result6: minute $var7 ; Result7: seconds Note: -Error flag if file isn't exist -Error flag if syntax error
Example (Get local time):
Section ${GetTime} "" "L" $0 $1 $2 $3 $4 $5 $6 ; $0="01" day ; $1="04" month ; $2="2005" year ; $3="Friday" day of week name ; $4="16" hour ; $5="05" minute ; $6="50" seconds MessageBox MB_OK 'Date=$0/$1/$2 ($3)$\nTime=$4:$5:$6' SectionEnd
Example (Get file time):
Section ${GetTime} "$WINDIR\Explorer.exe" "C" $0 $1 $2 $3 $4 $5 $6 ; $0="12" day ; $1="10" month ; $2="2004" year ; $3="Tuesday" day of week name ; $4="2" hour ; $5="32" minute ; $6="03" seconds IfErrors 0 +2 MessageBox MB_OK "Error" IDOK +2 MessageBox MB_OK 'Date=$0/$1/$2 ($3)$\nTime=$4:$5:$6' SectionEnd
Example (Get system time):
Section ${GetTime} "" "LS" $0 $1 $2 $3 $4 $5 $6 ; $0="01" day ; $1="04" month ; $2="2005" year ; $3="Friday" day of week name ; $4="11" hour ; $5="05" minute ; $6="50" seconds MessageBox MB_OK 'Date=$0/$1/$2 ($3)$\nTime=$4:$5:$6' SectionEnd
Example (Convert time to 12-hour format AM/PM):
Section ${GetTime} "" "L" $0 $1 $2 $3 $4 $5 $6 StrCmp $4 0 0 +3 StrCpy $4 12 goto +3 StrCmp $4 12 +5 IntCmp $4 12 0 0 +3 StrCpy $7 AM goto +3 IntOp $4 $4 - 12 StrCpy $7 PM MessageBox MB_OK 'Date=$0/$1/$2 ($3)$\nTime=$4:$5:$6 $7' SectionEnd
Function Code
Function GetTime !define GetTime `!insertmacro GetTimeCall` !macro GetTimeCall _FILE _OPTION _R1 _R2 _R3 _R4 _R5 _R6 _R7 Push `${_FILE}` Push `${_OPTION}` Call GetTime Pop ${_R1} Pop ${_R2} Pop ${_R3} Pop ${_R4} Pop ${_R5} Pop ${_R6} Pop ${_R7} !macroend Exch $1 Exch Exch $0 Exch Push $2 Push $3 Push $4 Push $5 Push $6 Push $7 ClearErrors StrCmp $1 'L' gettime StrCmp $1 'A' getfile StrCmp $1 'C' getfile StrCmp $1 'M' getfile StrCmp $1 'LS' gettime StrCmp $1 'AS' getfile StrCmp $1 'CS' getfile StrCmp $1 'MS' getfile goto error getfile: IfFileExists $0 0 error System::Call /NOUNLOAD '*(i,l,l,l,i,i,i,i,&t260,&t14) i .r6' System::Call /NOUNLOAD 'kernel32::FindFirstFileA(t,i)i(r0,r6) .r2' System::Call /NOUNLOAD 'kernel32::FindClose(i)i(r2)' gettime: System::Call /NOUNLOAD '*(&i2,&i2,&i2,&i2,&i2,&i2,&i2,&i2) i .r7' StrCmp $1 'L' 0 systemtime System::Call /NOUNLOAD 'kernel32::GetLocalTime(i)i(r7)' goto convert systemtime: StrCmp $1 'LS' 0 filetime System::Call /NOUNLOAD 'kernel32::GetSystemTime(i)i(r7)' goto convert filetime: System::Call /NOUNLOAD '*$6(i,l,l,l,i,i,i,i,&t260,&t14)i(,.r4,.r3,.r2)' System::Free /NOUNLOAD $6 StrCmp $1 'A' 0 +3 StrCpy $2 $3 goto tolocal StrCmp $1 'C' 0 +3 StrCpy $2 $4 goto tolocal StrCmp $1 'M' tolocal StrCmp $1 'AS' tosystem StrCmp $1 'CS' 0 +3 StrCpy $3 $4 goto tosystem StrCmp $1 'MS' 0 +3 StrCpy $3 $2 goto tosystem tolocal: System::Call /NOUNLOAD 'kernel32::FileTimeToLocalFileTime(*l,*l)i(r2,.r3)' tosystem: System::Call /NOUNLOAD 'kernel32::FileTimeToSystemTime(*l,i)i(r3,r7)' convert: System::Call /NOUNLOAD '*$7(&i2,&i2,&i2,&i2,&i2,&i2,&i2,&i2)i(.r5,.r6,.r4,.r0,.r3,.r2,.r1,)' System::Free $7 IntCmp $0 9 0 0 +2 StrCpy $0 '0$0' IntCmp $1 9 0 0 +2 StrCpy $1 '0$1' IntCmp $2 9 0 0 +2 StrCpy $2 '0$2' IntCmp $6 9 0 0 +2 StrCpy $6 '0$6' StrCmp $4 0 0 +3 StrCpy $4 Sunday goto end StrCmp $4 1 0 +3 StrCpy $4 Monday goto end StrCmp $4 2 0 +3 StrCpy $4 Tuesday goto end StrCmp $4 3 0 +3 StrCpy $4 Wednesday goto end StrCmp $4 4 0 +3 StrCpy $4 Thursday goto end StrCmp $4 5 0 +3 StrCpy $4 Friday goto end StrCmp $4 6 0 error StrCpy $4 Saturday goto end error: SetErrors StrCpy $0 '' StrCpy $1 '' StrCpy $2 '' StrCpy $3 '' StrCpy $4 '' StrCpy $5 '' StrCpy $6 '' end: Pop $7 Exch $6 Exch Exch $5 Exch 2 Exch $4 Exch 3 Exch $3 Exch 4 Exch $2 Exch 5 Exch $1 Exch 6 Exch $0 FunctionEnd