#include <iostream>
#include <ctime>
using namespace std;
const char * MONTHS[12] = {
"Januar","Februar","Marts","April","Maj","Juni","Juli","August","September","Oktober","November","December"
};
int main (int argc, char ** argv) {
struct tm * current_time;
time_t t = time(NULL);
current_time = localtime(&t);
cout << "Year: " << (1900 + current_time->tm_year) << endl;
cout << "Month: " << MONTHS[current_time->tm_mon] << endl;
cout << "Day of month: " << current_time->tm_mday << endl;
cout << "Hour: " << current_time->tm_hour << endl;
cout << "Minute: " << current_time->tm_min << endl;
cout << "Second: " << current_time->tm_sec << endl;
return 0;
}
I det ovenstående finder jeg den nuværende tid med time() funktionen. Jeg giver så en pointer til denne tid til localtime() funktionen som returnerer en pointer til en 'tm' struct, som indeholder flere informationer.
Dem skriver jeg så ud:
$ ./test
Year: 2008
Month: August
Day of month: 19
Hour: 16
Minute: 34
Second: 27