localtime_s, _localtime32_s, _localtime64_s

转换时间值并为本地时区更正。 这些是 localtime, _localtime32, _localtime64 的版本与安全增强的 CRT中的安全功能如中所述。

errno_t localtime_s(
   struct tm* _tm,
   const time_t *time 
);
errno_t _localtime32_s(
   struct tm* _tm,
   const time32_t *time 
);
errno_t _localtime64_s(
   struct tm* _tm,
   const _time64_t *time 
);

参数

  • _tm
    要填充的机制的指针。

  • time
    为内存的指针。

返回值

零,如果成功。 ,如果有错误,则返回值是错误代码。 错误代码。 Errno.h 定义。 有关列出了这些错误,请参见 errno

错误状态

_tm

time

返回值

在 _tm的值

调用无效参数处理程序

NULL

任何

EINVAL

不修改

不是 NULL (指向有效的内存)

NULL

EINVAL

所有字段设置为 -1

不是 NULL (指向有效的内存)

小于 0 或大于 _MAX__TIME64_T

EINVAL

所有字段设置为 -1

在前两个错误条件,无效参数调用处理程序,如 参数验证所述。 如果执行允许继续,对 EINVAL 的这些功能集 errno 和返回 EINVAL。

备注

_localtime32_s 功能在类型 tm结构强制转换为 time_t 值存储的时间并存储结果。 long 值 timer 秒表示自午夜 (00:00 elapsed: 00), 1970 年一月 1 日, UTC。 此值从 time 功能通常获取。

,如果用户首先设置全局环境变量 TZ,_localtime32_s 为本地时区更正。 当 TZ 设置时,其他三个环境变量 (_timezone、 _daylight和 _tzname) 自动设置为。 如果 TZ 变量未设置, localtime32_s 尝试在 " 控制面板 " 中用于日期/时间应用程序指定时区的信息。 如果此信息无法获取,默认情况下,表示太平洋时区,使用 PST8PDT。 有关这些变量的声明参见 _tzset 。 TZ 是 localtime的 ANSI 标准定义的而不是 Microsoft 扩展 " 一节。

备注

目标环境应尝试确定夏时制是否有效。

_localtime64_s,使用 __time64_t 结构,允许日期下午 23:59 表示: 59, 3000 年十二月 31 日,,协调世界 (UTC)时 (utc),,而 _localtime32_s 下午 03:14 表示日期: 一月 07 日 19 日 2038 中, UTC。

localtime_s 是计算结果为 _localtime64_s的内联函数,并且, time_t 与 __time64_t等效。 如果需要强制编译器解释 time_t 为旧 32 位 time_t,可以定义 _USE_32BIT_TIME_T。 这样做将导致 localtime_s 计算为 _localtime32_s。 建议不要这样做,因为应用程序可以在 2038 年一月 19 日之后,失败,并且在 64 位平台不允许的。

结构类型 tm 字段存储下列值,每个都是 int。

  • tm_sec
    秒数分钟 (0 – 59) 之后。

  • tm_min
    分钟数小时 (0 – 59) 之后。

  • tm_hour
    在午夜 (0 – 23) 之后的小时数。

  • tm_mday
    日期月份 (1 – 31)。

  • tm_mon
    月份 (0 – 11;一月 = 0)。

  • tm_year
    年份 (减号 1900 的当前年份)。

  • tm_wday
    每周日期 (0 – 6;sunday = 0)。

  • tm_yday
    日、 (0 – 365;一月 1 日 = 0 日)。

  • tm_isdst
    正值,如果夏时制实际上是;0,如果夏时制不起作用;负值,如果夏时制的状态是未知的。 如果 TZ 环境变量设置, C 运行库假定规则适用于实现的夏时制的计算美国 (DST)。

要求

实例

必需的头

localtime_s

time.h

_localtime32_s

time.h

_localtime64_s

time.h

有关更多兼容性信息,请参见中介绍的 兼容性

示例

// crt_localtime_s.c
/* This program uses _time64 to get the current time 
 * and then uses _localtime64_s() to convert this time to a structure 
 * representing the local time. The program converts the result 
 * from a 24-hour clock to a 12-hour clock and determines the 
 * proper extension (AM or PM).
 */

#include <stdio.h>
#include <string.h>
#include <time.h>

int main( void )
{
        struct tm newtime;
        char am_pm[] = "AM";
        __time64_t long_time;
        char timebuf[26];
        errno_t err;

        // Get time as 64-bit integer.
        _time64( &long_time ); 
        // Convert to local time.
        err = _localtime64_s( &newtime, &long_time ); 
        if (err)
        {
            printf("Invalid argument to _localtime64_s.");
            exit(1);
        }
        if( newtime.tm_hour > 12 )        // Set up extension. 
                strcpy_s( am_pm, sizeof(am_pm), "PM" );
        if( newtime.tm_hour > 12 )        // Convert from 24-hour 
                newtime.tm_hour -= 12;    // to 12-hour clock. 
        if( newtime.tm_hour == 0 )        // Set hour to 12 if midnight.
                newtime.tm_hour = 12;

        // Convert to an ASCII representation. 
        err = asctime_s(timebuf, 26, &newtime);
        if (err)
        {
           printf("Invalid argument to asctime_s.");
           exit(1);
        }
        printf( "%.19s %s\n", timebuf, am_pm );
}

示例输出

Fri Apr 25 01:19:27 PM

.NET Framework 等效项

系统:: datetime:: ToLocalTime

请参见

参考

时间线

asctime_s, _wasctime_s

ctime, _ctime32, _ctime64, _wctime, _wctime32, _wctime64

_ftime, _ftime32, _ftime64

gmtime_s, _gmtime32_s, _gmtime64_s

localtime, _localtime32, _localtime64

time, _time32, _time64

_tzset