Differenze tra le versioni di "Sapere quanti nanosecondi sono passati dalla data iniziale del tempo Unix sino alla data e all'orario correnti"

Da Gambas-it.org - Wikipedia.
 
(2 versioni intermedie di uno stesso utente non sono mostrate)
Riga 1: Riga 1:
Per sapere quanti nanosecondi sono passati dalla data iniziale del tempo "''[https://it.wikipedia.org/wiki/Tempo_(Unix) Unix]''" sino alla data e all'orario correnti, si possono adottare almeno due modalità.
+
Per sapere quanti nanosecondi sono passati dalla data iniziale del tempo "''[https://it.wikipedia.org/wiki/Tempo_(Unix) Unix]''" sino alla data e all'orario correnti, è possibile utilizzare la funzione esterna "clock_gettime()" della libreria standard C e definita nel file header "''/usr/include/time.h'' ".
 
 
1) è possibile utilizzare la funzione esterna ''clock_gettime( )'' della libreria standard C "''time.h''":
 
 
  Library "libc:6"
 
  Library "libc:6"
 
   
 
   
Riga 18: Riga 16:
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()
 
    
 
    
  Dim spec As New Timespec
+
  Dim spec As New Timespec
 
    
 
    
 
   clock_gettime(CLOCK_REALTIME, spec)
 
   clock_gettime(CLOCK_REALTIME, spec)
 
    
 
    
   Print spec.tv_sec; ",\e[31m"; spec.tv_nsec; "\e[0m"
+
   Print "\e[31m"; (spec.tv_sec * 1000000000) + spec.tv_nsec; "\e[0m"
 
 
'''End'''
 
 
 
 
 
2) è possibile utilizzare le funzioni esterne ''clock_gettime( )'', ''strftime( )'' e ''localtime( )'':
 
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
 
 
<FONT Color=gray>' ''int clock_gettime (clockid_t __clock_id, struct timespec *__tp)''
 
' ''Get current value of clock CLOCK_ID and store it in TP.''</font>
 
Private Extern clock_gettime(__clock_id As Integer, __tp As Timespec) As Integer
 
 
<FONT Color=gray>' ''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.''</font>
 
Private Extern strftime(__s As Pointer, __maxsize As Long, __format As String, __tp As Tm) As Long
 
 
<FONT Color=gray>' ''struct tm *localtime (const time_t *__timer)''
 
' ''Return the `struct tm' representation of *TIMER in the local timezone.''</font>
 
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'''
 
  '''End'''

Versione attuale delle 08:31, 17 ago 2022

Per sapere quanti nanosecondi sono passati dalla data iniziale del tempo "Unix" sino alla data e all'orario correnti, è possibile utilizzare la funzione esterna "clock_gettime()" della libreria standard C e definita nel file header "/usr/include/time.h ".

Library "libc:6"

Public Struct timespec
  tv_sec As Long
  tv_nsec As Long
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


Public Sub Main()
 
  Dim spec As New Timespec
 
  clock_gettime(CLOCK_REALTIME, spec)
  
  Print "\e[31m"; (spec.tv_sec * 1000000000) + spec.tv_nsec; "\e[0m"
  
End