_ftime_s, _ftime32_s, _ftime64_s

获取当前时间。 这些函数的版本是 _ftime_ftime32_ftime64,具有安全性增强功能,如 CRT 中的安全功能中所述。

语法

errno_t _ftime_s( struct _timeb *timeptr );
errno_t _ftime32_s( struct __timeb32 *timeptr );
errno_t _ftime64_s( struct __timeb64 *timeptr );

参数

timeptr
指向 _timeb__timeb32__timeb64 结构的指针。

返回值

如果成功,返回零;如果失败,则返回错误代码。 如果 timeptrNULL,则返回值为 EINVAL

备注

_ftime_s 函数将获取当前本地时间,并将其存储在 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_s 函数允许文件创建日期最大表示为 3000 年 12 月 31 日 23:59:59,UTC;而 _ftime32_s 只能表示截至 2038 年 1 月 18 日 23:59:59,UTC 之前的日期。 1970 年 1 月 1 日午夜是所有这些函数的日期范围下限。

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

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

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

要求

函数 必需的标头
_ftime_s <sys/types.h> 和 <sys/timeb.h>
_ftime32_s <sys/types.h> 和 <sys/timeb.h>
_ftime64_s <sys/types.h> 和 <sys/timeb.h>

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

C 运行时库的所有版本。

示例

// crt_ftime64_s.c
// This program uses _ftime64_s 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;

   _ftime64_s( &timebuffer );

    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