Changing time UTC

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bed
    Junior Member
    • Jun 2017
    • 8

    #1

    Changing time UTC

    Hi,

    Wonder if anyone can help. I am a beginner to programming and am trying to write some code. I am unable to retrieve Australian horse racing which I think is due to me passing the time in UTC not local time. I can retrieve GB racing OK but not AU. My current coding is as follows. Can anyone please suggest any changes that will help me get Australian races?

    Many thanks.

    Dim marketStartTime As New StartTime

    If Today.IsDaylightSavingTime() Then
    marketStartTime.from = Format(Date.Now, "yyyy-MM-dd") & "T" & Format(Now.AddHours(-1), "HH:mm") & "Z"

    Else

    marketStartTime.from = Format(Date.Now, "yyyy-MM-dd") & "T" & Format(Now, "HH:mm") & "Z"

    End If

    marketStartTime.to = Today.ToString("u").Replace(" ", "T").Replace("00:00", "23:00")
    params.filter.marketStartTime = marketStartTime
  • jabe
    Senior Member
    • Dec 2014
    • 705

    #2
    I haven't tried to get Australian races. If I get time, I'll see what I can do. Something I did spot - though it probably won't stop you getting races - is a flaw in your IF clause:

    You say if it's daylight saving, to do this:

    marketStartTime.from = Format(Date.Now, "yyyy-MM-dd") & "T" & Format(Now.AddHours(-1), "HH:mm") & "Z"

    However, if the time were between midnight and 1:00am (it might never happen - I don't know what time Aus racing starts), the time would be set to between 11pm and midnight, but the date would be unaltered, so that rather than going back an hour, you're going forward 23 hours.
    Last edited by jabe; 05-06-2017, 01:24 PM.

    Comment

    • jabe
      Senior Member
      • Dec 2014
      • 705

      #3
      Below, I've put request data for a listEvents call, plus what came back. Before I get to that, however, there is a flaw in your design. The place to start should be with listEvents. This will give you a list of meetings. The time returned for a meeting is the time of the first race, so if you use the current time and the meeting has already started, the meeting will not be returned. Set the from-time to all zeroes, as I did below, and that might do the trick.


      My program has a tab specifically for testing calls. I just tried getting races with tomorrow's date and this is what happened.

      The first code is what I put between the filter data when I called List Events:

      Code:
      "filter": {"eventTypeIds": ["7"],"marketStartTime": {"from": "2017-06-06T00:00:00Z","to":" 2017-06-06T23:59:00Z"}}
      This is what came back (with added newlines for readability):

      Code:
      {"jsonrpc":"2.0","result":[
      {"event":{"id":"28260427","name":"Tamw (AUS) 6th Jun","countryCode":"AU","timezone":"Australia/Sydney","venue":"Tamworth","openDate":"2017-06-06T02:00:00.000Z"},"marketCount":16},
      {"event":{"id":"28261098","name":"Font 6th Jun","countryCode":"GB","timezone":"Europe/London","venue":"Fontwell","openDate":"2017-06-06T16:50:00.000Z"},"marketCount":26},
      {"event":{"id":"28260746","name":"GlPk (AUS) 6th Jun","countryCode":"AU","timezone":"Australia/Perth","venue":"Gloucester Park","openDate":"2017-06-06T09:20:00.000Z"},"marketCount":16},
      {"event":{"id":"28260839","name":"PrIsleD (US) 5th Jun","countryCode":"US","timezone":"US/Eastern","venue":"Presque Isle Downs","openDate":"2017-06-05T21:25:00.000Z"},"marketCount":3},
      {"event":{"id":"28260582","name":"Warw (AUS) 6th Jun","countryCode":"AU","timezone":"Australia/Queensland","venue":"Warwick","openDate":"2017-06-06T02:43:00.000Z"},"marketCount":14},
      {"event":{"id":"28260837","name":"MountP (US) 5th Jun","countryCode":"US","timezone":"US/Eastern","venue":"Mountaineer Park","openDate":"2017-06-05T23:00:00.000Z"},"marketCount":18},
      {"event":{"id":"28260353","name":"Bath (AUS) 6th Jun","countryCode":"AU","timezone":"Australia/Sydney","venue":"Bathurst","openDate":"2017-06-06T02:55:00.000Z"},"marketCount":14},
      {"event":{"id":"28260737","name":"APrk (AUS) 6th Jun","countryCode":"AU","timezone":"Australia/Queensland","venue":"Albion Park","openDate":"2017-06-06T03:38:00.000Z"},"marketCount":16},
      {"event":{"id":"28260736","name":"Shep (AUS) 6th Jun","countryCode":"AU","timezone":"Australia/Sydney","venue":"Shepparton","openDate":"2017-06-06T07:30:00.000Z"},"marketCount":20},
      {"event":{"id":"28260828","name":"Mohawk (Harness) (US) 5th Jun","countryCode":"US","timezone":"US/Eastern","venue":"Mohawk","openDate":"2017-06-05T23:30:00.000Z"},"marketCount":24},
      {"event":{"id":"28260827","name":"SunR (US) 5th Jun","countryCode":"US","timezone":"US/Mountain","venue":"Sunray Park","openDate":"2017-06-05T21:00:00.000Z"},"marketCount":3},
      {"event":{"id":"28261113","name":"Sthl 6th Jun","countryCode":"GB","timezone":"Europe/London","venue":"Southwell","openDate":"2017-06-06T17:05:00.000Z"},"marketCount":31},
      {"event":{"id":"28260503","name":"Wang (AUS) 6th Jun","countryCode":"AU","timezone":"Australia/Sydney","venue":"Wangaratta","openDate":"2017-06-06T01:55:00.000Z"},"marketCount":20},
      {"event":{"id":"28260726","name":"Newc (AUS) 6th Jun","countryCode":"AU","timezone":"Australia/Sydney","venue":"Newcastle","openDate":"2017-06-06T07:02:00.000Z"},"marketCount":14},
      {"event":{"id":"28261077","name":"Chep 6th Jun","countryCode":"GB","timezone":"Europe/London","venue":"Chepstow","openDate":"2017-06-06T13:00:00.000Z"},"marketCount":40},
      {"event":{"id":"28260625","name":"Mngl (AUS) 6th Jun","countryCode":"AU","timezone":"Australia/Sydney","venue":"Menangle","openDate":"2017-06-06T01:45:00.000Z"},"marketCount":22}],
      "id":1}
      So I did get some Australian meetings.
      Last edited by jabe; 05-06-2017, 01:27 PM.

      Comment

      • jabe
        Senior Member
        • Dec 2014
        • 705

        #4
        If you're coding in VB.Net (I think you are) this might help.

        In my prog, I have this

        Code:
        Dim getDate As DateTime = Now()
        Defining it like this means it includes the current time, so I can take an hour off

        Code:
        getDate = getDate.AddHours(-1)
        and it adjusts the date as well as the time, if necessary.

        My daylight saving code:

        Code:
        If DateTime.Now.IsDaylightSavingTime Then
                    TimeOfYear = "BST"
                Else
                    TimeOfYear = "GMT"
                End If
        and

        Code:
        If TimeOfYear = "BST" Then
                        getDate = getDate.AddHours(-1)
                    End If

        However. A BIG however. I'm in the UK and it appears that Betfair changes to daylight saving at the same time as the UK. So if you're here, this may all be redundant. It confused me to start with, as I'd coded for it but found my times were wrong when, months later, the changeover happened.

        Comment

        • betdynamics
          Junior Member
          • Sep 2010
          • 534

          #5
          To convert a local time to UTC (in VB.Net), you can use the .ToUniversalTime() method.

          For example:

          'Local Time
          Dim getDate As DateTime = Now()

          'Universal Time
          Dim utcDate As DateTime = Now().ToUniversalTime()

          If you just want to find the current time in UTC, then you can use DateTime.UtcNow

          For example:

          Dim saveUtcNow As DateTime = DateTime.UtcNow

          This works wherever you are in the world as long as your copy of Windows is set to the correct local timezone/time.

          No need to fiddle with BST, etc as Windows automatically knows about Daylight Saving Time.

          Comment

          • jabe
            Senior Member
            • Dec 2014
            • 705

            #6
            Thanks for the info. The reason I used BST/GMT was so that I only had to test for daylight saving once.

            Comment

            • betdynamics
              Junior Member
              • Sep 2010
              • 534

              #7
              Unless you are doing something special with BST/GMT then you shouldn't need to ever check it.

              Windows knows your local timezone and whether that timezone is currently in Daylight Saving Time. If you use ToUniversalTime() then Windows will automatically make the necessary adjustments to give you the correct UTC equivalent.

              Comment

              • jabe
                Senior Member
                • Dec 2014
                • 705

                #8
                Originally posted by betdynamics View Post
                Unless you are doing something special with BST/GMT then you shouldn't need to ever check it.

                Windows knows your local timezone and whether that timezone is currently in Daylight Saving Time. If you use ToUniversalTime() then Windows will automatically make the necessary adjustments to give you the correct UTC equivalent.
                Thanks. I had no idea when I wrote the program how UK time fitted around Betfair time. I'll add the code to my program.

                Comment

                • Bed
                  Junior Member
                  • Jun 2017
                  • 8

                  #9
                  Thanks guys for your thoughts. I am a (very) beginner at this! Your code appears to work OK for getting me "GB" races but I still get an Operation Timed Out error message if I try "AU"....

                  Any ideas?

                  Comment

                  • jabe
                    Senior Member
                    • Dec 2014
                    • 705

                    #10
                    Originally posted by Bed View Post
                    Thanks guys for your thoughts. I am a (very) beginner at this! Your code appears to work OK for getting me "GB" races but I still get an Operation Timed Out error message if I try "AU"....

                    Any ideas?
                    I used exactly the same code as I'd use for getting UK races. Could you get hold of the JSON string you're sending and post it?

                    Comment

                    • Bed
                      Junior Member
                      • Jun 2017
                      • 8

                      #11
                      Hi guys,

                      Thanks for the replies. I am a little confused now though as I have very limited programming experience. I have tried changing the code but still get error messages. I did manage to get some AUS races today but once I tried later in the day, I just got errors. I would have thought that whenever the program run the available races would be displayed no matter what time of day/night. Could you please suggest what I have to do to my code to include yours? ie. am I deleting all mine and just using yours?

                      Here is my original....

                      Dim marketStartTime As New StartTime

                      If Today.IsDaylightSavingTime() Then
                      marketStartTime.from = Format(Date.Now, "yyyy-MM-dd") & "T" & Format(Now.AddHours(-1), "HH:mm") & "Z"

                      Else

                      marketStartTime.from = Format(Date.Now, "yyyy-MM-dd") & "T" & Format(Now, "HH:mm") & "Z"

                      End If

                      marketStartTime.to = Today.ToString("u").Replace(" ", "T").Replace("00:00", "23:00")
                      params.filter.marketStartTime = marketStartTime

                      Comment

                      • jabe
                        Senior Member
                        • Dec 2014
                        • 705

                        #12
                        Which API call are you using?

                        What is the data string you're sending it?

                        Comment

                        • only1jake
                          Junior Member
                          • Nov 2016
                          • 29

                          #13
                          I am in New Zealand and this is how I convert betfair racetime to local time.

                          Ignore the terrible variable names but 'ser' is the JSON object which holds the response from betfair with all the races and details.
                          'thing' gets the races from the betfair response.

                          From there we can select a race by going over the response using a 'for x to y loop', choose "marketStartTime" and then specify this time is UTC using DateTime.SpecifyKind and then just using DateTime to convert this to localtime, but I'm not sure if this takes Daylight Savings into account as its a system converted time.

                          Code:
                          Dim ser As JObject = JObject.Parse(json)
                          
                          Dim racecount As Integer = ser.SelectToken("result").Count
                          For i = 0 To racecount - 1
                          
                          Dim thing As JObject = ser.SelectToken("result")(i)
                          Dim marketstart As DateTime = thing.SelectToken("marketStartTime")
                          Dim markettime As String
                          Dim converteddate As DateTime = DateTime.SpecifyKind(DateTime.Parse(marketstart), DateTimeKind.Utc)
                          converteddate = converteddate.ToLocalTime
                          markettime = converteddate.TimeOfDay.ToString
                          Next
                          If you are using VB.NET then you could just insert this code depending on how you are doing it

                          FORGOT to add, but if you plan on using this code and the JSON Objects you need to place this code at the very very top of your code:
                          Code:
                          Imports Newtonsoft.Json
                          Imports Newtonsoft.Json.Linq
                          Last edited by only1jake; 06-06-2017, 11:47 AM.

                          Comment

                          • Bed
                            Junior Member
                            • Jun 2017
                            • 8

                            #14
                            Thanks for the replies guys. Please forgive me but the code I have used is copied from a book and not from myself. Therefore I'm finding this all a little confusing! I have been playing around with the times to see what difference it makes. It appears that it all depend on what time of day I try and run the code. At present I can alter the time by (-8) and I get a list of AU races. However, if I put in -9 it doesn't work and I get an error. I tried last night and continually got errors. I'm presuming this means my code does not reflect the current time. I have included the response I get back from my time -8. Even here I don't see all the races in AU. It begins with race 2 on my gridview. I see the time also says 01:52 (reflecting my -8hrs) Thanks again for any help!

                            [{"jsonrpc":"2.0","result":[{"marketId":"1.132063490","marketName":"R1 2180m Pace M","marketStartTime":"2017-06-07T01:52:00.000Z","totalMatched":0.0,"runners":[{"selectionId":11055351,"runnerName":"1. Santanna Breeze","handicap":0.0,"sortPriority":1},{"selecti onId":11953564,"runnerName":"2. Wandin Star","handicap":0.0,"sortPriority":2},{"selection Id":8949229,"runnerName":"3. Oldseadog","handicap":0.0,"sortPriority":3},{"sele ctionId":13210324,"runnerName":"4. Rocking Tess","handicap":0.0,"sortPriority":4},{"selection Id":10569165,"runnerName":"5. Oxs Courage","handicap":0.0,"sortPriority":5},{"select ionId":9234365,"runnerName":"6. Aerodyne Fella","handicap":0.0,"sortPriority":6},{"selectio nId":288594,"runnerName":"7. Rigel","handicap":0.0,"sortPriority":7}],"event":{"id":"28261637","name":"Staw (AUS) 7th Jun","countryCode":"AU","timezone":"Australia/Sydney","venue":"Stawell","openDate":"2017-06-07T01:52:00.000Z"}},{"marketId":"1.132063593","mar ketName":"R1 2200m Mdn","marketStartTime":"2017-06-07T02:00:00.000Z","totalMatched":1239.712235999999 8,"runners":[{"selectionId":12111019,"runnerName":"1. Kuttamurra Al","handicap":0.0,"sortPriority":1},{"selectionId ":13210491,"runnerName":"2. The Last Echo","handicap":0.0,"sortPriority":2},{"selection Id":11989024,"runnerName":"3. Datuk Zadragon","handicap":0.0,"sortPriority":3},{"selec tionId":12282978,"runnerName":"4. King Of Monaco","handicap":0.0,"sortPriority":4},{"selecti onId":12714765,"runnerName":"5. Roman Senator","handicap":0.0,"sortPriority":5},{"select ionId":13210492,"runnerName":"6. Surfers Dolphins","handicap":0.0,"sortPriority":6},

                            Comment

                            • only1jake
                              Junior Member
                              • Nov 2016
                              • 29

                              #15
                              I think you might be going about it the wrong way. Why are you selecting Australian races by time? You can specify that you only want AUS races when you send off request. Can you post the request you are using? Thanks.

                              Comment

                              Working...
                              X