AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Script to set sytemtime and systemdate.

 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
littlebut0



Joined: 22 Dec 2005
Posts: 49
Location: Belgium

PostPosted: Sun Oct 01, 2006 10:29 pm    Post subject: Script to set sytemtime and systemdate. Reply with quote

Recently I needed a script which would be capable of setting the system time and the system date on any usersmachine. I searched the forum but did not find any working script. I wrote the following script which you can download at http://users.skynet.be/amnestylanden/downloads/setsystemtime.zip
The script needs the following three files be installed in the same directory as the script directory (all three of them included in zipfile):
cmdret.dll, cmdstub.exe en yes.asd.
The meaning of the file yes.asd is explained in the script.


Code:
inputdate:
Gui, Font, S12 CDefault, Verdana
Gui, Add, Text, x106 y20 w160 h20, Set system time
Gui, Font, S8 CDefault, Verdana
Gui, Add, Text, x26 y80 w170 h20 , New system time (HH:mm)
Gui, Add, Text, x26 y120 w170 h20 , New date (yyyy-MM-dd)
Gui, Add, Edit, x206 y80 w130 h20 vNewtime,
Gui, Add, Edit, x206 y120 w130 h20 vNewdate,
Gui, Add, Button, x236 y160 w100 h20 gOK default, OK
Gui, Show, x553 y353 h200 w369, Set System Time
Return

GuiClose:
ExitApp



OK:
gui,submit,nohide
timeToSet =   


; ----------check if entered date and time are valuable -------------------------------------
if newdate
   {
   format := chkdateformat(newdate)
   if format
      value := chkdatevalue(newdate)
   if not format||not value
      {
      msgbox You typed %newdate% as the new date.`nPlease enter a valid date (between 1990-01-01 and 2100-12-31)`nMake sure you use the correct format: yyyy-MM-dd
      guicontrol,1:,newdate,
      return
      }
   }

if newtime
   {
   format := chktimeformat(newtime)
   if format
      value := chktimevalue(newtime)
   if not format||not value
      {
      msgbox You typed %newtime% as the new time.`nPlease enter a valid time (between 00:00 and 23:59)`nMake sure you use the correct format: 24H:mm
      guicontrol,1:,newtime,
      return
      }
   }   
; ----------check if entered date and time are valuable -------------------------------------


