I think I have a solution, except for the minor problem that it doesn't work! In the SDK documentation for the PITimeServer DLL, it has an example of using the PITimeServer.PITime class:
Dim pt1 As New PITime
pt1.LocalDate = Text1.Text
so from that I sumised that PITime.LocalDate should be able to parse a text string. I then coded this:
Private Sub Logo_Trend_TimeRangeChange(ByVal StartTime As String, ByVal EndTime As String)
Dim dEndTime As PITimeServer.PITime
Dim diffSecs As Integer
Set dEndTime = New PITimeServer.PITime
dEndTime.LocalDate = EndTime '<-- Here it goes 'bang'
diffSecs = DateDiff("s", dEndTime.LocalDate, Now())
but on line 7 I get "Run time error 13: Type Mismatch". Why?!?!? EndTime at this point is "18/08/2009 08:27:12.619", which is exactly the same format as the example in the helpfile.
The problem is that ProcessBook (>= v3.0) returns the time with milliseconds and the Date object in VBA does not like milliseconds so the cast from the string to Date fails. You need to strip off the milliseconds if they exist in your time string.
Thanks, that has worked. Trouble now is that it has not solved the fundamental problem after all, which is that the time strings passed to TimeRangeChange are PI Time strings and can very easily be things like "*-1h". I assumed that the PITime object would be able to handle PI times like this but it does not! How do I convert a PI time string into a VB Date object?
In that case you switch your attention to the PITimeFormat object. Set the time zone info to that of your connected PI server.
Dim PTM As New PITimeFormat
Set PTM.TimeZoneInfo = Srv.PITimeZoneInfo
PTM.InputString = "*-1h"
Yep, that is what I have found. I did not think I wanted PITimeFormat because the help was talking about the server time zone rather than parsing PI time strings. I am not sure whether I need the server PI Time zone or the client. I suppose if I compare with the current time on the server instead of Now() then it would make sense to use the servers time zone settings.
Yep...just like the settings in ProcessBook you can choose the Server time or the Client.
You can use the ServerTime property of a PI server or the PITimeFormat, which you don't have to specify the TimeZoneInfo. By default it uses the box you are running your code on.
Dim ClientPTM As New PITimeFormat
ClientPTM.InputString = "*"
Dim ServerPTM As New PITimeFormat
Set ServerPTM.TimeZoneInfo = Srv.PITimeZoneInfo
ServerPTM.InputString = "*"
Debug.Print "Server = " & ServerPTM.LocalDate
Debug.Print "Client = " & ClientPTM.LocalDate
Do you know if this has been fixed in a later version of ProcessBook?
not sure what you are refering to as a thing that needs to be fixed? The solution is to use PITimeFormat as Rhys has mentioned.