Condividi tramite


_mkgmtime, _mkgmtime32, _mkgmtime64

Converte un'ora UTC rappresentata da un struct tm in un'ora UTC rappresentata da un tipo time_t.

Sintassi

time_t _mkgmtime(
   struct tm* timeptr
);
__time32_t _mkgmtime32(
   struct tm* timeptr
);
__time64_t _mkgmtime64(
   struct tm* timeptr
);

Parametri

timeptr
Puntatore all'ora UTC come struct tm da convertire.

Valore restituito

Una quantità di tipo __time32_t o __time64_t che rappresenta il numero di secondi trascorsi dalla mezzanotte del 1 gennaio 1970 nel formato UTC (Coordinated Universal Time). Se la data non è compreso nell'intervallo (vedere la sezione Osservazioni) o l'input non può essere interpretato come un'ora valida, il valore restituito è -1.

Osservazioni:

Le funzioni _mkgmtime32 e _mkgmtime64 convertono un'ora UTC in un __time32_t o in un tipo __time64_t che rappresenta l'ora nel formato UTC. Per convertire un'ora locale in ora UTC, usare invece mktime, _mktime32 e _mktime64.

_mkgmtime è una funzione inline equivalente a _mkgmtime64 e time_t è equivalente a __time64_t. Se è necessario forzare il compilatore in modo che interpreti time_t come il vecchio time_ta 32 bit, è possibile definire _USE_32BIT_TIME_T. Non è consigliabile, perché l'applicazione potrebbe non riuscire dopo il 18 gennaio 2038, l'intervallo massimo di un oggetto a 32 bit time_t. Non è consentito affatto su piattaforme a 64 bit.

La struttura temporale passata viene modificata come segue, nello stesso modo in cui viene modificata dalle _mktime funzioni: i tm_wday campi e tm_yday vengono impostati su nuovi valori in base ai valori di tm_mday e tm_year. Poiché si presuppone che l'ora sia UTC, il tm_isdst campo viene ignorato.

L'intervallo della funzione _mkgmtime32 è compreso tra la mezzanotte del 1 gennaio 1970 UTC alle 23.59.59 del 18 gennaio 2038 UTC. L'intervallo di _mkgmtime64 è compreso tra la mezzanotte del 1 gennaio 1970 UTC alle 23.59.59 del 31 dicembre 3000 UTC. Una data non compreso nell'intervallo restituisce un valore restituito pari a -1. L'intervallo di _mkgmtime dipende dal fatto che sia definito _USE_32BIT_TIME_T. Quando non è definito, ovvero l'impostazione predefinita, l'intervallo è uguale _mkgmtime64a . In caso contrario, l'intervallo è limitato all'intervallo a 32 bit di _mkgmtime32.

Sia gmtime che localtime usano un buffer statico comune per la conversione. Se si fornisce questo buffer a _mkgmtime, i contenuti precedenti verranno eliminati.

Esempi

// crt_mkgmtime.c
#include <stdio.h>
#include <time.h>

int main()
{
    struct tm t1, t2;
    time_t now, mytime, gmtime;
    char buff[30];

    time( & now );

    _localtime64_s( &t1, &now );
    _gmtime64_s( &t2, &now );

    mytime = mktime(&t1);
    gmtime = _mkgmtime(&t2);

    printf("Seconds since midnight, January 1, 1970\n");
    printf("My time: %I64d\nGM time (UTC): %I64d\n\n", mytime, gmtime);

    /* Use asctime_s to display these times. */

    _localtime64_s( &t1, &mytime );
    asctime_s( buff, sizeof(buff), &t1 );
    printf( "Local Time: %s\n", buff );

    _gmtime64_s( &t2, &gmtime );
    asctime_s( buff, sizeof(buff), &t2 );
    printf( "Greenwich Mean Time: %s\n", buff );

}
Seconds since midnight, January 1, 1970
My time: 1171588492
GM time (UTC): 1171588492

Local Time: Thu Feb 15 17:14:52 2007

Greenwich Mean Time: Fri Feb 16 01:14:52 2007

Nell'esempio seguente viene illustrato il modo in cui la struttura incompleta viene compilata da _mkgmtime. Calcola i valori sia per il giorno della settimana che per l'anno.

// crt_mkgmtime2.c
#include <stdio.h>
#include <time.h>
#include <memory.h>

int main()
{
    struct tm t1, t2;
    time_t gmtime;
    char buff[30];

    memset(&t1, 0, sizeof(struct tm));
    memset(&t2, 0, sizeof(struct tm));

    t1.tm_mon = 1;
    t1.tm_isdst = 0;
    t1.tm_year = 103;
    t1.tm_mday = 12;

    // The day of the week and year will be incorrect in the output here.
    asctime_s( buff, sizeof(buff), &t1);
    printf("Before calling _mkgmtime, t1 = %s t.tm_yday = %d\n",
            buff, t1.tm_yday );

    gmtime = _mkgmtime(&t1);

    // The correct day of the week and year were determined.
    asctime_s( buff, sizeof(buff), &t1);
    printf("After calling _mkgmtime, t1 = %s t.tm_yday = %d\n",
            buff, t1.tm_yday );

}
Before calling _mkgmtime, t1 = Sun Feb 12 00:00:00 2003
t.tm_yday = 0
After calling _mkgmtime, t1 = Wed Feb 12 00:00:00 2003
t.tm_yday = 42

Vedi anche

Gestione orari
asctime, _wasctime
asctime_s, _wasctime_s
gmtime, _gmtime32, _gmtime64
gmtime_s, _gmtime32_s, _gmtime64_s
localtime_s, _localtime32_s, _localtime64_s
mktime, _mktime32, _mktime64
time, _time32, _time64