Page 1 of 1

Issues with the calendar and date gadgets.

Posted: Sat Oct 22, 2022 6:16 am
by lilRalph
I don't know if this is OS specific or generalised.
I also don't know if this shows up in other countries.
When using the Calendar or Date gadget the date returned gets screwed up and becomes different at certain times of the day.
The time component seems to lock onto the time the gadget launches and is different depending on how it's accessed.
The date gets out of step possibly due to mismanagement of time zones.
I have built a small app to demonstrate the issue but if the time zone problem doesn't show up for you it won't mean anything.
Also you'll have to test it over the course of a day to see what happens.

I have built into the app the work around that I use because that seems to be consistently accurate here in Australia.
The screenshots in the link show the discrepancy in the dates reported by the Calendar and Date gadgets. The local time I collected these screenshots was 3:31 pm.

https://www.evernote.com/shard/s382/sh/ ... 138c920827

You can see that the time for GetGadgetText is wrong and the date for GetGadgetState is wrong.

For your testing the code I used is below. If I've screwed up somewhere please let me know, but I want this functionality to be cross platform.


DateTest.pbf:

Code: Select all

;
; This code is automatically generated by the FormDesigner.
; Manual modification is possible to adjust existing commands, but anything else will be dropped when the code is compiled.
; Event procedures needs to be put in another source file.
;

Global Window_2

Global Text_0, CalDate, Date_0

Enumeration FormFont
  #Font_Window_2_0
EndEnumeration

