time, _time32, _time64

获取系统时间。

time_t time(
   time_t *timer 
);
__time32_t _time32(
   __time32_t *timer 
);
__time64_t _time64(
   __time64_t *timer 
);

参数

  • timer
    对存储位置的指针时的。

返回值

返回时,秒从零开始, -1 年一月 1 日, 1970 年或 elapsed 在错误。

备注

time 函数返回秒数自午夜 (00:00 elapsed: 00), 1970 年一月 1 日,协调世界 (UTC)时 (utc),根据系统时钟。 返回值。 timer给定位置的存储。 ,在不存储情况下,此参数可以为 NULL返回值。

time 是 _time64 的包装,默认情况下,因此, time_t 与 __time64_t等效。 如果需要强制编译器解释 time_t 为旧 32 位 time_t,可以定义 _USE_32BIT_TIME_T。 这样做,因为应用程序可以在 2038 中,一月 18 日之后失败不建议使用;使用此宏在 64 位平台不允许的。

要求

实例

必需的头

time

time.h

_time32, _time64

time.h

有关其他的兼容性信息,请参见中介绍的 兼容性

示例

// crt_times.c
// compile with: /W3
// This program demonstrates these time and date functions:
//      time         _ftime    ctime_s     asctime_s
//      _localtime64_s    _gmtime64_s    mktime    _tzset
//      _strtime_s     _strdate_s  strftime
//
// Also the global variable:
//      _tzname
//

#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>

int main()
{
    char tmpbuf[128], timebuf[26], ampm[] = "AM";
    time_t ltime;
    struct _timeb tstruct;
    struct tm today, gmt, xmas = { 0, 0, 12, 25, 11, 93 };
    errno_t err;

    // Set time zone from TZ environment variable. If TZ is not set,
    // the operating system is queried to obtain the default value 
    // for the variable. 
    //
    _tzset();

    // Display operating system-style date and time. 
    _strtime_s( tmpbuf, 128 );
    printf( "OS time:\t\t\t\t%s\n", tmpbuf );
    _strdate_s( tmpbuf, 128 );
    printf( "OS date:\t\t\t\t%s\n", tmpbuf );

    // Get UNIX-style time and display as number and string. 
    time( &ltime );
    printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime );
    err = ctime_s(timebuf, 26, &ltime);
    if (err)
    {
       printf("ctime_s failed due to an invalid argument.");
       exit(1);
    }
    printf( "UNIX time and date:\t\t\t%s", timebuf );

    // Display UTC. 
    err = _gmtime64_s( &gmt, &ltime );
    if (err)
    {
       printf("_gmtime64_s failed due to an invalid argument.");
    }
    err = asctime_s(timebuf, 26, &gmt);
    if (err)
    {
       printf("asctime_s failed due to an invalid argument.");
       exit(1);
    }
    printf( "Coordinated universal time:\t\t%s", timebuf );

    // Convert to time structure and adjust for PM if necessary. 
    err = _localtime64_s( &today, &ltime );
    if (err)
    {
       printf("_localtime64_s failed due to an invalid argument.");
       exit(1);
    }
    if( today.tm_hour >= 12 )
    {
   strcpy_s( ampm, sizeof(ampm), "PM" );
   today.tm_hour -= 12;
    }
    if( today.tm_hour == 0 )  // Adjust if midnight hour.
   today.tm_hour = 12;

    // Convert today into an ASCII string 
    err = asctime_s(timebuf, 26, &today);
    if (err)
    {
       printf("asctime_s failed due to an invalid argument.");
       exit(1);
    }

    // Note how pointer addition is used to skip the first 11 
    // characters and printf is used to trim off terminating 
    // characters.
    //
    printf( "12-hour time:\t\t\t\t%.8s %s\n",
       timebuf + 11, ampm );

    // Print additional time information. 
    _ftime( &tstruct ); // C4996
    // Note: _ftime is deprecated; consider using _ftime_s instead
    printf( "Plus milliseconds:\t\t\t%u\n", tstruct.millitm );
    printf( "Zone difference in hours from UTC:\t%u\n", 
             tstruct.timezone/60 );
    printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] ); //C4996
    // Note: _tzname is deprecated; consider using _get_tzname
    printf( "Daylight savings:\t\t\t%s\n", 
             tstruct.dstflag ? "YES" : "NO" );

    // Make time for noon on Christmas, 1993. 
    if( mktime( &xmas ) != (time_t)-1 )
    {
       err = asctime_s(timebuf, 26, &xmas);
       if (err)
       {
          printf("asctime_s failed due to an invalid argument.");
          exit(1);
       }
       printf( "Christmas\t\t\t\t%s\n", timebuf );
    }

    // Use time structure to build a customized time string. 
    err = _localtime64_s( &today, &ltime );
    if (err)
    {
        printf(" _localtime64_s failed due to invalid arguments.");
        exit(1);
    }

    // Use strftime to build a customized time string. 
    strftime( tmpbuf, 128,
         "Today is %A, day %d of %B in the year %Y.\n", &today );
    printf( tmpbuf );
}
  

.NET Framework 等效项

不适用。若要调用标准 C 函数,请使用 PInvoke。有关更多信息,请参见 平台调用示例

请参见

参考

时间线

asctime, _wasctime

asctime_s, _wasctime_s

_ftime, _ftime32, _ftime64

gmtime, _gmtime32, _gmtime64

gmtime_s, _gmtime32_s, _gmtime64_s

localtime, _localtime32, _localtime64

localtime_s, _localtime32_s, _localtime64_s

_utime, _utime32 _utime64, _wutime, _wutime32, _wutime64