Android Question Datetime problem with first Sundays in November

Cliff McKibbin

Member
Licensed User
While developing a calendar view I built an array of dates for the month starting with the 1st. I was also computing the day of the week in order to populate the calendar array. When I tested November, 2020, the 1st Monday ended up with the same date as Sunday. In further testing, I realized it is consistent in November of any year. The following code illustrates the problem in years 2020 to 2023.
I originally looped through all months but found no problems in any other months.

You will note in the log that the 1st monday Lg value is incremented properly by 864 ticks per day but it resolves to the same date as the previous Sunday.

I am on b4a version 10.2. Java version 8.
Sorry for the double letter vars (ii, jj..) I tested in my real program and didn't want to scrap on other vars.
Sorry I can't find how to copy the log so I could include it. Let me know how for next time.
Thanks, Cliff



Dim ii,jj,kk As Int
Dim lg As Long
Dim xx As String

Log ("Start November Date Check")
DateTime.DateFormat="yyyy/MM/dd"
For jj=2020 To 2023
xx=jj & "/" & "11/01"
Log (" ")

lg=DateTime.DateParse(xx)
For ii=1 To 10
xx=DateTime.Date(lg)
kk=xx.SubString2(8,10)
If kk<>ii Then
Log (ii & " " & xx & " " & DateTime.GetDayOfWeek(lg) & " " & lg & " Error in day")
Exit
Else
' ok
Log (ii & " " & xx & " " & DateTime.GetDayOfweek(lg) & " " & lg)

End If
lg=lg+DateTime.TicksPerDay
Next
Next
 

drgottjr

Expert
Licensed User
Longtime User
i didn't look at your code (why spoil the surprise?:)), but the first thing that jumps to mind is the change to standard time.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
In EST daylight saving time ends on November 1. Adding ticks per day does not account for that. No bug.

B.T.W. Your code is hard to read without code tags.
 
Upvote 0

hzq200409

Member
Licensed User
I'm sorry. My English is poor.If I misunderstand you, please forgive me.
You're saying that 2020/11/1 is supposed to be Sunday, and here it says 1, right?
I don't know if you've noticed.This is the description of DateTime.GetDayOfWeek:
DateTime.GetDayOfWeek
Method
Returns the day of week component from the ticks value.
Values are between 1 to 7, where 1 means Sunday.
You can use the AHLocale library if you need to change the first day.
 
Last edited:
Upvote 0

hzq200409

Member
Licensed User
Maybe this is a BUG.I tested the following code and it also calculated incorrectly.
B4X:
    DateTime.DateFormat="yyyy/MM/dd HH:mm:ss"
    lg = DateTime.DateParse("2020/05/03 15:23:45")
    Log(DateTime.GetDayOfMonth(lg)) 'Results: 3  It's actually Sunday. I don't understand the rules .
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Moved to the questions forum.
2. Please use [code]code here...[/code] tags when posting code.
3. When you see code with DateTime.TicksPerDay you can immediately know that it will not be 100% accurate. The assumption that there are 24 hours per day is wrong.

It is explained in the date and time video tutorial: https://www.b4x.com/etp.html

You should use DateUtils.AddPeriod.
 
Upvote 0

Cliff McKibbin

Member
Licensed User
Thank you drgottrjr and Wil for bringing up the answer of 'Daylight' savings. Sunday, November 1, 2020, is the termination date for Daylight savings in the US. That day actually has 25 hours and the addition of DateTime.TicksPerDay is apparently only adding the standard 24 hours, thus leaving the date one hour short of the 'next day' which should be Monday 10/2.

I did find a reference to the issue in the forum entry from Erel on 'How many ticks in a day?'. Erel suggests the only correct way to make these calculations is with DateUtils.

A workaround in my example would be to set the Lg starting value at:
lg=DateTime.DateParse(xx)+DateTime.TicksPerHour * 4
By adding 4 hours we are placing the date 4 hours into each day when we add 'ticksperday' which eliminates the problem even if we are 1 hour off.

Thanks again to all for your help. Cliff
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
I don't know why you wouldn't use DateUtils, it is just a click on the libraries list.

Your work-around works, but hides the variation in day length in Daylight saving's in the Fall and the Spring. Of course it also varies with locale.
For example, in Canada, all of Saskatchewan, and parts of Ontario, Quebec, and British Columbia do not use it.

I have struggled with date time computation problems and as Erel urges, date time calculations are best left to the specialist(s) who wrote the utilities.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
@Erel : since a top secret was disclosed, should we now kill anyone reading the post? :D

ps: I stopped reading just at the words "top secret", so I should be safe... eheh
 
Upvote 0
Top