@@ -314,12 +314,14 @@ class _CommonSDLEventAttributes(TypedDict):
314314 """Common keywords for Event subclasses."""
315315
316316 sdl_event : _C_SDL_Event
317+ timestamp_ns : int
317318
318319
319320def _unpack_sdl_event (sdl_event : _C_SDL_Event ) -> _CommonSDLEventAttributes :
320321 """Unpack an SDL_Event union into common attributes, such as timestamp."""
321322 return {
322323 "sdl_event" : sdl_event ,
324+ "timestamp_ns" : sdl_event .common .timestamp ,
323325 }
324326
325327
@@ -328,7 +330,27 @@ class Event:
328330 """The base event class."""
329331
330332 sdl_event : _C_SDL_Event = attrs .field (default = None , eq = False , repr = False )
331- """When available, this holds a python-cffi 'SDL_Event*' pointer. All sub-classes have this attribute."""
333+ """Holds a python-cffi ``SDL_Event*`` pointer for this event when available."""
334+
335+ timestamp_ns : int = attrs .field (default = 0 , eq = False )
336+ """The time of this event in nanoseconds since SDL has been initialized.
337+
338+ .. seealso::
339+ :any:`tcod.event.time_ns`
340+
341+ .. versionadded:: Unreleased
342+ """
343+
344+ @property
345+ def timestamp (self ) -> float :
346+ """The time of this event in seconds since SDL has been initialized.
347+
348+ .. seealso::
349+ :any:`tcod.event.time`
350+
351+ .. versionadded:: Unreleased
352+ """
353+ return self .timestamp_ns / 1_000_000_000
332354
333355 @property
334356 @deprecated ("The Event.type attribute is deprecated, use isinstance instead." )
@@ -337,6 +359,8 @@ def type(self) -> str:
337359
338360 .. deprecated:: Unreleased
339361 Using this attribute is now actively discouraged. Use :func:`isinstance` or :ref:`match`.
362+
363+ :meta private:
340364 """
341365 type_override : str | None = getattr (self , "_type" , None )
342366 if type_override is not None :
@@ -3070,6 +3094,22 @@ def __getattr__(name: str) -> int:
30703094 return value
30713095
30723096
3097+ def time_ns () -> int :
3098+ """Return the nanoseconds elapsed since SDL was initialized.
3099+
3100+ .. versionadded:: Unreleased
3101+ """
3102+ return int (lib .SDL_GetTicksNS ())
3103+
3104+
3105+ def time () -> float :
3106+ """Return the seconds elapsed since SDL was initialized.
3107+
3108+ .. versionadded:: Unreleased
3109+ """
3110+ return time_ns () / 1_000_000_000
3111+
3112+
30733113__all__ = ( # noqa: F405 RUF022
30743114 "Point" ,
30753115 "Modifier" ,
@@ -3111,6 +3151,8 @@ def __getattr__(name: str) -> int:
31113151 "get_modifier_state" ,
31123152 "Scancode" ,
31133153 "KeySym" ,
3154+ "time_ns" ,
3155+ "time" ,
31143156 # --- From event_constants.py ---
31153157 "MOUSEWHEEL_NORMAL" ,
31163158 "MOUSEWHEEL_FLIPPED" ,
0 commit comments