c语言中的time相关函数:
#include <iostream>
#include <cstdio>
#include <ctime>
using namespace std;
void printlocaltime();
int main()
{
time_t rawtime;
struct tm *timeinfo;
int year, month, day;
char *weekday[] = { "Sunday", "Monday",
"Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
/* prompt user for date */
printf ("Enter year: ");
scanf ("%d", &year);
printf ("Enter month: ");
scanf ("%d", &month);
printf ("Enter day: ");
scanf ("%d", &day);
/* get current timeinfo and modify it to the user's choice */
time(&rawtime);
timeinfo = localtime(&rawtime);
timeinfo->tm_year = year - 1900;
timeinfo->tm_mon = month - 1;
timeinfo->tm_mday = day;
cout << timeinfo->tm_wday << endl;
/* call mktime: timeinfo->tm_wday will be set */
mktime(timeinfo);
cout << timeinfo->tm_wday << endl;
printf ("That day is a %s.\n", weekday[timeinfo->tm_wday]);
time_t secsnow;
time(&secsnow);
cout << ctime(&secsnow) << endl;
cout << asctime(localtime(&secsnow)) << endl;
cout << asctime(gmtime(&secsnow)) << endl;
clock_t t0 = clock();
cout << "t0 = " << t0 << endl;
printlocaltime();
long i = 10000000;
while(i--);
cout << "Hello world!" << endl;
printlocaltime();
clock_t t1 = clock();
cout << "t1 = " << t1 << endl;
clock_t t2 = t1 - t0;
cout <<"t1 - t0 = " << t2 << endl;
cout << " secs: " << ((double)t2) / CLOCKS_PER_SEC << endl;
return 0;
}
void printlocaltime() {
struct tm *timeptr;
time_t secsnow;
time(&secsnow);
timeptr = localtime(&secsnow);
cout << timeptr->tm_year << endl;
cout << timeptr->tm_wday << endl;
printf("The date is %d-%d-%4d\n",
(timeptr->tm_mon) +1,
(timeptr->tm_mday),
(timeptr->tm_year) + 1900);
printf("Local time is %02d:%02d:%02d\n",
timeptr->tm_hour,
timeptr->tm_min,
timeptr->tm_sec);
}