C++ 中的localtime() 函数将自纪元以来的给定时间转换为表示为本地时间的日历时间。
localtime() 函数在<ctime> 头文件中定义。
localtime()原型
tm* localtime(const time_t* time_pretr);
localtime() 函数将 time_t 类型的指针作为其参数,并返回结构 tm 的指针对象。 localtime() 函数返回的值为当地时间。
然后,可以分别使用tm_hour ,  tm_min 和tm_sec 访问小时、分钟和秒。
参数:
time_ptr:指向要转换的 time_t 对象的指针。
返回:
- 成功时,localtime() 函数返回一个指向 
tm对象的指针。 - 失败时,返回一个空指针。
 
示例:localtime() 函数如何工作?
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
	time_t curr_time;
	curr_time = time(NULL);
	tm *tm_local = localtime(&curr_time);
	cout << "Current local time : " << tm_local->tm_hour << ":" << tm_local->tm_min << ":" << tm_local->tm_sec;
	return 0;
}
运行程序时,输出将是:
Current local time : 19:20:14
相关用法
- C++ localtime()用法及代码示例
 - C++ localeconv()用法及代码示例
 - C++ log2()用法及代码示例
 - C++ log10()用法及代码示例
 - C++ complex log()用法及代码示例
 - C++ logb()用法及代码示例
 - C++ log()用法及代码示例
 - C++ log1p()用法及代码示例
 - C++ complex log10()用法及代码示例
 - C++ longjmp() and setjmp()用法及代码示例
 - C++ list assign()用法及代码示例
 - C++ llround()用法及代码示例
 - C++ list front()用法及代码示例
 - C++ list::remove()、list::remove_if()用法及代码示例
 - C++ lrint() and llrint()用法及代码示例
 - C++ lldiv()用法及代码示例
 - C++ list back()用法及代码示例
 - C++ list merge()用法及代码示例
 - C++ ldiv()用法及代码示例
 - C++ list remove()用法及代码示例
 
注:本文由纯净天空筛选整理自 C++ localtime()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
