IClockSource Example

This example uses the IClockSource interface of a PISDK Server object in a DynamicTime object to display current time for both the server and client. The server time in the server time zone is also shown.

Build the IClockSource example as follows

  1. Create a VB project with a form.
  2. Add a reference to the PI-SDK and PITimeServer.
  3. Create a text box (named text1) for entering the server name.
  4. Create a command button (named command1) next to the text box.
  5. Create four labels (named label1 to label4). They are for the server time, the the client time, the server time in it's time zone and the last is for messages.

Create the following code for the form.

Option Explicit

Private Sub CStime()
On Error GoTo eHandler
Dim srvItem As Server, srvSrc As IClockSource
Dim dTSrv As New DynamicTime, dyT2 As New DynamicTime
  
  Me.MousePointer = vbHourglass
  Set srvItem = PISDK.Servers(Text1.Text)
  ' Use default user to open a connection to the server.
  srvItem.Open
  Set srvSrc = srvItem
  
  ' Set up a dynamic time to use the server time
  ' and the other is left to use the local time.
  dTSrv.SetClockSource srvItem
  ' Also could use the arg srvSrc instead of srvItem
  
  ' Display the server and local time in the default TZ
  Label1.Caption = dTSrv.OutputString & " " & srvSrc.Name _
     & " time"
  Label2.Caption = dyT2.OutputString & " Local time"
  ' Display the server time in its own TZ
  Set dTSrv.TimeZoneInfo = srvSrc.TimeZoneInfo
  dTSrv.FormatString = "yyyy-MM-dd hh:mm:ss.### z"
  Label3.Caption = dTSrv.OutputString & " " & srvSrc.Name _
     & " time in the server TZ"
  Label4.Caption = Err.Description
  
  Me.MousePointer = vbDefault
  Exit Sub
eHandler:
  Me.MousePointer = vbDefault
  Label4.Caption = Err.Description
End Sub

Private Sub Command1_Click()
  CStime
End Sub

Private Sub Form_Load()
  ' At startup, add the current default server time
  Text1.Text = PISDK.Servers.DefaultServer
  CStime
End Sub

Enabling Operational Intelligence