Export (0) Print
Expand All
Expand Minimize

_mkgmtime, _mkgmtime32, _mkgmtime64 

Converts a local time to a calendar value UTC time.

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



A pointer to the time as a struct tm to convert.

A quantity of type __time32_t or __time64_t representing the number of seconds elapsed since midnight, January 1, 1970, in Coordinated Universal Time (UTC). If the date is out of range (see the Remarks section) or the input cannot be interpreted as a valid time, the return value is –1.

The _mkgmtime32 and _mkgmtime64 functions convert a local time to a __time32_t or __time64_t type representing the time in UTC. Otherwise, this family of functions is analogous to mktime, _mktime32, and _mktime64.

In Visual C++ 2005, _mkgmtime is an inline function that evaluates to _mkgmtime64, and time_t is equivalent to __time64_t. If you need to force the compiler to interpret time_t as the old 32-bit time_t, you can define _USE_32BIT_TIME_T. This is not recommended because your application might fail after January 19, 2038 (the maximum range of a 32-bit time_t), and it is not allowed at all on 64-bit platforms.

The time structure passed in will be changed as follows, in the same way as they are changed with the _mktime functions: the tm_wday and tm_yday fields are set to new values based on the values of tm_mday and tm_year. When specifying a tm structure time, set the tm_isdst field to:

  • Zero (0) to indicate that standard time is in effect.

  • A value greater than 0 to indicate that daylight saving time is in effect.

  • A value less than zero to have the C run-time library code compute whether standard time or daylight saving time is in effect.

The C run-time library uses the TZ environment variable to determine the correct daylight savings time. If TZ is not set, the operating system is queried to get the correct regional daylight savings time behavior. tm_isdst is a required field. If not set, its value is undefined and the return value from mktime is unpredictable.

The range of the _mkgmtime32 function is from midnight, January 1, 1970, UTC to January 19, 3:14:07, 2038, UTC. The range of _mkgmtime64 is from midnight, January 1, 1970, UTC to 23:59:59, December 31, 3000, UTC. An out-of-range date results in a return value of –1. The range of _mkgmtime depends on whether _USE_32BIT_TIME_T is defined. If not defined (the default) the range is that of _mkgmtime64; otherwise, the range is limited to the 32-bit range of _mkgmtime32.

The time passed in is interpreted as a local time and is adjusted to UTC based on the TZ environment variable. If the local time is close to the range limits, it might lie outside the range limit when converted to UTC. If this occurs, the return value is 1.

Note that gmtime and localtime use a single statically allocated buffer for the conversion. If you supply this buffer to mkgmtime, the previous contents are destroyed.

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

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

    time( & now );

    _localtime64_s( &t1, &now );
    mytime = mktime(&t1);
    gmtime = _mkgmtime(&t1);

    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 );
    _localtime64_s( &t1, &gmtime );
    asctime_s( buff, sizeof(buff), &t1 );
    printf( "Greenwich Mean Time: %s\n", buff );

Seconds since midnight, January 1, 1970
My time: 1051302521
GM time (UTC): 1051277321

Local Time: Fri Apr 25 13:28:41 2003

Greenwich Mean Time: Fri Apr 25 06:28:41 2003

The following example shows how the incomplete structure is filled out with the computed values of the day of the week and the day of the year.

// 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

Community Additions

© 2015 Microsoft