Sapere quanti nanosecondi sono passati dalla data iniziale del tempo Unix sino alla data e all'orario correnti
Da Gambas-it.org - Wikipedia.
Versione del 7 apr 2018 alle 11:14 di Vuott (Discussione | contributi)
Per sapere quanti nanosecondi sono passati dalla data iniziale del tempo "Unix" sino alla data e all'orario correnti, si possono adottare almeno due modalità.
1) è possibile utilizzare la funzione esterna clock_gettime( ) della libreria standard C "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 spec.tv_sec; ",\e[31m"; 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 ' 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