Function equal to java System.currentTimeMillis()?

Hi all,

I am trying to find a lotusscript funciton does the job of java System.currentTimeMillis()… but could not find any. I searched the forum and found this code segment written by Jan Van Puyvelde…

E = Evaluate({@Text(DateTimeField; “*”)}, doc)

cs% = Clng(“&H” & Right$(E(0), 8)) Mod 100

I found the output are very different… java returns a much longer (and UNIQUE) number like 1127259106232 which is what I need. As a newbi to lotusscript I don’t understand Jan’s code, so I don’t know if I could actually modify it to do the currentTimeMillis()'s job…

Any suggestion?

Henry

Subject: function equal to java System.currentTimeMillis()?

I’m not a java developer, so I’m not familiar with the function. What are you trying to achieve? (I presume you need timing to about a millisecond)

Perhaps this thread will help:

http://www-10.lotus.com/ldd/46dom.nsf/55c38d716d632d9b8525689b005ba1c0/63914bb4871cec9285256b6e0037d12e?OpenDocument

Subject: RE: function equal to java System.currentTimeMillis()?

Hi Michelle,

Thank you for your reply.

What I am trying to do is use the millisccond number returned by currentTimeMillis() to compose a guid. Why not use @Unique? simply because I need the id to be in some other format :slight_smile:

currentTimeMillis() returns the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC. which is a unique number.

LSI_THREAD_TICKS, as far as I read from the help file(I don’t know much domino), Get current clock ticks after the thread started ? (is that true? what thread? btw) which is not a unique number…

Please correct me if I am wrong. Or if you have any other suggestion, please let me know

Thank you

Subject: RE: function equal to java System.currentTimeMillis()?

How frequently do you need to create a unique ID? Could you generate it using Now()? Or is that not accurate enough for your purposes?

Subject: RE: function equal to java System.currentTimeMillis()?

Michelle,

Actually, not very frequently. I tried to put @Now in Jan’s code and it gave me a 2 digits number.(I have no idea how it works :P) I dont need it to be too precise. centisecond should be enough…

I think the real problem is I don’t know how to get a 13 digits number(like the one java function returns) out of @Now or Now(). The help file didn’t provide enough details… Any suggestion?

I really wish I learnt vb, it seems LotusScript is very similar to vb :frowning:

Thanks

Subject: RE: function equal to java System.currentTimeMillis()?

Jan’s formula is only returning the hundredths of a second.

What it does (using @Now which doesn’t require the doc parameter) :

E = Evaluate({@Text(@Now; “*”)} )

cs% = Clng(“&H” & Right$(E(0), 8)) Mod 100

  1. @Text converts the current time to text. The “*” parameter causes it to be converted as a Hex string, which includes hundredths of a second. If you examine it in the debugger, you should see something like this:

CA257083:00198BDA

The first part of the string is the date, the second part is the time since midnight. I can’t remember exactly how the date is stored but I know the first two digits represent the time-zone offset.

  1. Right(E(0), 8) extracts the last 8 characters (i.e. the time component)

  2. Clng (“&H”& val ) converts the time from Hex to a (long) number - again this is hundredths of a seconds since midnight (I think midnight GMT in fact)

  3. mod returns the remainder after dividing by 100

I think what you need may be:

Dim E As Variant

Dim daynum As Long

Dim timenum As Long

Dim id As String





E = Evaluate({@Text(@Now; "*")} )



daynum = Clng( "&H" & Mid$(E(0), 3,6) ) ( 6 characters, starting at char 3 )

timenum = Clng(  "&H" & Right$(E(0), 8)) (last eight characters)



ID = Cstr( daynum ) + Cstr( timenum )

The number I am getting here from the date portion is way to large, but hopefully I have given you enough to modify to suite your purposes

Subject: RE: function equal to java System.currentTimeMillis()?

Hi Michelle

Thank you very much! Everything becomes crystal clear after I read your explanation. I will give it a try :slight_smile:

Henry

Subject: function equal to java System.currentTimeMillis()?

LotusScript’s Timer function is probably the nearest equivalent to currentTimeMillis().

