Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compact Framework/PocketPC - Getting/Setting Time 1

Status
Not open for further replies.

zarkon4

MIS
Dec 16, 2003
641
US
I am looking to get date & time from a time server and set/synch the time from within my application. My application will be needing to date-time stamp data and I need to ensure the date and time are accurate with our time server.

Any ideas?
 
Maybe this will help you - example code within. Help others when you can.

Peace out,
TLN1



Option Strict Off
Option Explicit On

Imports System.IO
Imports System.Diagnostics
Imports System.String
Imports System.Buffer
Imports System.Math
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

<StructLayout(LayoutKind.Sequential)> _
Public Structure SYSTEMTIME
Dim wYear As UInt16
Dim wMonth As UInt16
Dim wDayOfWeek As UInt16
Dim wDay As UInt16
Dim wHour As UInt16
Dim wMinute As UInt16
Dim wSecond As UInt16
Dim wMilliseconds As UInt16
End Structure


<DllImport("coredll.dll")> _
Public Function GetLocalTime(ByRef lpSystemTime As SYSTEMTIME) As Boolean
End Function
<DllImport("coredll.dll")> _
Public Function SetLocalTime(ByRef lpSystemTime As SYSTEMTIME) As Boolean
End Function
<DllImport("coredll.dll")> _
Public Function GetSystemTime(ByRef lpSystemTime As SYSTEMTIME) As Boolean
End Function
<DllImport("coredll.dll")> _
Public Function SetSystemTime(ByRef lpSystemTime As SYSTEMTIME) As Boolean
End Function


'EXAMPLE_CODE
'-------------------------------------------------------------------------------------
Dim sHH, sMM, sSS, sMSEC, sXBuf As String
Dim dUTCtime, d24hrTime As Double
Dim iTZoffset As Int32
Dim SysDT As SYSTEMTIME

'First get the HHC system UTC date and time
SRVGetSystemDateTime(SysDT, iTZoffset)
sXBuf = "13.251500" 'A time string in 24 hr HH.MMSSdd format (For example from GPS)
'MsgBox("1 UTC TIME: " & sXBuf, MsgBoxStyle.OKOnly, "DCSoft")
'MsgBox("2 UTC TIME: " & d24hrTime, MsgBoxStyle.OKOnly, "DCSoft")
sHH = sData(1).Substring(0, 2)
sMM = sData(1).Substring(2, 2)
sSS = sData(1).Substring(4, 2)
sMSEC = sData(1).Substring(7, 2)
'Now sync HHC clock with GPS receiver time
SysDT.wHour = Convert.ToUInt16(sHH)
SysDT.wMinute = Convert.ToUInt16(sMM)
SysDT.wSecond = Convert.ToUInt16(sSS)
SysDT.wMilliseconds = Convert.ToUInt16(Convert.ToSingle(sMSEC) * 10)
SRVSetSystemDateTime(SysDT)


'=========================================================================
'=========================================================================
'SRVSetSystemDateTime - Set the Windows CE date and time according to
'To UTC Date and Time
'Inputs:
'xDateTime - The SYSTEMTIME structure object defined previously that contains current
' Date and time info you wish to set
'
'EXAMPLES:
'Dim xDateTm as SYSTEMIME
'Dim iTZoffset as Int32
'
'SRVGetSystemDateTime(xDateTm, iTZoffset) 'WinCE Version
'xDateTime.wMinute= xDateTime.wMinute + 1 'Change time by one minute (Note NO error Checking!!)
'SRVSetSystemDateTime(xDateTm) 'WinCE Version
'
'Returns: - Nothing useful at this time
'
Public Function SRVSetSystemDateTime(ByVal xDateTime As SYSTEMTIME) As Int32
SetSystemTime(xDateTime)
End Function

'=========================================================================
'=========================================================================
'SRVGetSystemDateTime - Get the Windows CE date and time according to
'To UTC Date and Time and return the Time zone offset as well
'Inputs:
'xDateTime - The DateTime structure in which the Date and Time values will
' be returned
'
'EXAMPLES:
'Dim xDateTm as SYSTEMTIME
'Dim iTz as Int32
'SRVGetSystemDateTime(SYSTEMTIME, iTz) 'WinCE Version
'
'Returns: - A systemtime in a structure as well as time zome offset
'
Public Function SRVGetSystemDateTime(ByRef xDateTime As SYSTEMTIME, ByRef iTZoffset As Int32) As Int32
Dim LocalDateTime As SYSTEMTIME

GetSystemTime(xDateTime)
GetLocalTime(LocalDateTime)

iTZoffset = Convert.ToInt32(srvFMOD(Convert.ToDouble(xDateTime.wHour) - Convert.ToDouble(LocalDateTime.wHour), 24.0))

End Function


'=========================================================================
'=========================================================================
'This function closely emulates the fmod function found in C/C++
'Function will handle positive or negative numbers for x but it is
'assumed that y is ALWAYS a positive value

'Example xval= srvFMOD(-225.13334, 360.00) - All Values input must be in decimal
'Upon return xval contains 134.8666

Public Function srvFMOD(ByVal x As Double, ByVal y As Double) As Double
Dim xval As Double

xval = Math.IEEERemainder(x, y)
If xval < 0.0 Then
Return (xval + y)
Else
Return (xval)
End If

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top