时间
struct tm{
int tm_sec; // 秒, 0-59(61)
int tm_min; // 分, 0-59
int tm_hour; // 时, 0-23
int tm_mday; // 月第几天, 1-31
int tm_mon; // 月, 0-11
int tm_year; // 年, 自1900年始
int tm_mon; // 月, 0-11
int tm_wday; // 周第几天, 0-6, 周日算起
int tm_yday; // 年第几天, 0-365, 1月1日算起
int tm_isdst; // 夏令时
}
函数
time()
: 返回系统当前日历时间, 自1970年1月1日以来经过的秒数, 系统无时间返回-1
time_t time(time_t *time);
ctime()
: 返回一个表示当地时间的字符串指针
char *ctime(const time_t *time);
localtime()
: 返回一个指向本地时间的tm结构的指针
struct tm *localtime(const time_t *time);
asctime()
: 返回指向字符串的指针, 字符串含time所指向结构存储的信息
char *asctime(const struct tm * time);
clock()
: 返回程序执行起, 处理器时钟所用时间
gmtime()
: 返回指向time的指针, tm结构, 协调世界时UTC
struct tm *gmtime(const time_t *time);
mktime()
: 返回日历时间
time_t mktime(struct tm *time);
difftime()
: 返回time1与time2相差秒数
double difftime(time_T time2, time_t time1);
strftime()
: 格式化日期和时间为指定格式
size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr);
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
time_t now = time(0); // 基于当前系统的当前日期/时间
cout << now << endl;
char* dt = ctime(&now); // 转为字符串形式
cout << dt << endl;
struct tm * dt1 = localtime(&now);
cout << asctime(dt1) << endl;
struct tm * dt2 = gmtime(&now);
cout << asctime(dt2) << endl;
struct tm time3;
cout << mktime(&time3) << endl;
cout << difftime(time(NULL), mktime(&time3)) << endl;\
char buffer[50];
strftime(buffer, 50, "%Y-%m-%d %H:%M:%S",gmtime(&now));
cout << clock() << endl;
cout << buffer << endl;
return 0;
}