Here are few different methods to retrieve time in C++ depends on your requirements
I always need to retrieve time information one way or anther to use in my code. so I decided to create this Knol to keep/list all methods and related information for everyone so we can retrieve this easier in the future!
/*
The Goal of this project is to be a reference to all Date and time stuff
Author: Hesham Elsaghir
Date: 10/13/2010
*/The Goal of this project is to be a reference to all Date and time stuff
Author: Hesham Elsaghir
Date: 10/13/2010
#include"stdafx.h"#include "iostream"#include "time.h"#include "atltime.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[]){
SYSTEMTIME st;
FILETIME ft;
unsigned short File_Time_Date;
unsigned short File_Time_Time; // time and date in numbers format...
GetLocalTime(&st);
SystemTimeToFileTime(&st, &ft);
cout << "Today's Day # is: " << st.wDay << endl;
cout << "Today's Month # is: " << st.wMonth << endl;
cout << "Today's Year is: " << st.wYear << endl;
// This function will convert the time and date to Dos Date and Time format
FileTimeToDosDateTime(&ft, &(File_Time_Date), &(File_Time_Time));
cout << "Time now in File time is: " << File_Time_Time << endl;
cout << "Time now in File Date is: " << File_Time_Date << endl;
FileTimeToDosDateTime(&ft, &(File_Time_Date), &(File_Time_Time));
cout << "Time now in File time is: " << File_Time_Time << endl;
cout << "Time now in File Date is: " << File_Time_Date << endl;
// This is a different method to obtain Date and Time
struct tm now;
time_t time_now;
time(&time_now);
now = *localtime(&time_now); /* Get time and date structure */mktime(&now);
time_t time_now;
time(&time_now);
now = *localtime(&time_now); /* Get time and date structure */mktime(&now);
cout << "Time now is: " << now.tm_mon << now.tm_mday << now.tm_year << endl;cout << "Ctime format to time: " << ctime(&time_now) << endl;
long time_taken = clock();
printf("%ld\n", time_taken);
printf("%ld\n", time_taken);
printf("%d\n", CLOCKS_PER_SEC);
// How to use sscanf() to int day, month, year;
char test[] = "10/3/2010";
sscanf(test, "%d/%d/%d", &day, &month, &year);
printf("day: %02d\n",day);
printf("moth: %02d\n",month);
printf("year: %04d\n",year);
printf("day: %02d\n",day);
printf("moth: %02d\n",month);
printf("year: %04d\n",year);
// Here is a way to print local and Gtime "UTC"struct tm *local;
time_t t;
t = time(NULL);
local = localtime(&t);
printf("Local time and date: %s\n", asctime(local));
local = gmtime(&t);
printf("UTC time and date: %s\n", asctime(local));
time_t t;
t = time(NULL);
local = localtime(&t);
printf("Local time and date: %s\n", asctime(local));
local = gmtime(&t);
printf("UTC time and date: %s\n", asctime(local));
// Using sprintf to write a formated string into an array
GetLocalTime(&st);
char temp_mdy[50];
sprintf(temp_mdy, " @date %02d/%02d/%04d\n",st.wMonth,st.wDay, st.wYear);
cout << temp_mdy << endl;
// Different methods to capture time elapsed
long st_time = clock();
// do some work here
Sleep(500); // sleep 500 milliseconds
long sp_time = clock();
printf("Time Elapsed is: %ld Millisecond\n", sp_time - st_time);
long st_time = clock();
// do some work here
Sleep(500); // sleep 500 milliseconds
long sp_time = clock();
printf("Time Elapsed is: %ld Millisecond\n", sp_time - st_time);
// Or you can use this
time_t time_1, time_2;
time(&time_1);
Sleep(10000); // sleep 10 seconds
time(&time_2);
printf("Time Elapsed is: %.2lf seconds\n", difftime(time_2,time_1));
time_t time_1, time_2;
time(&time_1);
Sleep(10000); // sleep 10 seconds
time(&time_2);
printf("Time Elapsed is: %.2lf seconds\n", difftime(time_2,time_1));
return 0;
}
Here is the output of the program:
I will be adding more futures to cover C# later.
References:
http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/
No comments:
Post a Comment