atomic time with websocket
Posted: Tue Oct 10, 2023 9:18 am
In case someone needs the exact time of the day 
Attention, UTC time is supplied and would have to be converted to local time if necessary. Does anyone have any ideas?

Attention, UTC time is supplied and would have to be converted to local time if necessary. Does anyone have any ideas?
Code: Select all
; Get the current atomic time from the PTB
; https://www.ptb.de/cms/fileadmin/internet/publikationen/ptb_mitteilungen/mitt2019/PTB-Mitteilungen_2019_Heft_4.pdf
; by dige 10/2023
Structure _PTB
s.s
e.s
c.s
EndStructure
Procedure Events()
Static count = 0
Protected ptb._PTB
Select Event()
Case #PB_Event_Gadget
Select EventGadget()
Case 0
txt.s = ~"{\"c\":" + Str(count) + "}"
*Buffer = AllocateMemory(Len(txt))
PokeS(*Buffer, 0, txt)
SendWebSocketData(2, *Buffer, 0, Len(txt))
count + 1
EndSelect
Case #PB_Event_WebSocket
Select EventType()
Case #PB_EventType_Connected
Debug "WebSocket #" + EventWebSocket() + " connected."
Case #PB_EventType_Closed
Debug "WebSocket #" + EventWebSocket() + " closed."
Case #PB_EventType_Data
Debug "Data recieved on WebSocket #" + EventWebSocket() + "."
*Buffer = EventData()
Debug "Data size: " + MemorySize(*Buffer) + ". First byte: " + PeekA(*Buffer, 0)
; Don't forget to free the buffer
FreeMemory(*Buffer)
Case #PB_EventType_String
If ParseJSON(0, EventString())
ExtractJSONStructure(JSONValue(0), @ptb, _PTB)
FreeJSON(0)
EndIf
Debug "UTC:" + FormatDate("%dd.%mm.%yyyy %hh:%ii:%ss", Val(ptb\s)/1000)
Case #PB_EventType_Error
Debug "Error on WebSocket #" + EventWebSocket() + "."
Default
Debug "Unknown Event"
EndSelect
EndSelect
EndProcedure
BindEvent(#PB_Event_Gadget, @Events())
BindEvent(#PB_Event_WebSocket, @Events())
OpenWindow(0, 100, 100, 220, 50, "WebSocket test")
ButtonGadget(0, 10, 10, 200, 30, "Send data !")
If OpenWebSocket(2, "wss://uhr.ptb.de/time")
Debug "Trying to open the websocket"
Else
Debug "Web socket not supported."
EndIf