for x=1 to 1000

print Timer

Next x

Subject: RE: function equal to java System.currentTimeMillis()?

If you happen to be using Win/32, you can use “GetLocalTime” as follows:

Public Type sysTimeTYPE

wYear As Integer

wMonth As Integer

wDayOfWeek As Integer

wDay As Integer

wHour As Integer

wMinute As Integer

wSecond As Integer

wMilliseconds As Integer

End Type

Declare Sub W32GetLocalTime Lib “kernel32” Alias “GetLocalTime” ( _

lpSystemTime As sysTimeTYPE

)

Dim localtime As sysTimeTYPE

Dim strMsgTxt As String

Call W32GetLocalTime(localtime)

strMsgTxt$ = _

"Year: " & localtime.wYear & Chr(13) _

& "Month: " & localtime.wMonth & Chr(13) _

& "Day of week: " & localtime.wDayOfWeek & Chr(13) _

& "Day: " & localtime.wDay & Chr(13) _

& "Hour: " & localtime.wHour & Chr(13) _

& "Minute: " & localtime.wMinute & Chr(13) _

& "Second: " & localtime.wSecond & Chr(13) _

& "Millisecond: " & localtime.wMilliseconds

Msgbox strMsgTxt$, , “Results . . .”

But a more platform independent option is to use Getthreadinfo:

Const LSI_THREAD_TICKS_PER_SEC = 7 '// see lsprcval.lss

Const LSI_THREAD_TICKS = 6 '// see lsprcval.lss

Dim dblTickCount As Double

Dim intTicksPerSecond As Integer

intTicksPerSecond% = Getthreadinfo(LSI_THREAD_TICKS_PER_SEC)

dblTickCount# = Getthreadinfo(LSI_THREAD_TICKS)

Print "Ticks per second: " & intTicksPerSecond%

Print "Ticks lapsed: " & dblTickCount# '// on Win/32, this is essentially milliseconds but other platforms differ

Print "Seconds lapsed: " & dblTickCount# / intTicksPerSecond%

Print "Minutes lapsed: " & (dblTickCount# / intTicksPerSecond%) / 60

Print "Hours lapsed: " & (dblTickCount# / intTicksPerSecond%) / (60^2)

On Windows, the above code reports ticks lapsed since the current machine was started.

hth,

dgg

Subject: RE: function equal to java System.currentTimeMillis()?

Hi Dallas,

Thank you, excellent example!

Unfortunately, we are using Unix and “the ticks lapsed since the current machine was started” is not good enough for my application since there are chances of same ticks number returned. I know the chance is very small, but… you know… :stuck_out_tongue:

I am thinking about to use Now() although it is not very precise… or just use the second code segment you posted if I run out of coffee and still couldn’t find a solution myself.

Thank you again :wink:

Subject: RE: function equal to java System.currentTimeMillis()?

If you really need to, I suppose you could use LS2J. The function below actually works faster than I figured it would.

dgg

Option Declare

Uselsx “*javacon”

Function getTimeInMillis(pdblTimeOut As Double) As Boolean

On Error Goto errorHandler



Dim jSess As JavaSession

Dim jClss As JavaClass

Dim jMethod As JavaMethod



getTimeInMillis = False

Set jSess = New JavaSession()

Set jClss = jSess.GetClass("java/lang/System")

Set jMethod = jClss.GetMethod("currentTimeMillis", "()J")



pdblTimeOut# = jMethod.Invoke()

getTimeInMillis = True

functionExit:

If Not(jMethod Is Nothing) Then

	Delete jMethod

End If



If Not(jClss Is Nothing) Then

	Delete jClss

End If



If Not(jSess Is Nothing) Then

	Delete jSess

End If

Exit Function

errorHandler:

Msgbox "Error " & Err & ": " & Error$ & " at line " & Erl & " of " & Getthreadinfo(1) & ".", , "Error . . ."

Print "Error " & Err & ": " & Error$ & " at line " & Erl & " of " & Getthreadinfo(1) & " . . ."

Resume functionExit

End Function