I have a web app (API 1.0, so Xojo 2019r3.2) with a custom calendar control based on DateTime (Yes, I know a plug in would be much wiser in terms of performance. For reasons unknown to me by customer request).
I was just made aware that there is no week 1 when you cross next new year.
Could it be that DateTime gets WeekOfYear wrong?
The quick and dirty code that builds my date time array:
Var firstDayOfMonth As DateTime = today.firstDayOfMonth Var FirstWeekDay As Integer = firstDayOfMonth.GermanDayOfWeek Var LeadingMonthDays As Integer = FirstWeekDay-1 Var lastDayOfMonth As DateTime = Today.LastDayOfMonth Var LastWeekDay As Integer = lastDayOfMonth.GermanDayOfWeek Var TrailingMonthDays As Integer = 7 - LastWeekDay DisplayDays.ResizeTo(LeadingMonthDays-1) Var currentdatadate As New DateTime(firstDayOfMonth) For q = LeadingMonthDays -1 DownTo 0 currentdatadate = currentdatadate-New DateInterval(0, 0, 1) DisplayDays(q) = New DateTime(currentdatadate) Next currentdatadate = New DateTime(firstDayOfMonth) DisplayDays.AddRow New date(currentdatadate) For q = 2 To lastDayOfMonth.Day currentdatadate = currentdatadate+ New DateInterval(0,0,1) DisplayDays.AddRow New date(currentdatadate) Next For q = DisplayDays.LastRowIndex To 40 currentdatadate = currentdatadate+ New DateInterval(0,0,1) DisplayDays.AddRow New date(currentdatadate) Next
(So basically I just build a days array, starting with Monday of the first or leading week, adding a timeinterval of one day for each item which should be safe I guess.)
GermanDayOfWeek simply shifts dayOfWeek by one because our weeks start with Monday.
And part of the control’s paint event:
If ShowWeekNumbers Then // Wochennummern Displaywidth = Displaywidth - 35 LeftBorder = 36 g.DrawLine 35,TopBorder,35,g.Height For rows As Integer = 0 To If (Me.ViewMode = ViewModes.Monthly, 6, 0) g.DrawLine 1,TopBorder+rows*cellheight, 36, topborder+rows*cellheight If rows < 6 Then Var currentdate As DateTime = DisplayDays(rows*7) Var week As Integer = currentdate.WeekOfYear g.TextFont = "Noto Sans" g.TextSize = 12 g.DrawString week.ToString("00"), 8, TopBorder+rows*Cellheight+17 End If Next End If
That bug is not based on crossing the year. February 2021 starts with week 6 which is wrong.
Is this a known bug? Was it fixed?
(I will have to resort to a workaround anyway and think that checking for an offset – if Jan 1st of the current year is not in week one, subtract 1 – will be sufficient, but I blushed a bit. Or any mistake on my side visible?)