Bug? TimeParsing invalid time in try/catch changes timezoneoffset

kostefar

Active Member
Licensed User
Longtime User
Dear All,

I was looking for a simple way to determine whether if a time is formatted correctly or not. My program uses a time picker, but also can be accessed remotely where a time needs to be entered manually in a prompt, and that´s where I need it.

So I found this thread:

https://www.b4x.com/android/forum/threads/verify-a-valid-date.92274/#content

And made this, just an example to show the problem:

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
End Sub
Sub istimevalid (mtime As String)
   
    Try
        DateTime.TimeParse(mtime)
    Catch
        Return False
    End Try
    'Log (DateTime.DateTimeParse(DateTime.Date(DateTime.Now),DateTime.Time(DateTime.Now)))
    Return True
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    Log (DateTime.Time(DateTime.Now))
    Log (istimevalid("blah"))
    Log (DateTime.Time(DateTime.Now))
    Log (istimevalid("02:17:30"))
    Log (DateTime.Time(DateTime.Now))
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

In the log this gives me:

11:49:06
false
09:49:06
true
11:49:06


So, if I don´t parse a valid time after an invalid one, the timezone offset changes!
I just fixed it by modifying the code so that it always automatically will pass a valid time afterwards, and it works fine for me that way but I highly doubt that this is intentional.
 
Last edited:

Daestrum

Expert
Licensed User
Longtime User
If you add
B4X:
DateTime.SetTimeZone(DateTime.TimeZoneOffset)
into
B4X:
Sub istimevalid (mtime As String)
    Try
        DateTime.TimeParse(mtime)
    Catch
       DateTime.SetTimeZone(DateTime.TimeZoneOffset) ' correct the timezone on parse failure
       Return False
    End Try
    'Log (DateTime.DateTimeParse(DateTime.Date(DateTime.Now),DateTime.Time(DateTime.Now)))
    Return True
End Sub

As the java routine clears the timezone on entry but the failure to parse the time, it bugs out on an exception before resetting the timezone.
The above line just forces it to reset the timezone on failure.
 

kostefar

Active Member
Licensed User
Longtime User
Ok, that´s a more effecient way of doing it that my way of dealing with it. So this is a javabug then? I mean, there´s no really good reason why it should be cleared by java in the first place but I do get that it´s not a b4j bug; using try/catch to find out if a date is in the right format is a bit of a hacked way of doing it.
 

Daestrum

Expert
Licensed User
Longtime User
Java never cleared it (not a java bug) , it was some 'generated' code from B4J that clears it.
 
Top