LoadFont(#Font_Window_2_0,"Founders Grotesk Text", 23, #PB_Font_Bold)


Procedure OpenWindow_2(x = 0, y = 0, width = 600, height = 400)
  Window_2 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu)
  Text_0 = TextGadget(#PB_Any, 0, 10, 600, 40, "Date test page.", #PB_Text_Center)
  SetGadgetFont(Text_0, FontID(#Font_Window_2_0))
  CalDate = CalendarGadget(#PB_Any, 180, 70, 230, 200, 0)
  Date_0 = DateGadget(#PB_Any, 170, 290, 240, 40, "")
EndProcedure

Procedure Window_2_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False

    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect

    Case #PB_Event_Gadget
      Select EventGadget()
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure
DateTest.pb

Code: Select all

Declare.s PickM(name$)
XIncludeFile "DateTest.pbf"
OpenWindow_2()


Procedure WeekOfMonth(targetDate)
  If Mod(targetDate,7) = 0
    WeekNum.i = targetDate / 7
  Else
    WeekNum.i = (targetDate / 7) + 1
  EndIf
  ProcedureReturn WeekNum
EndProcedure

Procedure.s PickM(name$)
  Select name$
    Case "January"
      Month$ = "1"
      ProcedureReturn Month$
    Case "February"
      Month$ = "2"
      ProcedureReturn Month$
    Case "March"
      Month$ = "3"
      ProcedureReturn Month$
    Case "April"
      Month$ = "4"
      ProcedureReturn Month$
    Case "May"
      Month$ = "5"
      ProcedureReturn Month$
    Case "June"
      Month$ = "6"
      ProcedureReturn Month$
    Case "July"
      Month$ = "7"
      ProcedureReturn Month$
    Case "August"
      Month$ = "8"
      ProcedureReturn Month$
    Case "September"
      Month$ = "9"
      ProcedureReturn Month$
    Case "October"
      Month$ = "10"
      ProcedureReturn Month$
    Case "November"
      Month$ = "11"
      ProcedureReturn Month$
    Case "December"
      Month$ = "12"
      ProcedureReturn Month$
  EndSelect
  
EndProcedure

; The main event loop as usual
Repeat
  event = WaitWindowEvent() ; 250)
  
  window = EventWindow()
  Select event
    Case #PB_Event_CloseWindow
      Break
      
    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case CalDate
          Cal = GetGadgetState(CalDate)
          Debug Cal
          Cal$ = FormatDate("%dd/%mm/%yyyy %hh:%ii:%ss", Cal)
          Text$ = GetGadgetText(CalDate)
          Debug "GadgetText: " + Text$ + Chr(10)  + "GadgetState: " + Cal$
          Debug "Day " + Str(Day(cal))
          Debug "Month " + Str(Month(cal))
          Debug "Today Month " + Str(Month(Date()))
          Debug "Today DoW " + Str(DayOfWeek(cal))
          Debug "Today WoM " + Str(WeekOfMonth(Day(cal)))
          ;Saturday, 22 October 2022 at 3:18:11
          Day$ = Mid(Text$,FindString(Text$, ", ") + 2, 2)
          Year$ = Mid(Text$,FindString(Text$, " 202") + 1,4)
          Month$ = Mid(Text$,FindString(Text$, Day$) + 2,FindString(Text$, Year$) - FindString(Text$, Day$)-2)
          Month$ = PickM(Trim(Month$))
          Debug Day$ + ", " + Month$ + ", " + Year$ + " from GadgetText string"
          ;Dates$ = 
          Cal2 = ParseDate("%dd/%mm/%yyyy",Day$ + "/" + Month$ + "/" + Year$)
          Debug Str(Cal2) + " -- " + Str(Day(Cal2)) + "/" + Str(Month(Cal2)) + Chr(10)
        Case target 
          Debug "Target"
      EndSelect
  EndSelect
  
ForEver


// Added Code-Tags (Kiffi)

Re: Issues with the calendar and date gadgets.

Posted: Sat Oct 22, 2022 8:26 am
by BarryG
lilRalph wrote: Sat Oct 22, 2022 6:16 amthe time for GetGadgetText is wrong
GetGadgetText() can't be used with the CalendarGadget(), so you can't get text from it like that. See the manual.

I'd like to help but need more details on what you're trying to do. I'm in Australia too (Gold Coast) so this interests me because we don't have DST.

Re: Issues with the calendar and date gadgets.

Posted: Sat Oct 22, 2022 10:45 am
by lilRalph
G'Day BarryG, I did read the manual but I tried it anyway and it does work.

What I'm building is a task manager tool for my Internet business.
I need it to tell me when a scheduled task should be done and flag when it's overdue.

I don't care about the time, but I do need the date to be correct.

Re: Issues with the calendar and date gadgets.

Posted: Sat Oct 22, 2022 10:51 am
by BarryG
lilRalph wrote: Sat Oct 22, 2022 10:45 amI tried it anyway and it does work
No, it doesn't. You're getting the text from something else, or are confused. The manual does not list GetGadgetText() as an accepted command for the CalendarGadget(), so you can't use it or rely on it. The CalendarGadget() returns a number, not a text string.

Proof:

Code: Select all

If OpenWindow(0, 400, 200, 250, 200, "CalendarGadget", #PB_Window_SystemMenu)
  CalendarGadget(0, 10, 10, 230, 180)
  Repeat
    ev = WaitWindowEvent()
    If ev = #PB_Event_Gadget
      Debug "Text = "+GetGadgetText(0) ; Never shows text for the selected date.
    EndIf
  Until ev = #PB_Event_CloseWindow
EndIf
Your code is doing this for the CalendarGadget(), which is not valid code:

Code: Select all

CalDate = CalendarGadget(#PB_Any, 180, 70, 230, 200, 0)
[...]
Text$ = GetGadgetText(CalDate)

Re: Issues with the calendar and date gadgets.

Posted: Sat Oct 22, 2022 11:14 am
by lilRalph
I didn't write this code >> CalDate = CalendarGadget(#PB_Any, 180, 70, 230, 200, 0).
That was done by the Form designer.

Where else would the text come from?
Run the code and please explain where I've gone wrong.

Actually, I ran your code snippet and got this in the debug window
Text = Sunday, 23 October 2022 at 7:15:26 am Australian Central Daylight Time.

So, is this only a Mac result?

Re: Issues with the calendar and date gadgets.

Posted: Sat Oct 22, 2022 11:27 am
by BarryG
lilRalph wrote: Sat Oct 22, 2022 11:14 am I ran your code snippet and got this in the debug window
Text = Sunday, 23 October 2022 at 7:15:26 am Australian Central Daylight Time.
So, is this only a Mac result?
Interesting. That looks like a bug, or undocumented feature for Mac. According to the manual, it's not meant to do that, and it doesn't on Windows. No wonder our wires are getting crossed, lol. I'll file a bug report.

I can't test your code because I still don't know what you're expecting to see/do. You're obviously parsing the text returned from the CalendarGadget() on Mac, which I can't duplicate here due to the PureBasic bug. Maybe someone with a Mac can take over the help.

Re: Issues with the calendar and date gadgets.

Posted: Sat Oct 22, 2022 11:34 am
by lilRalph
All I'm trying to do is get a correct date for my time zone.

As you can see with the text I put in the last post the date was wrong because here it's still the 22nd and 9 pm.
That's a long way from 23rd and 7 am.

The calendar gadget on launch shows 23rd so it's being set internally from somewhere and cannot be correct.
There is nowhere on the planet that's that date and time.
The nearest is the Chatham Islands and that is currently 12:15 AM.
Sunday, 23 October 2022 (GMT+13:45)
Time in Chatham Islands Territory, New Zealand

The next place East of that is the International Date Line.

Re: Issues with the calendar and date gadgets.

Posted: Sat Oct 22, 2022 12:06 pm
by mk-soft
The function GetGadgetText on the CalendarGadget is not a valid function and only works on macOS (there are such special cases, but they are not official).

Unfortunately Purebasic is not yet internally converted to _time64.
So the date is also limited to 1970 until 2038.
There are Date64 modules for this. But this is not executable on processor macOS M1 or Raspberry ARM, because it uses Intel ASM.

I have written a new module for Windows, Linux and macOS. The time value is always UTC to avoid the time zone problem. I would then always set the CalendarGadget to 12:00 local time.

Link: Module DateTime - Date as Double Since 1970

Re: Issues with the calendar and date gadgets.

Posted: Sat Oct 22, 2022 12:08 pm
by BarryG
lilRalph wrote: Sat Oct 22, 2022 11:34 amAll I'm trying to do is get a correct date for my time zone.
The calendar gadget on launch shows 23rd
So this shows the 23rd for you, despite being the 22nd right now? Shows 22 for me on the Gold Coast. What time zone is your Mac set to?

Code: Select all

If OpenWindow(0, 400, 200, 250, 200, "CalendarGadget", #PB_Window_SystemMenu)
  CalendarGadget(0, 10, 10, 230, 180)
  Repeat
    ev = WaitWindowEvent()
  Until ev = #PB_Event_CloseWindow
EndIf

Re: Issues with the calendar and date gadgets.

Posted: Sat Oct 22, 2022 12:22 pm
by mk-soft
macOS:

I have checked the CalendarGadget and the output again.
Here is a bug in Purebasic, because the timezone of the CalendarGadget is internally UTC (default) and not changed to local timezone. But Purebasic internally works with local timezone.

PB Bug
Text = Samstag, 22. Oktober 2022 um 15:10:29 Mitteleuropäische Sommerzeit
Module DateTime
Saturday, 22. October 2022 at 13:13:48 Central European Summer Time

Re: Issues with the calendar and date gadgets.

Posted: Sat Oct 22, 2022 1:11 pm
by mk-soft
Let's test if it is possible to set the CalendarGaget with UTC. But then you also have to calculate the reading of the date.

Code: Select all

;-TOP

Macro CocoaString(NSString)
  PeekS(CocoaMessage(0, NSString, "UTF8String"), -1, #PB_UTF8)
EndMacro

Procedure GetSecondsFromGMT()
  Protected NSPool, NSTimeZone
  Protected r1
  
  NSPool = CocoaMessage(0, 0, "NSAutoreleasePool new")
  NSTimeZone = CocoaMessage(0, 0, "NSTimeZone localTimeZone")
  r1 = CocoaMessage(0, NSTimeZone, "secondsFromGMT")
  CocoaMessage(0, NSPool, "release")
  ProcedureReturn r1
EndProcedure


Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(0)
  dy = WindowHeight(0) - StatusBarHeight(0) - MenuHeight()
  ; Resize Gadgets
EndProcedure

Procedure Main()
  Protected dx, dy
  
  #WinStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If OpenWindow(0, #PB_Ignore, #PB_Ignore, 600, 400, "Test Window", #WinStyle)
    ; MenuBar
    CreateMenu(0, WindowID(0))
    MenuTitle("File")
    
    ; StatusBar
    CreateStatusBar(0, WindowID(0))
    AddStatusBarField(#PB_Ignore)
    
    ; Gadgets
    dx = WindowWidth(0)
    dy = WindowHeight(0) - StatusBarHeight(0) - MenuHeight()
    CalendarGadget(0, 10, 10, 180, 180)
    
    ; Bind Events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), 0)
    
    ; Get user local timezone
    NSTimezone = CocoaMessage(0, 0, "NSTimeZone localTimeZone")
    Debug "TimeZone: " + CocoaString(CocoaMessage(0, NSTimezone, "name"))
    ; Set correct calendar date
    SetGadgetState(0, Date() - GetSecondsFromGMT())
    
    ; Main Loop
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case 0
              Break
          EndSelect
          
        Case #PB_Event_Menu
          Select EventMenu()
              
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case 0
              Debug "Date: " + GetGadgetText(0)
              Debug "PB: " + FormatDate("%YYYY.%MM.%DD %HH:%II:%SS", GetGadgetState(0) + GetSecondsFromGMT())
              
          EndSelect
          
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()

Re: Issues with the calendar and date gadgets.

Posted: Sat Oct 22, 2022 1:21 pm
by lilRalph
My time zone on the Mac is set to Central Australian Daylight Time.
The Mac reports the time and date currently as Sat 22 Oct 10:48 pm.

The CalendarGadget reports Sunday, 23 October 2022 at 9:19:08 am Australian Central Daylight Time.

I'll try mk-soft's code, thanks for your input, and see how that reports.
I think I can manage time zone shifts if I can get reliable UTC reporting.

OK, back from testing and that code is reporting the correct date and time.
Nice work.

That's not going to be cross-platform though is it?
How would I do the same for a Windows PC?

Re: Issues with the calendar and date gadgets.

Posted: Sat Oct 22, 2022 1:40 pm
by lilRalph
Never mind, I've tried your code in the previous link and that works fine as well.
Because it also includes code for Windows and Linux I can use that.

Thank you for your help and excellent code.

Re: Issues with the calendar and date gadgets.

Posted: Sat Oct 22, 2022 2:15 pm
by mk-soft
Found another bug in my DateTime module in the Linux part and fixed it. The time value is always UTC and I had forgotten to set the CalendarGadget output to Local.
So now for all OS (Window, macOS, Linux) the date value (UTC) of the CalendarGadget should always show the correct local date.

Info Module DateTime:
If you pass a date string as UTC, you must set the parameter local to False.

Re: Issues with the calendar and date gadgets.

Posted: Sat Oct 22, 2022 11:18 pm
by lilRalph
Thanks for your assistance mk-soft.

My ability to read and fully follow your code has not helped me with finding the bug mentioned.
Found another bug in my DateTime module in the Linux part and fixed it. The time value is always UTC and I had forgotten to set the CalendarGadget output to Local.
Can you either give more details or point me to the corrected code?