Author |
Topic |
|
steve
28 Posts |
Posted - 02 Apr 2003 : 10:51:10
|
I'd like to set the clock on my Bitsy+ from an eVB program, but I can't see any quick way of doing it. Is an API call in order, and is it one that can be used from eVB? Thanks, -Steve
|
Edited by - steve on 02 Apr 2003 10:51:31 |
|
ctacke
877 Posts |
Posted - 02 Apr 2003 : 11:21:28
|
You need to call SetSystemTime or SetLocalTime. Here's a snippet:
Declare Function GetLocalTime Lib "Coredll" (ByVal lpSystemTime As String) As Long Declare Function SetLocalTime Lib "Coredll" (ByVal lpSystemTime As String) As Long
Public Const CE_INTEGER = 2 Public Const CE_LONG = 4
Public Function FromBinaryString(BString As String) As Variant Dim i As Integer Dim bIsNegative As Boolean bIsNegative = False
' start at the end of the string and work backwards For i = LenB(BString) - 1 To 0 Step -1 If i = LenB(BString) - 1 And (AscB(MidB(BString, i + 1, 2)) And &H80) Then ' check the signigicant bit ' If it's negative, set a flag bIsNegative = True End If If bIsNegative = True Then ' extract the binary complement of the byte FromBinaryString = FromBinaryString + ((AscB(MidB(BString, i + 1, 2)) Xor &HFF) * (2 ^ (8 * i))) Else ' extract the byte FromBinaryString = FromBinaryString + AscB(MidB(BString, i + 1, 2)) * (2 ^ (8 * i)) End If Next i
' if it is supposed to be negative, make it so If bIsNegative Then ' Subtract one FromBinaryString = FromBinaryString + 1 ' make it negative FromBinaryString = FromBinaryString * -1 End If
End Function
Public Function ToBinaryString(Number As Variant, Bytes As Integer) As String Dim i As Integer Dim bIsNegative As Boolean
' cannot check byte positions other than 0 to 3 If Bytes > 4 Or Bytes < 1 Then Exit Function
' If the number is negative, we need to handle it last, so we'll set a flag If Number < 0 Then bIsNegative = True ' get the absolute value Number = Number * -1 ' get the binary complement Number = Number Xor ((2 ^ (8 * Bytes - 1)) - 1) ' add one Number = Number + 1 End If
' start at the least significant bit (0) and work backwards For i = 0 To Bytes - 1 If i = Bytes - 1 And bIsNegative Then ' If the number is negative we must turn on the most significant bit ' and then and append it to the string ToBinaryString = ToBinaryString & (ChrB(GetByteValue(Number, i) + &H80)) Else ' just append the byte to our string ToBinaryString = ToBinaryString & ChrB(GetByteValue(Number, i)) End If Next i End Function
Public Function GetByteValue(Number As Variant, BytePos As Integer) As Integer Dim mask As Long On Error Resume Next ' cannot check byte positions other than 0 to 3 If BytePos > 3 Or BytePos < 0 Then Exit Function If BytePos < 3 Then ' build a mask of all bits on for the desired byte mask = &HFF * (2 ^ (8 * BytePos)) Else ' the last bit is reserved for sign (+/-) mask = &H7F * (2 ^ (8 * BytePos)) End If ' turn of all bits but the byte we're after GetByteValue = Number And mask ' move that byte to the end of the number GetByteValue = GetByteValue / (2 ^ (8 * BytePos)) End Function
Public Function SetDeviceTime(Year As Integer, Month As Integer, _ DayOfWeek As Integer, _ Day As Integer, Hour As Integer, Minute As Integer, _ Second As Integer, Millisecond As Integer) As Boolean Dim binaryString As String Dim lRet As Long binaryString = ToBinaryString(Year, CE_INTEGER) binaryString = binaryString & ToBinaryString(Month, CE_INTEGER) binaryString = binaryString & ToBinaryString(DayOfWeek, CE_INTEGER) binaryString = binaryString & ToBinaryString(Day, CE_INTEGER) binaryString = binaryString & ToBinaryString(Hour, CE_INTEGER) binaryString = binaryString & ToBinaryString(Minute, CE_INTEGER) binaryString = binaryString & ToBinaryString(Second, CE_INTEGER) binaryString = binaryString & ToBinaryString(Millisecond, CE_INTEGER) lRet = SetLocalTime(binaryString) ' check for success If lRet = 0 Then SetDeviceTime = False Else SetDeviceTime = True End If End Function
Function GetDeviceTime(ByRef Year As Integer, _ ByRef Month As Integer, _ ByRef DayOfWeek As Integer, _ ByRef Day As Integer, _ ByRef Hour As Integer, _ ByRef Minute As Integer, _ ByRef Second As Integer, _ ByRef Millisecond As Integer) As Boolean Dim binaryString As String Dim lRet As Long ' Allocate space for the variable binaryString = String(8, Chr(0)) ' Call the API lRet = GetLocalTime(binaryString) ' Extract the values from the binary string ' Remeber, we must use MidB to extract bytes Year = FromBinaryString(MidB(binaryString, 1, 2)) Month = FromBinaryString(MidB(binaryString, 3, 2)) DayOfWeek = FromBinaryString(MidB(binaryString, 5, 2)) Day = FromBinaryString(MidB(binaryString, 7, 2)) Hour = FromBinaryString(MidB(binaryString, 9, 2)) Minute = FromBinaryString(MidB(binaryString, 11, 2)) Second = FromBinaryString(MidB(binaryString, 13, 2)) Millisecond = FromBinaryString(MidB(binaryString, 15, 2)) End Function
|
|
|
steve
28 Posts |
Posted - 02 Apr 2003 : 11:37:50
|
Wow, ok. I see now that it's the code from your book, so I'll go ahead and try that. Am I correct in assuming that Now() is still a valid way to read the current date and time, or am I forced to use GetDeviceTime? -Steve |
|
|
ctacke
877 Posts |
Posted - 02 Apr 2003 : 11:39:35
|
Now() still works. |
|
|
steve
28 Posts |
Posted - 05 May 2003 : 17:08:08
|
Wow, this date code looks complex, but works beautifully! Thanks! Have you thought of writing another book??? -Patrick
|
|
|
wabtec4
59 Posts |
Posted - 03 Aug 2005 : 03:02:42
|
Hi Steve, I have a strange problem with SetLocalTime in Windows CE 3.0:
SYSTEMTIME DateTime; DateTime.wYear=2005; DateTime.wMonth=8; DateTime.wDay=3; DateTime.wHour=1; DateTime.wMinute=33; DateTime.wSecond=55;
TIME_ZONE_INFORMATION TimeZone; memset( &TimeZone, 0, sizeof( TIME_ZONE_INFORMATION ) ); TimeZone.Bias = -600; // 600 minutes, Sydney = UTC + 10 hours SetTimeZoneInformation( (const TIME_ZONE_INFORMATION* )&TimeZone );
SetLocalTime( & DateTime );
GetLocalTime( &DateTime );
I got:2005-8-3 1:33:54 I expect: 2005-8-3 1:33:55 Where did the missing one second go? All function were finished correctly, I checked.
|
|
|
wabtec4
59 Posts |
Posted - 03 Aug 2005 : 03:03:58
|
Sorry I forgot: the above code snippet is in C language |
|
|
|
Topic |
|
|
|