Converting Xojo.Core.Date to NSDateComponentsMBS problem

I’ve got 2 customers where Xojo.Core.Date is crashing or giving a runtime error in Xojo 2019r3. Therefore, I had the idea to use the MBS plugin instead.

Old code:

[code]'make a time difference in seconds
dim theMinutes as Integer = Val(TimeOffset.Right(2))
dim RestOffset as String = Mid(TimeOffset, 1, TimeOffset.Length - 2)
dim theHours as Integer = Val(RestOffset.Right(2))
if TimeOffset.Left(1) = “-” then theHours = - theHours

'make a timezone
dim TimeDifference as Integer = theMinutes * 60 + (theHours * 60 * 60)
dim theTimeZone as new Xojo.Core.TimeZone(TimeDifference)

'the core date handles time zones automatically
dim CoreDate as new Xojo.Core.Date(theDate.Year, theDate.Month, theDate.Day, theDate.Hour, theDate.Minute, theDate.Second, theTimeZone)
if CoreDate = Nil then Return

'make a date with the local timezone
dim localDate as new Xojo.Core.Date(CoreDate.SecondsFrom1970, Xojo.Core.TimeZone.Current)

'make new date in the old framework
theDate = Nil
theDate = new Date
theDate.Year = localDate.Year
theDate.Month = localDate.Month
theDate.Day = localDate.Day
theDate.Hour = localDate.Hour
theDate.Minute = localDate.Minute
theDate.Second = localDate.Second
[/code]

New code:

[code]'make a time difference in seconds
dim theMinutes as Integer = Val(TimeOffset.Right(2))
dim RestOffset as String = Mid(TimeOffset, 1, TimeOffset.Length - 2)
dim theHours as Integer = Val(RestOffset.Right(2))
if TimeOffset.Left(1) = “-” then theHours = - theHours

'make a timezone
dim TimeDifference as Integer = theMinutes * 60 + (theHours * 60 * 60)

'make a date
dim theTimeZoneMBS as NSTimeZoneMBS = NSTimeZoneMBS.timeZoneForSecondsFromGMT(TimeDifference)
dim CoreDateMBS as new NSDateComponentsMBS
CoreDateMBS.year = theDate.Year
CoreDateMBS.month = theDate.Month
CoreDateMBS.day = theDate.Day
CoreDateMBS.Hour = theDate.Hour
CoreDateMBS.minute = theDate.Minute
CoreDateMBS.second = theDate.Second
CoreDateMBS.timeZone = theTimeZoneMBS

dim newDate as Date = CoreDateMBS.date[/code]

The last line of code doesn’t work. So I had to get back the result with a bit more code:

dim newDate as new Date newDate.Year = CoreDateMBS.year newDate.Month = CoreDateMBS.month newDate.Day = CoreDateMBS.day newDate.Hour = CoreDateMBS.hour newDate.Minute = CoreDateMBS.minute newDate.Second = CoreDateMBS.second

But I’m not sure if what I’m doing is correct here. This is the date I’m trying to parse:
original date string: Sat, 26 May 2018 10:18:36 +0900

The result in the debugger are:
theDate: 2018-05-26 03:18:36
newDate: 2018-05-26 10:18:36

theDate is the original result which I know is correct. newDate doesn’t seem to do anything with the timezone at all.

What am I doing wrong? Xojo 2019r3. High Sierra, MBS 19.5.

A bit confused, so you gave a sample project?
Feedback cases for bugs in Xojo?

I would not expect setting time zone to on NSDateComponentsMBS to do anything. It’s just a normal property.
When you query date, internally the time may be converted, but in order to convert it back to a Xojo date, we apply a time zone.

I’ll make a sample project tomorrow. Here is the Feedback case: <https://xojo.com/issue/58879>
Applying the time zone is the main purpose of the code.

how about this?

[code]Private Function AddOffsetMBS(OriginalDate as Date, TimeOffset as String) as Date

'timeoffset: +0000 (UTC)

'make a time difference in seconds
dim theMinutes as Integer = Val(TimeOffset.Right(2))
dim RestOffset as String = Mid(TimeOffset, 1, TimeOffset.Length - 2)
dim theHours as Integer = Val(RestOffset.Right(2))
if TimeOffset.Left(1) = “-” then theHours = - theHours

'make a timezone
Dim TimeDifference As Integer = theMinutes * 60 + (theHours * 60 * 60)

Dim d As New date(OriginalDate)

'Dim d1 As String = d.SQLDateTime
d.GMTOffset = 0 // to UTC
'Dim d2 As String = d.SQLDateTime
d.GMTOffset = theHours + theMinutes/60 // and to new time zone
'Dim d3 As String = d.SQLDateTime

Return d
End Function[/code]

My code doesn’t set the new date to the GMTOffset but adds the difference in GMTOffsets.

If I start with the following values

dim d as String = “2017-06-26 01:18:45”
dim t as String = “+0300”

for date and offset then the result of the original function is

2017-06-26 00:18:45

because here the GMTOffset is +200 for Germany with Daylight Savings Time in June. Therefore, the function only subtracts one hour instead of 2.

I need this solved urgently. Yesterday evening I got the third customer with a crash.