B4J Question [SOLVED embarrassingly] Daylight saving has some unexpected effects

JackKirk

Well-Known Member
Licensed User
Longtime User
I just started this thread in the bugs section:


But Erel has decided it is a question, not a bug.

I have another example:
B4X:
Private Sub AppStart(Form1 As Form, Args() As String)

    DateTime.DateFormat="yyyy-MM-dd"

    Private wrk_datelong1, wrk_datelong2 As Long

    wrk_datelong2 = DateTime.DateParse("1985-02-27")

    wrk_datelong1 = DateTime.DateParse("1985-02-28")
    Log("1985-02-28 " & wrk_datelong1 & " " & (wrk_datelong1 - wrk_datelong2))
    wrk_datelong2 = wrk_datelong1

    wrk_datelong1 = DateTime.DateParse("1985-03-01")
    Log("1985-03-01 " & wrk_datelong1 & " " & (wrk_datelong1 - wrk_datelong2))
    wrk_datelong2 = wrk_datelong1

    wrk_datelong1 = DateTime.DateParse("1985-03-02")
    Log("1985-03-02 " & wrk_datelong1 & " " & (wrk_datelong1 - wrk_datelong2))
    wrk_datelong2 = wrk_datelong1

    wrk_datelong1 = DateTime.DateParse("1985-03-03")
    Log("1985-03-03 " & wrk_datelong1 & " " & (wrk_datelong1 - wrk_datelong2))
    wrk_datelong2 = wrk_datelong1

    wrk_datelong1 = DateTime.DateParse("1985-03-04")
    Log("1985-03-04 " & wrk_datelong1 & " " & (wrk_datelong1 - wrk_datelong2) & " <<<<<<<<<")
    wrk_datelong2 = wrk_datelong1

    wrk_datelong1 = DateTime.DateParse("1985-03-05")
    Log("1985-03-05 " & wrk_datelong1 & " " & (wrk_datelong1 - wrk_datelong2))
    
    'StartMessageLoop
    
End Sub
which when you run it gives this log:
Waiting for debugger to connect...
Program started.
1985-02-28 478357200000 86400000
1985-03-01 478443600000 86400000
1985-03-02 478530000000 86400000
1985-03-03 478616400000 86400000
1985-03-04 478706400000 90000000 <<<<<<<<<
1985-03-05 478792800000 86400000

if this an error on my part I would like to know what I am doing wrong.
 

alwaysbusy

Expert
Licensed User
Longtime User
Again, you shouldn't put it as a bug, but as a question. The chances there is a bug in B4X are phenomenally low... ?

I would look into the java SDK version you have installed, as on my PC (OpenJDK 11.0.1), I get the correct results:

B4X:
Waiting for debugger to connect...
Program started.
1985-02-28 478393200000 86400000
1985-03-01 478479600000 86400000
1985-03-02 478566000000 86400000
1985-03-03 478652400000 86400000
1985-03-04 478738800000 86400000 <<<<<<<<<
1985-03-05 478825200000 86400000
Program terminated (StartMessageLoop was not called).

Also, you shouldn't make calculations like that to find a period between dates, as it doesn't work this way. You should use the jDateUtils library for that:
B4X:
Log(DateUtils.PeriodBetween(wrk_datelong2, wrk_datelong1).Days)
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I tested it for this spring and there is the difference.

2020-03-27 1585263600000 -86400000
2020-03-28 1585350000000 -86400000
2020-03-29 1585436400000 -82800000
2020-03-30 1585519200000 -86400000

I think that the date of change is not the same around the world.
 
Upvote 0
Top