iOS Question why does this code make my app crash??

ilan

Expert
Licensed User
Longtime User
something is not clear to me

i want to check if a specific February is a leap year with this piece of code:

B4X:
Sub IsLeapYear(Year As Int) As Boolean
   Try
      DateTime.DateFormat = "MM/dd/yyyy"
      DateTime.DateParse("02/29/" & Year)
      Return True
   Catch
      Log(LastException)
      Return False
   End Try
End Sub

so if the try wont be able to parse the date the catch should be raised and i should get a false returned
but i am getting an error and a crash that says could not parse the date.

Copying updated assets files (227)
Application_Start
Between mdays: 01/08/2016 - 31/08/2016
Between mdays: 01/09/2016 - 30/09/2016
Between mdays: 01/10/2016 - 31/10/2016
Between mdays: 01/11/2016 - 30/11/2016
Between mdays: 01/12/2016 - 31/12/2016
Between mdays: 01/01/2017 - 31/01/2017
<B4IExceptionWrapper: Error Domain=caught_exception Code=0 "Error parsing: 02/29/2017" UserInfo={NSLocalizedDescription=Error parsing: 02/29/2017}>
Error occurred on line: 790 (Main)
Error parsing: 28/02/2017
Stack Trace: (
CoreFoundation <redacted> + 150
libobjc.A.dylib objc_exception_throw + 38
CoreFoundation <redacted> + 0
B4i Example -[B4IDateTime DateParse:] + 186
B4i Example -[b4i_main _updateclv] + 4400
B4i Example -[b4i_main _shiftdateright_click] + 1208
CoreFoundation <redacted> + 68
CoreFoundation <redacted> + 292
B4i Example +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1786
B4i Example -[B4IShell runMethod:] + 574
B4i Example -[B4IShell raiseEventImpl:method:args::] + 1998
B4i Example -[B4IShellBI raiseEvent:event:params:] + 1442
B4i Example __33-[B4I raiseUIEvent:event:params:]_block_invoke + 74
libdispatch.dylib <redacted> + 10
libdispatch.dylib <redacted> + 22
libdispatch.dylib <redacted> + 1524
CoreFoundation <redacted> + 8
CoreFoundation <redacted> + 1574
CoreFoundation CFRunLoopRunSpecific + 520
CoreFoundation CFRunLoopRunInMode + 108
GraphicsServices GSEventRunModal + 160
UIKit UIApplicationMain + 144
B4i Example main + 108
libdyld.dylib <redacted> + 2
)

but why does this code crash? i am using a try catch..

any clue?

regards, ilan
 

ilan

Expert
Licensed User
Longtime User
ok i understand what was the problem. i grabbed this code from the forum and it uses a different Date.Format then i use in the Callback Sub. :)

changing the Date.Format to "dd/MM/yyyy" solved the problem
 
Upvote 0

Ed Brown

Active Member
Licensed User
Longtime User
Hi @ilan

This will give you a true/false answer:
B4X:
isLeap = (((y mod 4) = 0) And ((y mod 100) <> 0)) Or ((y mod 400) = 0)

used like...
B4X:
DateTime.DateFormat = "dd/MM/yyyy"
Dim ldate As Long = DateTime.DateParse("31/3/1901")

Dim y = DateTime.GetYear(ldate)        ' Get the year
Dim isLeap As Boolean
isLeap = (((y mod 4) = 0) And ((y mod 100) <> 0)) Or ((y mod 400) = 0)

Log(isLeap)

Leap years are every 4 years except for centuries (ie. 1700,1800,1900). Every 400 years the century IS a leap year (ie. 1600, 2000, 2400).

Hope this helps.
 
Upvote 0
Top