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

Da Gambas-it.org - Wikipedia.
(Creata pagina con "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 microsecon...")
 
 
Riga 37: Riga 37:
 
   
 
   
 
   
 
   
  '''Public''' Sub Main()
+
  Public Sub Main()
 +
 
 +
  Dim ts As New Timespec
 +
  Dim buff As New Byte[24]
 +
  Dim tsec As Long
 
    
 
    
  Dim ts As New Timespec
 
  Dim buff As New Byte[24]
 
  Dim tsec As Long
 
 
 
 
   clock_gettime(CLOCK_REALTIME, ts)
 
   clock_gettime(CLOCK_REALTIME, ts)
 
   tsec = ts.tv_sec
 
   tsec = ts.tv_sec
Riga 50: Riga 50:
 
   Print String@(buff.Data); ".\e[31m"; ts.tv_nsec; "\e[0m"
 
   Print String@(buff.Data); ".\e[31m"; ts.tv_nsec; "\e[0m"
 
    
 
    
  '''End'''
+
  End

Versione attuale delle 08:28, 1 lug 2024

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