Page 1 of 1 1
Topic Options
#199640 - 2010-08-23 10:27 PM Adding x minutes to current time
Georges_K Offline
Getting the hang of it

Registered: 2005-02-17
Posts: 83
Loc: Chino, CA
Hello all,

I'm struggling with a small detail regarding time manipulation.
I have a script that I need to launch after a certain number of minutes given a specified time.
So let's say the user logged in, and the time was 13:05. I'm looking to schedule a task at 13:07 with the "AT" command.

Is there an easy way to do this? I looked at all the UDFs, and most of them are converting cTime to Kixtime (which I'm not even sure what the difference is with those).

If you can provide a code snipper to do this, that would be most helpful!

Thank you!
_________________________
Network Specialist
Chino Unified School District

Top
#199641 - 2010-08-23 10:40 PM Re: Adding x minutes to current time [Re: Georges_K]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4567
Loc: USA
I've never used it, but the example in TimeConvert() appears to be close to what you want...

TimeConvert() -
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=173022

You could also just split() the time on ":" and rebuild the time in your AT. This would require some error checking but would work...

For example:
 Code:
? @time
$time=split(@time,":")
$hrs=$time[0]
$mins=$time[1]
$secs=$time[2]


? $mins

Top
#199642 - 2010-08-23 11:25 PM Re: Adding x minutes to current time [Re: Georges_K]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
TimeConvert is exactly what you want..
 Code:
; What is the time 3 minutes from now 
$Time = TimeConvert(TimeConvert(@DATE + ' ' + @TIME) + 180)
A "cTime" is a value that is the number of seconds that have elapsed since a particular date/time. It makes time calculations VERY easy.

Convert the current time to cTime format and you have a number. If you want the time 3 minutes from now, you add 180 seconds (3 * 60) to that. Convert the cTime value back to a Date/Time format and you have the desired time. The example above does that with recursive calls to TimeConvert - the inner call returns the cTime, the outer passes the value back, with 180 added to it.

TimeConvert does just that - you give it a Date & Time and it returns a cTime. Give it a cTime and it returns Date & Time. Can't be easier!

The most you'll need to do is to split the time and date parts. If you choose to split a time value into component parts to manipulate, you'll need to take overflow into account - 58 minutes + 3 minutes is NOT 61 minutes, but 1 hour and 1 minute.. TimeConvert avoids that overhead.

You might also want to look at the jt.exe and tcLib library for manipulating scheduled tasks. The "at" command has been depracated since Windows 2000 due to security vulnerabilities. Both tcLib and ScheduleTask() allow simple creation of tasks from within Kix. tcLib is a more advanced library of UDFs that allows you to not only create, but edit, query, run/terminate and delete tasks. Just put the jt.exe in the system Path, load the tcLib library, and make 4 short UDF calls to create a task.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#199643 - 2010-08-24 06:39 AM Re: Adding x minutes to current time [Re: Glenn Barnas]
Georges_K Offline
Getting the hang of it

Registered: 2005-02-17
Posts: 83
Loc: Chino, CA
Glenn, Allen

Thank you very much for the example, as well as the clarification about cTime. that makes a lot of sense, and indeed renders manipulation of time much simpler.

I did end up using Allen's method, by splitting the @TIME macro, and then I just did a couple of case statements within that function to take care of the 58, 59, 00, and the jump from 23 to 00. Since in this particular case, I'm only looking at adding two minutes, this exception wasn't too bad. For the longer term, TimeConvert is definitely what I'll be using.

I had also seen tcLib, (haven't seen jt.exe though). I need to look closer into those, as they were a little bit more complex (if my memory serves me right) than the AT. I guess the fact that AT is being deprecated, explains the fact that it can no longer be easily run with the /INTERACTIVE on Vista and Windows 7. (Fortunately the current OS i'm working with is XP). Something to plan for the future.

Again, thank you for your great advice as usual!
_________________________
Network Specialist
Chino Unified School District

Top
#199644 - 2010-08-24 01:17 PM Re: Adding x minutes to current time [Re: Georges_K]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
As bad as AT is (from a security perspective) there is a Windows Resource Kit tool called SOON that duplicates the AT command except that the Time parameter defines "seconds from now" instead of an exact time. Something to consider for this application.
 Code:
soon.ex 120 /interactive "command"
Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#199645 - 2010-08-24 01:42 PM Re: Adding x minutes to current time [Re: Glenn Barnas]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Georges,

tcLib just looks intimidating.. ;\) It's actually fairly easy to use. With just four lines of code - init, defineTask, defineTrigger, and setEvent, you can create or update a task event anywhere.

The ZIP file with the tcLib package includes jt.exe and a short Programmer's Reference Guide with several examples. To accomplish what you need, you'd put JT into the C:\Windows folder (or - better - another folder that is defined in the PATH), put tclib.udf on your system, and create the following code:
 Code:
Call '.\tclib.udf'
$aWhen = Split(TimeConvert(TimeConvert(@DATE + ' ' + @TIME) + 120), ' ')
tcInit()      ; init the library, prepare the arrays
$Rc = tcDefineTask('APP=command PRM=arg list USR=userid PWD=password')
$Rc = tcDefineTrigger(0, 'TYP=Once STM=' + $aWhen[1] + ' SDT=' + $aWhen[0])
If tcSetEvent(@WKSTA, 'RunSoon')
  'Success!' ?
Else
  'Failed to create task: ' tcErrMsg(@ERROR) ?
EndIf
The first line creates a 2-element array containing the date and time 2 minutes from now. This uses the TimeConvert example shown earlier wrapped in a Split to separate the date and time components.

The tcDefineTask defines the command, its arguments, and the credentials used to run the task. This is known as the "TASK" part of the scheduled "EVENT". Note that JT does not support Interactive processing as this was eliminated in W2K3 and later releases of XP to close security issues. You might want to add "DWD=1" to delete the task event when it is complete.

tcDefineTrigger defines when the task will run. It defines the type of trigger (run once) and the date & time as provided by the 2 array elements.

tcSetEvent creates or updates the event on the target system, calling it "RunSoon". Unlike AT jobs which have job numbers, task events can have meaningful names.

If an error occurs, the @ERROR macro is passed to tcErrMsg to return a message that is specific to the task event. This function will return standard @SERROR messages unless the error code has special meaning to JT, in which case a specific message will be returned instead.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#199654 - 2010-08-25 06:29 PM Re: Adding x minutes to current time [Re: Glenn Barnas]
Georges_K Offline
Getting the hang of it

Registered: 2005-02-17
Posts: 83
Loc: Chino, CA
That's excellent Glenn! Thank you for the clarification on the use of tcLib. I will take a look at it. Now that you explained it this way, it's much more understandable :), and soon.exe is a new command I didn't know of.
Live'n'learn!

Thanks again!!
_________________________
Network Specialist
Chino Unified School District

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 320 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.028 seconds in which 0.011 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org