gui,destroy
if not newtime &&  not newdate
   {
   msgbox You entered nothing.`nThe system time was not changed.
   exitapp
   }

if not newtime && newdate     ; we want to change systemdate but not systemtime
   {
   formattime,hms,%A_now%,HHmmss
   stringreplace,newdate,newdate,-,,1
   timeToSet = %newdate%%hms%
   }
   
if not newdate &&  newtime   ; we want to change systemtime but not systemdate
   {
   formattime,ymd,%A_now%,yyyyMMdd
   stringreplace,newtime,newtime,:,
   timeToSet = %ymd%%newtime%
   }

if newtime && newdate  ;we want to change both systemtime and systemdate
   {
   stringreplace,newtime,newtime,:,
   stringreplace,newdate,newdate,-,,1
   timeToSet = %newdate%%newtime%00
   }


   
if A_ostype = WIN32_WINDOWS   ;(win98)
   {
   today := cmdret("cmdstub.exe command.com /c date < yes.asd")
         ;yes.asd = a textdocument containing only a <enter-keystroke>
         ;if you open it in notepad or another texteditor, you won't see anything, it seems as if
         ;yes.asd is an empty document, but it does contain the enter keystroke.
         ;by means of the "<-symbol" we transfer the contents of this textdocument to the commandline
         ;which is equivalent as pressing the enter-key after the date command is executed
   
   ;msgbox %today%
   }
else  ;winNT; win2K; winXP
{
today := cmdret(comspec " /c date < yes.asd")
;msgbox %today%   ;today holds the current format and value of the systemdate   
}
stringsplit,today,today,`n
stringreplace,today1,today1,`r,,1
stringright,char,today1,1
if (char = A_space)
stringtrimright,today1,today1,1
stringright,today,today1,10   
   
ifinstring,today,-
goto minus
ifinstring,today,:
goto divide
ifinstring,today,.
goto point
ifinstring,today,/
goto slash

minus:
char = -
;msgbox minus
; -------------check if 15-11-2005 is a valid date on this system ---------------
if A_ostype = WIN32_WINDOWS
format := CMDret("cmdstub.exe command.com /c date 15-11-2005 < yes.asd ")
else
   {
   format := CMDret(comspec " /c date 15-11-2005 < yes.asd ")
   }
if format =         ; if format is empty, ths system accepted 15-11-2005 as a valid date
               ; so now we know the date format of the system
               ; if this string isn't empty, the system rejected the date
               ; and we'll try a new dateformat on this system
   {
   format = ddmmyyyy
   runwait, %comspec% /c date %today%,,hide
   goto settime
   }
; ------------check if 15-11-2005 is a valid date on this system ----------------




; ------------check if 11-15-2005 is a valid date on this system ----------------
else
   {
   if A_ostype = WIN32_WINDOWS
   format := CMDret("cmdstub.exe command.com /c date 11-15-2005 < yes.asd ")
   else
      {
      format := CMDret(comspec " /c date 11-15-2005 < yes.asd ")
      }
   if format =
      {
      format = mmddyyyy
;      runwait, %comspec% /c date %today%,,hide
      goto settime   
      }
   }
; ------------check if 11-15-2005 is a valid date on this system ----------------


format = yyyymmdd  ;if both other formats where invalid the format is yyyymmdd
;runwait, %comspec% /c date %today%,,hide
goto settime


divide:
char = :
;msgbox divide
if A_ostype = WIN32_WINDOWS
format := CMDret("cmdstub.exe command.com /c date 15:11:2005 < yes.asd ")
else
   {
   format := CMDret(comspec " /c date 15:11:2005 < yes.asd ")
   }
if format =
   {
   format = ddmmyyyy
;   runwait, %comspec% /c date %today%,,hide
   goto settime
   }
else
   {
   if A_ostype = WIN32_WINDOWS
   format := CMDret("cmdstub.exe command.com /c date 11:15:2005 < yes.asd ")
   else
      {
      format := CMDret(comspec " /c date 11:15:2005 < yes.asd ")
      }
   if format =
      {
      format = mmddyyyy
;      runwait, %comspec% /c date %today%,,hide
      goto settime
      }
   }

format = yyyymmdd
;runwait, %comspec% /c date %today%,,hide
goto settime


point:
char = .
;msgbox point
if A_ostype = WIN32_WINDOWS
format := CMDret("cmdstub.exe command.com /c date 15.11.2005 < yes.asd ")
else
   {
   format := CMDret(comspec " /c date 15.11.2005 < yes.asd ")
   }
if format =
   {
   format = ddmmyyyy
;   runwait, %comspec% /c date %today%,,hide
   goto settime
   }
else
   {
   if A_ostype = WIN32_WINDOWS
   format := CMDret("cmdstub.exe command.com /c date 11.15.2005 < yes.asd ")
   else
      {
      format := CMDret(comspec " /c date 11.15.2005 < yes.asd ")
      }
   if format =
      {
      format = mmddyyyy
;      runwait, %comspec% /c date %today%,,hide
      goto settime
      }
   }

format = yyyymmdd
;runwait, %comspec% /c date %today%,,hide
goto settime


slash:
;msgbox slash
char = /
if A_ostype = WIN32_WINDOWS
format := CMDret("cmdstub.exe command.com /c date 15%char%11%char%2005 < yes.asd ")
else
   {
   format := CMDret(comspec " /c date 15/11/2005 < yes.asd ")
   }
if format =
   {
   format = ddmmyyyy
;   runwait, %comspec% /c date %today%,,hide
   goto settime
   }
else
   {
   if A_ostype = WIN32_WINDOWS
   format := CMDret("cmdstub.exe command.com /c date 11/15/2005 < yes.asd ")
   else
      {
      format := CMDret(comspec " /c date 11/15/2005 < yes.asd ")
      }
   if format =
      {
      format = mmddyyyy
;      runwait, %comspec% /c date %today%,,hide
      goto settime
      }
   }

format = yyyymmdd
;runwait, %comspec% /c date %today%,,hide



;-------------------settime----------------------------------------
settime:                     
;msgbox settime %format%
if (format = "yyyymmdd")
{
formattime,toSet,%timeToSet%,yyyy%char%MM%char%dd
runwait %comspec% /c date %toSet%,,hide,
formattime,toSet,%timeToSet%,HH:mm:ss
runwait %comspec% /c time %toSet%,,hide
}

if (format = "mmddyyyy")
{
formattime,toSet,%timeToSet%,MM%char%dd%char%yyyy
runwait %comspec% /c date %toSet%,,hide,
formattime,toSet,%timeToSet%,HH:mm:ss
runwait %comspec% /c time %toSet%,,hide,
}

if (format = "ddmmyyyy")
{
formattime,toSet,%timeToSet%,dd%char%MM%char%yyyy
runwait %comspec% /c date %toSet%,,hide,
formattime,toSet,%timeToSet%,HH:mm:ss
runwait %comspec% /c time %toSet%,,hide,
}
;--------------------settime--------------------------------------



;---------------------showtime------------------------------------
showtime:
if (format = "yyyymmdd")
formattime,nu,%A_now%,yyyy%char%MM%char%dd HH:mm
if (format = "mmddyyyy")
formattime,nu,%A_now%,MM%char%dd%char%yyyy HH:mm
if (format = "ddmmyyyy")
formattime,nu,%A_now%,dd%char%MM%char%yyyy HH:mm
msgbox %nu%
;---------------------showtime------------------------------------


exitapp





CMDret(CMD)
{
  VarSetCapacity(StrOut, 10000)
  RetVal := DllCall("cmdret.dll\RunReturn", "str", CMD, "str", StrOut)
  Return, %StrOut%
}

chkdateformat(newdate)
{
stringsplit,newdate,newdate,-
if (strlen(newdate1) <>4 ||strlen(newdate2) <> 2||strlen(newdate3) <> 2||newdate0 <>3)
valuable = 0
else valuable = 1
return valuable
}


chkdatevalue(newdate)
{
leapyear = 0
stringsplit,newdate,newdate,-
a := mod(newdate1,4)
if not a
leapyear = 1
a := mod(newdate1,100)
if not a ; if dividble by 100
   {
   a := mod(newdate1,400)
   if a ; if not dividble by 400
   leapyear = 0
   }
valuable = 1
if ((newdate2=04||newdate2=06||newdate2=09||newdate2=11)&&newdate3=31)
valuable = 0
if (valuable&&newdate2=02&&newdate3>29)
valuable = 0
if (valuable&&newdate2=02&&newdate3=29)
   {
   If leapyear
      valuable = 1
   else valuable = 0
   }
if valuable&&(newdate1<1990||newdate1>2100)
valuable = 0
if (newdate2>12||newdate3>31||newdate2<0||newdate3<0)
valuable = 0
return valuable
}

chktimeformat(newtime)
{
stringsplit,newtime,newtime,:
if (strlen(newtime1) <>2 ||strlen(newtime2) <> 2||newtime0 <>2)
valuable = 0
else valuable = 1
return valuable
}

chktimevalue(newtime)
{
stringsplit,newtime,newtime,:
if (newtime1<00||newtime1>23||newtime2<00||newtime2>59)
valuable = 0
else valuable =1
return valuable
}
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Sun Oct 01, 2006 11:23 pm    Post subject: Reply with quote

Have you tried running %comspec% /c date or time? It could be two lines for setting the date and time to, e.g. the command line parameters, although w/o error check, etc.
Code:
run %comspec% /c date 10-01-06
run %comspec% /c time 19:20:50
Back to top
View user's profile Send private message
littlebut0



Joined: 22 Dec 2005
Posts: 49
Location: Belgium

PostPosted: Sun Oct 01, 2006 11:46 pm    Post subject: Reply with quote

Thqt's something I know Laszlo. But the script is in fact part of a larger script wich monitors attempts of changing system time or system date. I needed a way to restore automaticly the original date and time without the need of a user to enter anything.
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Mon Oct 02, 2006 12:02 am    Post subject: Reply with quote

You probably thought about it, but at boot time Windows saves the system date and time, and your script could periodically check if subtracting A_TickCount from the current time still gives the same startup time. If not, you could reset the system time, or simply periodically set it to the one computed from the startup time by adding A_TickCount. Both of these will ensure that the system time is not tampered with, as long the script is running. It is another issue to prevent the script be closed, or automatically restart it.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group