Ottenere l'orario locale comprensivo dei nanosecondi con le risorse di alcune librerie standard C

Da Gambas-it.org - Wikipedia.

E' possibile ottenere l'orario locale comprensivo dei nanosecondi con le risorse di alcune librerie standard C.

Mostriamo un semplice esempio pratico (la parte dei microsecondi apparirà in console colorata di rosso):

Library "libc:6"

Public Struct timespec
  tv_sec As Long
  tv_nsec As Long
End Struct

Public Struct tm
  tm_sec As Integer
  tm_min As Integer
  tm_hour As Integer
  tm_mday As Integer
  tm_mon As Integer
  tm_year As Integer
  tm_yday As Integer
  tm_isdst As Integer
  tm_gmtoff As Long
  tm_zone As Pointer
End Struct

Private Const CLOCK_REALTIME As Integer = 0

' int clock_gettime (clockid_t __clock_id, struct timespec *__tp)
' Get current value of clock CLOCK_ID and store it in TP.
Private Extern clock_gettime(__clock_id As Integer, __tp As Timespec) As Integer

' size_t strftime (char *__restrict __s, size_t __maxsize, const char *__restrict __format, const struct tm *__restrict __tp)
' Format TP into S according to FORMAT.
Private Extern strftime(__s As Pointer, __maxsize As Long, __format As String, __tp As Tm) As Long

' struct tm *localtime (const time_t *__timer)
' Return the `struct tm' representation of *TIMER in the local timezone.
Private Extern localtime(__timer As Pointer) As Tm


Public Sub Main()
 
 Dim ts As New Timespec
 Dim buff As New Byte[24]
 Dim tsec As Long
  
  clock_gettime(CLOCK_REALTIME, ts)
  tsec = ts.tv_sec
  
  strftime(buff.Data, CLong(buff.Count), "%D %T", localtime(VarPtr(tsec)))
  
  Print String@(buff.Data); ".\e[31m"; ts.tv_nsec; "\e[0m"
  
End