转换时间值并更正本地时区。 提供这些函数的更安全版本;请参阅 localtime_s、_localtime32_s、_localtime64_s。
语法
struct tm *localtime( const time_t *sourceTime ); // See note in remarks section about linkage
struct tm *_localtime32( const __time32_t *sourceTime );
struct tm *_localtime64( const __time64_t *sourceTime );
参数
sourceTime
指向存储时间的指针。
返回值
如果传递给函数的日期如下所示,则返回一个指向结构结果的指针或 NULL:
1970 年 1 月 1 日午夜前。
2038 年 1 月 19 日 03:14:07 之后(UTC)(使用
_time32和time32_t)。3000 年 12 月 31 日 23:59:59 之后(UTC)(使用
_time64和__time64_t)。
使用 _localtime64 结构的 __time64_t 允许日期最大值表示为 3000 年 12 月 31 日 23:59:59,协调世界时 (UTC);而 _localtime32 能表示截至 2038 年 1 月 18 日 23:59:59 的日期,UTC 。
localtime 是计算出 _localtime64 的内联函数,且 time_t 等同于 __time64_t。 如果需要强制编译器将 time_t 解释为旧的 32 位 time_t,你可以定义 _USE_32BIT_TIME_T。
_USE_32BIT_TIME_T 导致 localtime 计算结果为 _localtime32。 不建议 _USE_32BIT_TIME_T,因为应用程序可能会在 2038 年 1 月 18 日后失效;且在 64 位平台上不允许使用它。
结构类型 tm 的字段存储以下值,其中每个值为 int:
| 字段 | 说明 |
|---|---|
tm_sec |
整分后的秒数 (0 - 59)。 |
tm_min |
整点后的分钟数 (0 - 59)。 |
tm_hour |
午夜以后的小时数 (0 - 23)。 |
tm_mday |
每月的某一日 (1 - 31)。 |
tm_mon |
月(0 - 11;1 月 = 0)。 |
tm_year |
年(当前年份减去 1900)。 |
tm_wday |
每周的某一日(0 - 6;星期天 = 0)。 |
tm_yday |
一年的某一天(0 - 365;1 月 1 日 = 0)。 |
tm_isdst |
如果夏令时生效,则为正值;如果夏令时不生效,则为 0;如果夏令时状态未知,则为负值。 |
如果设置了 TZ 环境变量,C 运行时库会假设规则适用于美国,以使用该规则实现夏令时 (DST) 的计算。
注解
localtime 函数将转换存储为 time_t 值的时间并将结果存储在 tm 类型的结构中。
long 值 sourceTime 表示自 1970 年 1 月 1 日午夜 (00: 00:00) (UTC) 起经过的秒数. 此值通常从 time 函数中获取。
gmtime、mktime、mkgmtime 和 localtime 的 32 位和 64 位版本均为每个线程使用单个 tm 结构以进行转换。 每次调用这些例程都会破坏上一次调用的结果。
如果用户首次设置全局环境变量 localtime,TZ 将更正本地时区。 设置 TZ 时,其他三个环境变量(_timezone、_daylight 和 _tzname)也会自动设置。 如果未设置 TZ 变量,localtime 将尝试使用控制面板的日期/时间应用程序中指定的时区信息。 如果无法获取此信息,则它默认使用代表太平洋时区的 PST8PDT。 有关这些变量的说明,请参阅 _tzset。
TZ 是 Microsoft 扩展名,但并不是 localtime 的 ANSI 标准定义的一部分。
注意
目标环境应尝试确定夏令时是否生效。
这些函数验证其参数。 如果 sourceTime 为空指针,或 sourceTime 值为负值,这些函数将调用无效参数句柄,如参数验证中所述。 如果允许执行继续,则这些函数返回 NULL,并且将 errno 设置为 EINVAL。
默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态。
注意
使用 Windows SDK 版本 10.0.26100.6901 和 Visual Studio 2026 或更高版本时, localtime 不再 static inline (内部链接)。
inline 而是(外部链接)。
若要返回到以前的行为, #define _STATIC_INLINE_UCRT_FUNCTIONS=1 请先包括任何 CRT 标头。 默认情况下,_STATIC_INLINE_UCRT_FUNCTIONS 设置为 0。
此更改增加了与 C++ 标准的 UCRT 一致性,并提高了与C++模块的兼容性。
要求
| 例程 | 必需的 C 标头 | 必需的 C++ 标头 |
|---|---|---|
| .- . | <time.h> |
<ctime> 或 <time.h> |
有关兼容性的详细信息,请参阅 兼容性。
示例
// crt_localtime.cpp
// compile with: /W3
// This program uses _time64 to get the current time
// and then uses localtime64() 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;
_time64( &long_time ); // Get time as 64-bit integer.
// Convert to local time.
newtime = _localtime64( &long_time ); // C4996
// Note: _localtime64 deprecated; consider _localetime64_s
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;
char buff[30];
asctime_s( buff, sizeof(buff), newtime );
printf( "%.19s %s\n", buff, am_pm );
}
Tue Feb 12 10:05:58 AM
另请参阅
工时管理
%>
ctime, _ctime32, _ctime64, _wctime, _wctime32, _wctime64
.- .
.- .
.- .
.- .
_tzset