这个头文件还是蛮奇妙的,感觉获取系统当前时间这个以后会用到,学习了一下下:
参考:
自己写的cpp:
#include <time.h>
#include <iostream>
#include <stdlib.h> // system("pause")
#include <windows.h> // Sleep()
using namespace std;
const char wd[7][10] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int main() {
struct tm *ptm; // declare a pointer pointing to the tm(a struct to store some parameters expressing time)
time_t it; // alias of a fundamental arithmetic type capable of representing times, as those returned by function time
time_t it_2;
/*
time_t time (time_t* timer);
Get the current calendar time as a value of type time_t.
The function returns this value,
and if the argument is not a null pointer,
it also sets this value to the object pointed by timer.
*/
cout << it << endl;
cout << it_2 << endl;
it = time(NULL);
it = time(&it_2);
long int t = it;
cout << t << endl; // time_t is a long int
cout << it << endl; // in fact, the return value of the function time is a long int, representing the seconds from CUT(Coordinated Universal Time(1970 1 1 00:00:00))
cout << it_2 << endl; // and the return value can be received by a return value or a parameter
cout << endl;
/*
struct tm * localtime (const time_t * timer);
Uses the value pointed by timer to fill a tm structure
with the values that represent the corresponding time,
expressed for the local timezone.
*/
ptm = localtime(&it);
/*
char* asctime (const struct tm * timeptr);
Convert tm structure to string,
interprets the contents of the tm structure pointed by timeptr as a calendar time
and converts it to a C-string containing a human-readable version
of the corresponding date and time.
*/
string time_file = asctime(ptm);
cout << time_file << endl;
Sleep(2000);
//system("pause");
ptm = localtime(&it); // notice that the localtime function is just a convertor, the 'it' is still the time above, so the result is the same as above
time_file = asctime(ptm);
cout << time_file << endl;
it = time(&it); // notice that time function is a calculator, now the 'it' is changed
ptm = localtime(&it);
time_file = asctime(ptm);
cout << "the localtime: " << time_file << endl;
ptm = gmtime(&it);
time_file = asctime(ptm);
cout << "the GMT(Greenwich mean time): " << time_file << endl; // yes that i am in China
/*
time_t mktime (struct tm * timeptr);
mktime is a reverse of localtime, and mktime can set the unknown part of the parameter(a pointer to struct tm) according the known part
*/
cout << "tell me the year, month, day, and i can tell you the weekday:" << endl;
time_t nt;
struct tm* ptw;
time(&nt); // this 2 lines are used to get current time infomation
ptw = localtime(&nt);
cin >> ptw->tm_year >> ptw->tm_mon >> ptw->tm_mday; // notice that if i try "use week_day_cal." to replace "ptw->", the result is wrong(a fantastic number)
ptw->tm_year -= 1900; // it seems that without -1900, the tm_year is so large that the mktime function didnot working
ptw->tm_mon -= 1;
mktime(ptw); // or it = mktime(ptm)
cout << ptw->tm_year + 1900 << "(year) " << ptw->tm_mon + 1 << "(month) " << ptw->tm_mday << "(day) " << "is " << wd[ptw->tm_wday] << endl;
cout << endl;
/*
double difftime (time_t end, time_t beginning);
Calculates the difference in seconds between beginning and end.
*/
/*
cout << "tell me the begining time and the end time and i will tell you the difference between them: " << endl;
time_t bt;
struct tm* begin = new tm;
time(&bt);
begin = localtime(&bt);
cin >> begin->tm_year >> begin->tm_mon >> begin->tm_mday;
begin->tm_year -= 1900;
begin->tm_mon -= 1;
mktime(begin);
cout << asctime(begin) << endl;
time_t et;
struct tm* end = new tm;
time(&et);
end = localtime(&et);
cin >> end->tm_year >> end->tm_mon >> end->tm_mday;
end->tm_year -= 1900;
end->tm_mon -= 1;
mktime(end);
cout << asctime(end) << endl;
cout << "the difference(end - begin) between the 2 date are: \nyear: " << end->tm_year - begin->tm_year << "\nmonth: " << end->tm_mon - begin->tm_mon << "\nmonthday: " << end->tm_mday - begin->tm_mday << endl;
cout << difftime(mktime(begin), mktime(end)) << endl;
*/
return 0;
}