XOJO 2022 R4.1 Strange Date issue

Hello guys,

It seems that there is a weird issue with the date or the timeinterval on the latest xojo.

I am calling an API and in order not to check like crazy i get the TTL of the token , which in my case for test purposes i set it at 12 hours .

“expires_in”: 43199

Until here all good.

i add that to a DateTime variable and i store it in the DB .

Var dif As New DateInterval

AD.TokenTTL = loginJS.Value("expires_in").IntegerValue

dif.Seconds = AD.TokenTTL

AD.tokenExpDate = tokenTS + dif

So here i have the “Proper” Expiry timestamp.

In the main window i run a timer every 5 min to check the expiry date against the current date

Var curDte As DateTime
Var Interval As DateInterval

curDte = DateTime.Now

Interval = AD.tokenExpDate - curDte

If Interval.Minutes < 10 Then
'Fetch the new token
End If

Apparently after 6 hours, or always half the time i set in the expiry token it fires and fetches new token.

After it fires the first time, in my case in 6 hours not 12, then it seems that the time gets reduced to 1 hour and it fires every 50 min to fetch the new token .

The API guy says that he set the expiry date to 720 min , so 12 hours supposedly and it should provide a 12 hours token validity TTL in my case.

So considering that i do get the first token as 12 hours , stated above

“expires_in”: 43199

The question is , why the API call from XOJO based on my code fires at 6 Hours ? for the first time ?

Is this a bug or i do something wrong ?

IDE : XOJO 2022R4.1
OS : MacOS Monterey, 12.6.2
Hardware : MacMini, M1 Chip.

App compiled for M1, and it runs as build app and not as debug.

Thanks.

You can’t use DateInterval for what you want to do. You need to calculate the difference in DateTime in seconds and the divide that into hours.

And why would that be ? The way i use it is is ok , based on the XOJO Docs .

If you see the code i do get the interval based on 2 dates .

Could you please explain why is that bad ?

Thanks

Look at the code in the documentation:

Dim d1 As New DateTime(2017, 5, 1)
Dim d2 As New DateTime(2017, 5, 20)
Dim interval As DateInterval = d2 - d1

You get the difference in days here and not the hours times 24.

Well that is in the case where you specify your date as you did . in my case d1 = DateTime.Now which holds the timestamp, and i just did a test to make sure my code works , and it does

Screenshot 2022-12-31 at 10.28.20

i think i get your point

Apparently the Interval.Minutes holds just the 60 min and it resets back to 0 once it passes over 60 , and i get that once i tested with Seconds , which is bad. So now i get why my timer resets at every 50 min but i don’t get why it did reset after 6 hours instead of 12 , or i do.

Considering that i check every 10 min , if the Interval.Minutes was always > 10 then it was skipping, while after 6 hours it matched the min where it was < 10 and it reset, That could be the most logical explanation, i guess.

Thanks Beatrix, i guess i need to redo that part now.

I guess i should use DateTime.SecondsFrom1970 to be more accurate.

1 Like

Don’t forget doubles when you calculate anything with SecondsFrom1970. I used ints recently and the calculations didn’t work.

Well it does say Doubles on the Docs

SecondsFrom1970 As Double

So i use Doubles and then convert to Int to get te actual min with the computations.

Thanks.

Try this code

Const token As String = "{""expires_in"": 43199}"

Var j As New JSONItem(token)

Var ttl As Integer = j.Value("expires_in")

Var tokenExpires As DateTime = DateTime.Now.AddInterval(0, 0, 0, 0, 0, ttl)

And if you want to use a simple comparison in your timer add

tokenExpires = token.Expires.SubtractInterval(0, 0, 0, 0, 5) ' Subtract 5 mins to give a buffer

Now in your timer you can use

If DateTime.Now < expires Then
Return
End If

// Get a new token