Date/time oddity using JavaScriptSerializer()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jabe
    Senior Member
    • Dec 2014
    • 705

    #1

    Date/time oddity using JavaScriptSerializer()

    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.

    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
    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:

    Code:
    "kickoff":"\/Date(1477159500000)\/"
    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

    Code:
    "kickoff":"2016-10-22T23:00:00+01:00"
    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.
  • geoffw123
    Senior Member
    • Mar 2014
    • 250

    #2
    Hi Jabe

    Not sure if this helps at all, but that the funky date format is UnixTime in milliseconds I think.

    so 1477159500000/1000 --> 1477159500 UnixTime in Seconds

    Using http://www.onlineconversion.com/unix_time.htm

    This converts to Sat, 22 Oct 2016 18:05:00 GMT

    Not really sure of the subtleties of how you cope with Daylight Savings when using unixTime format.

    In .NET I just tend to use MarketStartTime.ToLocalTime();

    This has worked fine for me so far for both UK British summer time and GMT. (I am using the Newtonsoft Json Library too)

    Regards Geoff
    Last edited by geoffw123; 24-10-2016, 12:29 AM.

    Comment

    • jabe
      Senior Member
      • Dec 2014
      • 705

      #3
      Originally posted by geoffw123 View Post
      Hi Jabe

      Not sure if this helps at all, but that the funky date format is UnixTime in milliseconds I think.

      so 1477159500000/1000 --> 1477159500 UnixTime in Seconds

      Using http://www.onlineconversion.com/unix_time.htm

      This converts to Sat, 22 Oct 2016 18:05:00 GMT

      Not really sure of the subtleties of how you cope with Daylight Savings when using unixTime format.

      In .NET I just tend to use MarketStartTime.ToLocalTime();

      This has worked fine for me so far for both UK British summer time and GMT. (I am using the Newtonsoft Json Library too)

      Regards Geoff
      Hi Geoff

      Thank you. Perhaps .ToLocalTime is what I need. I'm wondering about changing completely to the Newtonsoft converter. Thanks for the converter link too.

      All the best
      Jabe

      Comment

      Working...
      X