This is rather long - if you're using JavaScriptSerializer() it might be worth reading. If not, save yourself a few hours.
My VB.Net program has been under development since God was a lad and, for reasons I don't quite recall, I've been using JavaScriptSerializer() to convert between JSON strings and objects. The reason may have been down to confusion in earlier versions of Visual Studio when I couldn't work out how to get hold of NewtonsoftJson. I can get it now.
My program bets on selected football games. I needed to be able to stop and restart the program, especially during the development phase but also in case of untrapped errors in the future. I have a class for details about the football matches and this includes various settings I make when I select a game for betting on.
When data contained within each football match object changes, the contents of the object are serialized and saved to a file (specific to the football match). On restart, the file contents are deserialized to a football match object.
The program displays the number of minutes before or after kickoff. Before is negative, after is positive. I'd noticed an oddity whereby sometimes the number of minutes to/from kickoff would be wrong by an hour; not just for one match, but for all of them.
When I wrote the program, I didn't know how Betfair or Visual Basic dealt with GMT and BST (daylight saving). I initially assumed I'd need to add or subtract 60 minutes somewhere, especially if Betfair's kickoff times were all in GMT (or UTC or UT1, etc). In the end, I didn't need to do that, but the intermittent 60 minute discrepancy made me wonder if I was missing something.
I eventually spotted that the 60 minute difference only happened when I stopped and restarted the program. There's a file I can delete to make a restart not a restart, and that allowed me to confirm that this was when the oddity happened.
But it didn't tell me why it happened.
This is the routine to get the number of minutes to/from kickoff.
kickoff is defined as timedate and initially given the value of openDate from the listEvent.
I put a breakpoint in and found that the value in the kickoff variable had the correct date and time, the same time as the string (also part of the object) written to the display. This was true all the way up to the point when the football match object was serialised and written to a file. It was true for whichever matches I looked at.
When I stopped and restarted the program, it recreated the football match objects. A checkpoint in the football match class allowed me to check the times and dates, and this was when it got strange. There was now a difference of one hour between the value held in the kickoff variable and the value in the display string. This was the case for all the ones I looked at.
One work round was to save the kickoff date/time as a string and to convert it back in the minutes() function. That worked okay. I googled to see if VB or Javascript had a date problem related to serializing/deserializing, and found reference to such a problem with Javascript. That led me to to try altering the routines for saving the objects as files and reloading them, making them use the NewtonsoftJson methods instead of the JavaScriptSerializer ones. And it worked fine.
In case you are wondering why I haven't mentioned it, the kickoff date/time values saved in the files for the football matches look like this:
and that didn't help me (nor did I investigate how to convert it to something more readable).
The NewtonSoftJson versions saves the value more helpfully
So it appears that the problem arises when serialising and deserialising a date/time value using the JavaScriptSerializer. I have no idea why it didn't appear to affect the value by a further 60 minutes on subsequent restarts.
I may write another program just to investigate the problem. If I do, I'll add the code and whatever else I find to this thread.
My VB.Net program has been under development since God was a lad and, for reasons I don't quite recall, I've been using JavaScriptSerializer() to convert between JSON strings and objects. The reason may have been down to confusion in earlier versions of Visual Studio when I couldn't work out how to get hold of NewtonsoftJson. I can get it now.
My program bets on selected football games. I needed to be able to stop and restart the program, especially during the development phase but also in case of untrapped errors in the future. I have a class for details about the football matches and this includes various settings I make when I select a game for betting on.
When data contained within each football match object changes, the contents of the object are serialized and saved to a file (specific to the football match). On restart, the file contents are deserialized to a football match object.
The program displays the number of minutes before or after kickoff. Before is negative, after is positive. I'd noticed an oddity whereby sometimes the number of minutes to/from kickoff would be wrong by an hour; not just for one match, but for all of them.
When I wrote the program, I didn't know how Betfair or Visual Basic dealt with GMT and BST (daylight saving). I initially assumed I'd need to add or subtract 60 minutes somewhere, especially if Betfair's kickoff times were all in GMT (or UTC or UT1, etc). In the end, I didn't need to do that, but the intermittent 60 minute discrepancy made me wonder if I was missing something.
I eventually spotted that the 60 minute difference only happened when I stopped and restarted the program. There's a file I can delete to make a restart not a restart, and that allowed me to confirm that this was when the oddity happened.
But it didn't tell me why it happened.
This is the routine to get the number of minutes to/from kickoff.
Code:
Public Function minutes()
Dim elapsed As TimeSpan = Now() - kickoff
Return (1440 * elapsed.Days + 60 * elapsed.Hours + elapsed.Minutes) ' this works; if neg, game hasn't started
End Function
I put a breakpoint in and found that the value in the kickoff variable had the correct date and time, the same time as the string (also part of the object) written to the display. This was true all the way up to the point when the football match object was serialised and written to a file. It was true for whichever matches I looked at.
When I stopped and restarted the program, it recreated the football match objects. A checkpoint in the football match class allowed me to check the times and dates, and this was when it got strange. There was now a difference of one hour between the value held in the kickoff variable and the value in the display string. This was the case for all the ones I looked at.
One work round was to save the kickoff date/time as a string and to convert it back in the minutes() function. That worked okay. I googled to see if VB or Javascript had a date problem related to serializing/deserializing, and found reference to such a problem with Javascript. That led me to to try altering the routines for saving the objects as files and reloading them, making them use the NewtonsoftJson methods instead of the JavaScriptSerializer ones. And it worked fine.
In case you are wondering why I haven't mentioned it, the kickoff date/time values saved in the files for the football matches look like this:
Code:
"kickoff":"\/Date(1477159500000)\/"
The NewtonSoftJson versions saves the value more helpfully
Code:
"kickoff":"2016-10-22T23:00:00+01:00"
I may write another program just to investigate the problem. If I do, I'll add the code and whatever else I find to this thread.


Comment