Time and Date

In some cases you may want to set the system time or date back before starting your portable and restoring the current time and date a few seconds later. In the example below we’ll set the date back 5 years and restore the actual date after 3 seconds. Thus it won’t do any harm to your system (hopefully).

This is not a common thing that you would use in your portables but I’ll show you how to do that, maybe it will come handy to you.

To be able to do this, you’ll have to modify the NSIS script to include the Time plugin. Although the plugin is in the right place (“ToolsNSIS 2.17Plugins” and “ToolsNSIS 2.17Include” directories, you have to put this line in your script under the other Includes:

;—-Includes—-

!include “Registry.nsh”

!include “Time.nsh”

Now comes the fun part. First you have to understand the main steps:

1. Store the current local date to $0 variable
2. Set any number in $1 variable (in the example below it is 5, which means 5 years now)
3. Compute the new date and store it in $2
4. Set the new local date based on $2
5. Sleep 1 seconds
6. Start main exe, than Sleep 2 seconds
7. Set the number you’ve set in Step 2 in $3 (you can use $1 again if you wish instead of $3)
8. Compute new date ($2 date + 3 seconds which is the result of the two sleeps) and store it in $4
9. Set the new local date (which is in fact the current date)
10. Unload the Time plugin

And here’s the code (change the “Application.exe” to your application’s name):

Section “Main”

${time::GetLocalTime} $0

StrCpy $1 5

${time::MathTime} “date($0) – date(0.0.$1 0:0:0) = date” $2

${time::SetLocalTime} “$2″ $R0

Sleep 1000

Exec “$EXEDIRApplication.exe”

Sleep 2000

StrCpy $3 5

${time::MathTime} “date($2) + date(0.0.$3 0:0:03) = date” $4

${time::SetLocalTime} “$4″ $R0

Sleep 200

${time::Unload}

SectionEnd

Of course, the new time may differ from the current time in some milliseconds but I think it is not a big deal.

This script may come handy if the software checks the current time and it doesn’t start if the date exceeds a specific date (for example when the trial period is over). You can alter the script the way you want, of course.

As I said, you will probably not use this script when creating your portables. But it shows that NSIS can cope with some other things also beside what I showed you earlier. Actually this is another script I get from the NSIS forum, but of course I had to modify it a bit. So if you have a problem that needs to be solved with NSIS, just go to the forum and perform a search there. If you’ve found nothing, you may ask the gurus there about it, I’m sure they’ll come up with an answer or recommend you another way to solve the problem.

One response to “Time and Date”

  1. wilsont3ch.com » How To Make Portables

    [...] 10. Time and Date [...]