_ftime, _ftime32, _ftime64

获取当前时间。 提供这些函数的更安全版本;请参阅 _ftime_s_ftime32_s_ftime64_s

语法

void _ftime( struct _timeb *timeptr );
void _ftime32( struct __timeb32 *timeptr );
void _ftime64( struct __timeb64 *timeptr );

参数

timeptr
指向 _timeb__timeb32__timeb64 结构的指针。

备注

_ftime 函数将获取当前本地时间,并将其存储在 timeptr 所指向的结构中。 _timeb__timeb32__timeb64 结构在 <sys\timeb.h> 中定义。 它们包含下表中列出的四个字段。

字段 说明
dstflag 如果本地时区目前正在实行夏令时,则为非零。 (请参阅 _tzset,了解如何确定夏令时。)
millitm 秒的分数(以毫秒为单位)。
time 自 1970 年 1 月 1 日午夜 (00: 00:00) 以来的时间(以秒为单位),格式为协调世界时 (UTC)。
timezone 从东向西,UTC 与本地时间之间的差值(以分钟为单位)。 根据全局变量 _timezone 设置的 timezone 值(请参阅 _tzset)。

如果使用 __timeb64 结构的 _ftime64 函数,文件创建日期最大可表示为 3000 年 12 月 31 日 23:59:59 (UTC);而 _ftime32 只能表示截至 2038 年 1 月 18 日 23:59:59,UTC 之前的日期。 1970 年 1 月 1 日午夜是所有这些函数的日期范围下限。

_ftime 函数等效于 _ftime64,并且 _timeb 包含 64 位时间,除非定义了 _USE_32BIT_TIME_T,在这种情况下旧行为有效;_ftime 使用 32 位时间,并且 _timeb 包含 32 位时间。

_ftime 会验证其参数。 如果将空指针传递为 timeptr,此函数会调用无效参数处理程序,如参数验证中所述。 如果允许执行继续,则该函数将 errno 设置为 EINVAL

默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态

要求

函数 必需的标头
_ftime <sys/types.h> 和 <sys/timeb.h>
_ftime32 <sys/types.h> 和 <sys/timeb.h>
_ftime64 <sys/types.h> 和 <sys/timeb.h>

有关兼容性的详细信息,请参阅 兼容性

示例

// crt_ftime.c
// compile with: /W3
// This program uses _ftime to obtain the current
// time and then stores this time in timebuffer.

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

int main( void )
{
   struct _timeb timebuffer;
   char timeline[26];
   errno_t err;
   time_t time1;
   unsigned short millitm1;
   short timezone1;
   short dstflag1;

   _ftime( &timebuffer ); // C4996
   // Note: _ftime is deprecated; consider using _ftime_s instead

   time1 = timebuffer.time;
   millitm1 = timebuffer.millitm;
   timezone1 = timebuffer.timezone;
   dstflag1 = timebuffer.dstflag;

   printf( "Seconds since midnight, January 1, 1970 (UTC): %I64d\n",
   time1);
   printf( "Milliseconds: %d\n", millitm1);
   printf( "Minutes between UTC and local time: %d\n", timezone1);
   printf( "Daylight savings time flag (1 means Daylight time is in "
           "effect): %d\n", dstflag1);

   err = ctime_s( timeline, 26, & ( timebuffer.time ) );
   if (err)
   {
       printf("Invalid argument to ctime_s. ");
   }
   printf( "The time is %.19s.%hu %s", timeline, timebuffer.millitm,
           &timeline[20] );
}
Seconds since midnight, January 1, 1970 (UTC): 1051553334
Milliseconds: 230
Minutes between UTC and local time: 480
Daylight savings time flag (1 means Daylight time is in effect): 1
The time is Mon Apr 28 11:08:54.230 2003

另请参阅

工时管理
asctime_wasctime
ctime_ctime32_ctime64_wctime_wctime32_wctime64
gmtime_gmtime32_gmtime64
localtime_localtime32_localtime64
time_time32_time64