Gmtime()

Da Gambas-it.org - Wikipedia.

La funzione della libreria di C

struct tm *gmtime(const time_t *timer);

utilizza il valore puntato dalla funzione timer() per riempire una struttura con i valori che rappresentano il tempo corrispondente, espressi in tempo universale coordinato (UTC) o GMT fuso orario.

In Gambas, bisognerà innanzitutto dichiarare la prevista Struttura tm:

Public Struct tm
  tm_sec As Integer      ' secondi (0 - 59)
  tm_min As Integer      ' minuti (0 - 59)
  tm_hour As Integer     ' ore (0 - 23)
  tm_mday As Integer     ' Giorno del mese (1 - 31)
  tm_mon As Integer      ' Mese (1 - 12)
  tm_year As Integer     ' Anno (numero di anni dal 1900)
  tm_wday As Integer     ' Giorno della Settimana (0 - 6)
  tm_yday As Integer     ' Giorno nell'Anno (0 - 365)
  tm_isdst As Integer    ' ora legale
End Struct

inoltre si dovrà dichiarare la fuzione gmtime() con Extern, nonché dichiarare la libreria di C: libc.so.6, nella quale la funzione è contenuta:

Private Extern gmtime(time_p As Pointer) As Tm In "libc:6"


Semplice esempio di uso in Gambas insieme con la funzione di C Time() per ottenere l'orario corrente:

Public Struct tm
  tm_sec As Integer      ' secondi (0 - 59)
  tm_min As Integer      ' minuti (0 - 59)
  tm_hour As Integer     ' ore (0 - 23)
  tm_mday As Integer     ' Giorno del mese (1 - 31)
  tm_mon As Integer      ' Mese (1 - 12)
  tm_year As Integer     ' Anno (numero di anni dal 1900)
  tm_wday As Integer     ' Giorno della Settimana (0 - 6)
  tm_yday As Integer     ' Giorno nell'Anno (0 - 365)
  tm_isdst As Integer    ' ora legale
End Struct

 Library "libc:6"
' time_t time(time_t *t)
' Poiché la funzione "time" ha lo steso nome della funzione "Time" i Gambas, le cambiamo nome,
' ma contestuamente dichiariamo il suo vero nome con "Exec":
Private Extern Ora(t As Pointer) As Integer Exec "time"

' struct tm *gmtime(const time_t *timer)
Private Extern gmtime(time_p As Pointer) As Tm


Public Sub Main()

 Dim nunc As Pointer
 Dim tempus As New Tm
 
' Legge il corrente orario del sistema:
  ora(VarPtr(nunc))
  
' Converte l'orario del sistema in GMT (ora UTC):
  tempus = gmtime(VarPtr(nunc))

' Mostra l'orario corrente valido per l'Italia:
  Print "Orario: "; (tempus.tm_hour + 2) Mod 24; ":"; Format(tempus.tm_min, "00"); ":"; Format(tempus.tm_sec, "00")

End