C++时间标准库时间time
生活随笔
收集整理的這篇文章主要介紹了
C++时间标准库时间time
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉自:http://www.cnblogs.com/yukaizhao/archive/2011/04/29/cpp_time_system_time.html (玉開)
C++標準庫中的時間需要引用time.h,可以取的本地時間或者格林威治時間,只能精確到秒
#include <iostream>
/*包含time頭文件*/
#include <time.h>
using namespace std;
int main()
{
//time_t是long類型,精確到秒,是當前時間和1970年1月1日零點時間的差
const time_t t = time(NULL);
cout<<"current time is "<<t<<endl;
/*本地時間:日期,時間 年月日,星期,時分秒*/
struct tm* current_time = localtime(&t);
printf("current year is %d;current month is %d;current date of month is %d
",
1900 + current_time->tm_year,
1 + current_time->tm_mon/*此month的范圍為0-11*/,
current_time->tm_mday);
printf("current day of year is %d;current day in week is %d
",
current_time->tm_yday,/*當前日期是今年的第多少天[0,365] */
current_time->tm_wday/*days since Sunday - [0,6] */);
printf("time part %d:%d:%d
",
current_time->tm_hour,
current_time->tm_min,
current_time->tm_sec);
printf(" 本地時間:%d-%d-%d %d:%d:%d
",
current_time->tm_year + 1900,
current_time->tm_mon + 1,
current_time->tm_mday,
current_time->tm_hour,
current_time->tm_min,
current_time->tm_sec);
/*格林威治時間*/
struct tm* current_gmtime = gmtime(&t);
printf("格林威治時間:%d-%d-%d %d:%d:%d
",
current_gmtime->tm_year + 1900,
current_gmtime->tm_mon + 1,
current_gmtime->tm_mday,
current_gmtime->tm_hour,
current_gmtime->tm_min,
current_gmtime->tm_sec);
system("pause");
return 0;
}
總結
以上是生活随笔為你收集整理的C++时间标准库时间time的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 14.cookie与自动登陆
- 下一篇: 10年生产300万辆 你贡献几辆?马斯克