localtime() 函数在 time.h 头文件中定义。此函数有助于返回本地时间。通过使用参数“timer”指向的时间来填充结构“tm”,其中包含表示本地时间的值。
struct tm *localtime(const time_t *timer); #where timer should be a pointer
localtime() 函数接受单个参数“timer”,它是一个指向包含日历时间结构的指针。
| 参数 | 描述 | 必需/可选 |
|---|---|---|
| timer | 指向表示日历时间的 time_t 值的指针 | 必需 |
该函数返回指向结构“tm”的指针,该结构包含时间信息。“tm”结构信息包含以下内容:
struct tm {
int tm_sec; /* seconds, range 0 to 59 */
int tm_min; /* minutes, range 0 to 59 */
int tm_hour; /* hours, range 0 to 23 */
int tm_mday; /* day of the month, range 1 to 31 */
int tm_mon; /* month, range 0 to 11 */
int tm_year; /* The number of years since 1900 */
int tm_wday; /* day of the week, range 0 to 6 */
int tm_yday; /* day in the year, range 0 to 365 */
int tm_isdst; /* daylight saving time */
};
| 输入 | 返回值 |
|---|---|
| 如果参数 | 指向 struct tm 对象的指针 |
| 如果失败 | NULL 指针 |
#include <stdio.h>
#include <time.h>
int main()
{
time_t rtime;
struct tm *ltime;
time( &rtime );
ltime = localtime( &rtime );
printf("Present local time & date is: %s", asctime(ltime));
return(0);
}
输出
Present local time & date is: Thu Aug 23 08:10:04 2012
#include <stdio.h>
#include <time.h>
int main (){
struct tm* localtm;
time_t ts = time(NULL);
// Get the localtime
localtm = localtime(&ts);
printf("The local time and date is: %s\n",
asctime(localtm));
return 0;
}
输出
The local time and date is: Mon Sep 23 05:15:51 2019