D
'Requires Registry DLL (object reg), Bitwise DLL (object bit) and Filippo's SysTime DLL (object SysTime)
Sub Globals
Dim zti(0) As Byte 'binary array for GetTimeZone
End Sub
Sub App_Start
Reg.New1
SysTime.New1
End Sub
Sub mnuSetSystemDateTime_Click
If GPS.Status<>"A" Then Return 'Aborts if no stable GPS signal present
'GPS.UTCDate and GPS.UTCTime available from Sub GPS_GPSDecoded. Please see gps.dll Help for details
Dim x, y, i, zt
zt = GetTimeZone
x = DateParse(GPS2Date(GPS.UTCDate))
y = TimeParse(GPS2Time(GPS.UTCTime)) + zt/24 * cTicksPerDay 'add your local time zone
i = Msgbox("GPS date and time converted to local is " & CrLf & Date(x) & " " & Time(y) & CrLf & "Set the device?"," Confirmation",cMsgBoxYesNo,cMsgBoxQuestion)
If i = cNo Then Return
ErrorLabel(SetError) 'There is no success flag in the SysTime DLL
SysTime.New1
Systime.SetDate(DateMonth(x),DateDay(x),DateYear(x))
Systime.SetTime(TimeHour(y),TimeMinute(y),TimeSecond(y))
Msgbox("GPS Date and Time set."," GPS Date and Time",cMsgBoxNone,cMsgBoxAsterisk)
Return
SetError:
Msgbox("Error setting Date and Time."," GPS Date and Time",cMsgBoxNone,cMsgBoxHand)
End Sub
Sub GPS2Time(x)
Dim colon
TimeFormat("HH:mm:ss")
colon=SubString(x,0,6)
colon=StrInsert(colon,2,":")
colon=StrInsert(colon,5,":")
x = TimeParse(colon)
if chkAMPM.Checked=True Then TimeFormat("hh:mm:ss tt") 'A check box selects EU or US format
Return Time(x)
End Sub
Sub GPS2Date(x)
Dim dash
DateFormat ("yyyy/mm/dd")
dash="20" & SubString(x,4,2) & "/"
dash=dash & SubString(x,2,2) & "/"
dash=dash & SubString(x,0,2)
x = DateParse(dash)
if chkAMPM.Checked=false then 'A check box selects EU or US format
DateFormat("dd/mm/yyyy")
else
DateFormat("mm/dd/yyyy")
End if
Return Date(x)
End Sub
Sub GetTimeZone
Dim m, i, b
Reg.RootKey(Reg.rtLocalMachine)
If cPPC = True Then
zti()=Reg.GetValue("Time","TimeZoneInformation")
For i = 3 To 0 Step -1
m = m & bit.DecToHex(zti(i))
Next
m = bit.HexToDec(m) / 60 * (-1)
Else
m = Reg.GetValue("System\CurrentControlSet\Control\TimeZoneInformation","ActiveTimeBias")
m = m / 60 * (-1)
End If
If m > 0 Then m = "+" & m
Return m
End Sub
Sub mnuSetSystemDateTime_Click
If GPS.Status="A" Then
Dim x, i
x =DateParse(GPS2Time(GPS.UTCTime)) 'addition of time zone must account for date change
x = x - x Mod cTicksPerDay
x = x + TimeParse(GPS2Time(GPS.UTCTime)) + zt * cTicksPerHour
i = Msgbox("GPS date and time converted to local is " & CrLf & Date(x) & " " & Time(x) & CrLF & "Set the device?"," Confirmation",cMsgBoxYesNo,cMsgBoxQuestion)
If i = cNo Then Return
ErrorLabel(SetError)
SysTime.New1
Systime.SetDate(DateMonth(x),DateDay(x),DateYear(x))
Systime.SetTime(TimeHour(x),TimeMinute(x),TimeSecond(x))
Msgbox("GPS Date and Time set."," GPS Date and Time",cMsgBoxNone,cMsgBoxAsterisk)
Return
SetError:
Msgbox("Error setting Date and Time."," GPS Date and Time",cMsgBoxNone,cMsgBoxHand)
Else
Msgbox("GPS data unstable or not available."," GPS Status",cMsgBoxNone,cMsgBoxHand)
Return
End If
End Sub
Syntax: SetDate(Day As Integer, Month As Integer, Year As Integer)
B4X:Sub mnuSetSystemDateTime_Click ... i = Msgbox("GPS date and time converted to local is " & CrLf & Date(x) & " " & Time(x) & CrLF & "Set the device?"," Confirmation",cMsgBoxYesNo,cMsgBoxQuestion) ... Return nd Sub
Hi,
SysTime is a small library to get and set the current system time and system date.
There are two different dll's, one for the desktop and one for the device.
Ciao.
Filippo
Ciao Filippo,
Execellent! Awesome!
Thanks to your DLL, I can set the clock with GPS date and time.
Here is the source for everybody. Perhaps someone can shorten it
B4X:... Sub GetTimeZone Dim m, i, b Reg.RootKey(Reg.rtLocalMachine) If cPPC = True Then zti()=Reg.GetValue("Time","TimeZoneInformation") For i = 3 To 0 Step -1 m = m & bit.DecToHex(zti(i)) Next m = bit.HexToDec(m) / 60 * (-1) Else m = Reg.GetValue("System\CurrentControlSet\Control\TimeZoneInformation","ActiveTimeBias") m = m / 60 * (-1) End If If m > 0 Then m = "+" & m Return m End Sub
I suspect they contain the DST information for the time zone.lots of other following bytes; goodness knows what they mean!
That is a struct definition that shouldn't really be visible. An instance of it is passed to the OS to set or get the time and date information. It is of no use as a Basic4ppc object.SYSTEMTIME
I suspect they contain the DST information for the time zone.
If you mean visible to the user then I don't think so, at least in Windows. I think the DST has always been either manual,as in the old days , or automatic and invisible to the user in more modern times.Isn't this standard locale information that should be readily available?
If you mean visible to the user then I don't think so, at least in Windows. I think the DST has always been either manual,as in the old days , or automatic and invisible to the user in more modern times.
Sorry gents,
There is an error in
mnuSetSystemDateTime_Click
which I have corrected as:
B4X:Sub mnuSetSystemDateTime_Click If GPS.Status="A" Then Dim x, i x =DateParse(GPS2Time(GPS.UTCTime)) 'addition of time zone must account for date change x = x - x Mod cTicksPerDay x = x + TimeParse(GPS2Time(GPS.UTCTime)) + zt * cTicksPerHour ... End Sub
Yes, it's an annoying quirk of the .NET DateTime Parse method that it adds the current date to the time when it parses a string if there is no date specified. I have no idea why someone thought that behaviour was a good idea .It's because the TimeParse() function rather strangely adds the time increment to the current date to generate the full ticks value
I added the change.@Filippo - how about hiding "SYSTEMTIME" by renaming it "_SYSTEMTIME"
I can not, I know myself not using c#and providing the sources for merging?
'Requires Registry DLL (object reg), ByteConverter DLL and Filippo's SysTime DLL (object SysTime)
Sub CompareSystemTime(nowTicks, yearUTC, monthUTC, dayUTC, hourUTC, minuteUTC, secondUTC)
moString = Format(monthUTC, "D2")
dyString = Format(dayUTC, "D2")
yrString = Format(yearUTC, "D4")
DateFormat("yyyy-mm-dd")
TimeFormat("HH:mm:ss")
dateString = yrString & "-" & moString & "-" & dyString
dateTicks = DateParse(dateString)
timeUTCTicks = TimeAdd(dateTicks, hourUTC, minuteUTC, secondUTC)
textBox1.Text = "Date&time (UTC)=" & CRLF & cTab & Date(timeUTCTicks) & " " & Time(timeUTCTicks)
minsOffset = - GetActiveTimeBias
timeTicks = TimeAdd(timeUTCTicks, 0, minsOffset, 0)
disparity = Int((timeTicks - nowTicks) / cTicksPerSecond + 0.5)
If disparity = 1 Then
unit = " second"
Else
unit = " seconds"
End If
textBox1.Text = textBox1.Text & CRLF & "Date&time (local)=" & CRLF & cTab & Date(timeTicks) & " " & Time(timeTicks) & _
CRLF & "System time =" & CRLF & cTab & Date(nowTicks) & " " & Time(nowTicks) & _
CRLF & "Time disparity = " & disparity & unit
If Abs(disparity) > 1 Then
If Msgbox("Disparity = " & disparity & unit, "Adjust system time?", cMsgboxYesNo) = cYes Then
adjustTime(disparity)
End If
End If
End Sub
Sub GetActiveTimeBias 'In minutes, to go from local to UTC (!)
Dim m, i, b
Reg.New1
Reg.RootKey(Reg.rtLocalMachine)
If cPPC = True Then
zti()=Reg.GetValue("Time","TimeZoneInformation")
m = Converter.Int32FromBytes(zti(), 168)
Else
m = Reg.GetValue("System\CurrentControlSet\Control\TimeZoneInformation","ActiveTimeBias")
End If
Return m
End Sub
Sub adjustTime(seconds)
TimeObj.New1
timeNow = Now
timeAdjusted = timeNow + seconds * cTicksPerSecond
TimeObj.SetDate(DateMonth(timeAdjusted),DateDay(timeAdjusted),DateYear(timeAdjusted)) ' Tweak the current system date
TimeObj.SetTime(TimeHour(timeAdjusted),TimeMinute(timeAdjusted),TimeSecond(timeAdjusted)) ' Tweak the current System time
End Sub
By way of restitution for my criticism of alfcen's code (which he invited us to shorten) and pay-back for all the assistance here, I offer my version of the crucial portion. ...
Obj1.New1(False)
Args1.New1(1)
Args1.SetValue(0, 0, "System.Int64")
Obj1.CreateNew2("System.DateTime", Args1.Value)
Obj1.Value = Obj1.GetProperty("UtcNow")
UtcTicks = Obj1.GetProperty("Ticks")
LocalOffset = Now - UtcTicks
Hi Filippo,
I think there is an error in your help file for SetDate
Its looking as if the Day and Month are swapped i.e. it should read:
Syntax: SetDate(Month As Integer, Day As Integer, Year As Integer)