I am an inexperienced developer fresh out of college. I was handed a Windows Forms Application to work on.
The application previously relied on system time. They wanted me to make it so that we could adjust the time within the application. No problem at all for me, I just added in some number selectors for up to +/- 23hrs, 59 mins, and 59secs.
I then I simply added this offset to the system time and used that as the clock within the application and all was well.
However, it was at this time that I was informed by one of my internal customers (one of the occasional users for this software & a supervisor) that he sometimes changes the time on his PC to the local time, and then when he connects to our network via VPN the time on his PC gets rest to our network time.
This was a problem since if user traveled to another time zone and then changed the time on his PC manually, then set up our application with the time adjusted to match the time of the client's clock at the job site, then when he connected to our network by VPN (for one reason or another), it would reset the time and then the system time plus the offset would no longer match up to the client's time. (asking users not to alter the time is not an option it seems).
In order to solve this problem I took the following steps:
- create a new Date object for our internal time (lets call it "RATA time")
- Plant time is initially equal to the system time
- you can add an offset to that time to match the time at the job site
- Then I capture a start time and used a timer to keep track of the seconds that pass and keep adding them to the start time to get the current time and it is unaffected by the system time
VB.NET code:
Private Sub dataTimer_Tick(sender As System.Object, e As System.EventArgs) Handles dataTimer.Tick
seconds += 1
time = TimeSpan.FromSeconds(seconds)
RATAtime = startTime + time '+ offset
formattedRAtaTime = RATAtime.ToString("HH\:mm\:ss")
lblRataTime.Text = formattedRAtaTime
lblRunningTime.Text = time.ToString("hh\:mm\:ss")
End Sub
So what went wrong? Answer: "RATA time" is falling behind the actual time. It's especially bad on a slow computer.
I think the issue is that the timer is not a real time system and only meant to be so accurate. the processor will get to it whenever it gets to it and no necessarily immediately. It may be an issue with not using await and asynch, which is a way to ensure threading is being properly implemented.
What is the best way to go about this? Computers may or may not have internet access while the application is being run.
Extra info: in desperation I even tried reading the time off of a device that this application interacts with, while I was successful in doing so, and parsing it into a date object, when I tried to set RATA time to that value, it was not happening and the debugger was saying there was an issue with threads. That was never an elegant or desirable solution though. Here is that code if anyone feels that that would help:
Private Sub ParseLoggerTime(response As String)
Dim loggerTime As Date
' Check if the response has the expected format
If response(0) = "@" AndAlso response.Substring(5, 2) = "LL" AndAlso IsNumeric(response.Substring(7, 12)) AndAlso response(19) = "&" Then
Dim year As Integer = Convert.ToInt32("20" & response.Substring(7, 2))
Dim month As Integer = Convert.ToInt32(response.Substring(9, 2))
Dim day As Integer = Convert.ToInt32(response.Substring(11, 2))
Dim hour As Integer = Convert.ToInt32(response.Substring(13, 2))
Dim minute As Integer = Convert.ToInt32(response.Substring(15, 2))
Dim second As Integer = Convert.ToInt32(response.Substring(17, 2))
Try
loggerTime = New Date(year, month, day, hour, minute, second)
lblLogger.Text = loggerTime.ToString("HH\:mm\:ss") ' Update the label here
loggerTimeGlobal = loggerTime
Catch ex As Exception
' Handle any potential DateTime creation exception here
' You might want to log the error or notify the user
loggerTime = Date.MinValue ' Set to a default value
lblLogger.Text = "Error" ' Update the label here with an error message
End Try
Else
' Handle invalid response format here
loggerTime = Date.MinValue ' Set to a default value
lblLogger.Text = "Invalid Format" ' Update the label here with an error message
End If
End Sub
The code for setting RATA time = logger time was put in the timer tick handler function/ procedure.