VC++ method to obtain system time summary

  • 2020-04-02 03:11:53
  • OfStack

1. Use CTime class (get the current time of the system, accurate to seconds)


CString str;
//Acquire system time
CTime tm;
tm=CTime::GetCurrentTime();//Get system date
str=tm.Format(" Now the time is %Y years %m month %d day  %X");
MessageBox(str,NULL,MB_OK);

A, extract the minutes and seconds of day, month and year from CTimet


CTime t = CTime::GetCurrentTime();
 int d=t.GetDay(); //Get the date
 int y=t.GetYear(); //For years
 int m=t.GetMonth(); //Get the current month
 int h=t.GetHour(); //When is the current
 int mm=t.GetMinute(); //For minutes
 int s=t.GetSecond(); //For seconds
 int w=t.GetDayOfWeek(); //For the day of the week, note that 1 is Sunday and 7 is Saturday

B, calculate the difference between two periods of time, you can use the CTimeSpan class, the specific use method is as follows:


CTime t1( 1999, 3, 19, 22, 15, 0 );
CTime t = CTime::GetCurrentTime();
CTimeSpan span=t-t1; //Calculates the interval between the current system time and time t1
int iDay=span.GetDays(); //Get how many days are in between
int iHour=span.GetTotalHours(); //Get the total number of hours
int iMin=span.GetTotalMinutes();//Gets the total number of minutes
int iSec=span.GetTotalSeconds();//Gets the total number of seconds

C, get the current date and time, and can be converted to a CString


CTime tm=CTime::GetCurrentTime(); 
CString str=tm.Format("%Y-%m-%d");//Show the year, month and day

2. Use the GetLocalTime: Windows API function to get the local current system date and time (accurate to milliseconds)
This function stores the acquired SYSTEMTIME information in the SYSTEMTIME structure


typedef struct _SYSTEMTIME
{
WORD wYear;//years
WORD wMonth;//month
WORD wDayOfWeek;//Week: 0 for Sunday, 1 for Monday, 2 for Tuesday...
WORD wDay;//day
WORD wHour;//when
WORD wMinute;//points
WORD wSecond;//seconds
WORD wMilliseconds;// milli seconds
}SYSTEMTIME,*PSYSTEMTIME;

Ex. :


SYSTEMTIME st;
CString strDate,strTime;
GetLocalTime(&st);
strDate.Format("%4d-%2d-%2d",st.wYear,st.wMonth,st.wDay);
strTime.Format("%2d:%2d:%2d",st.wHour,st.wMinute,st.wSecond) ;
AfxMessageBox(strDate);
AfxMessageBox(strTime);

3. Use GetTickCount: the number of milliseconds elapsed since the operating system was started and the return value is DWORD. (accurate to milliseconds)


//Gets the running time of the program
long t1=GetTickCount();//Get system running time (ms) before program segment starts
Sleep(500);
long t2=GetTickCount();();//Get system running time (ms) after program segment ends
str.Format("time:%dms",t2-t1);//The difference between before and after is the program running time
AfxMessageBox(str);
//Gets the system running time
long t=GetTickCount();
CString str,str1;
str1.Format(" System up and running  %d when ",t/3600000);
str=str1;
t%=3600000;
str1.Format("%d points ",t/60000);
str+=str1;
t%=60000;
str1.Format("%d seconds ",t/1000);
str+=str1;
AfxMessageBox(str);

4. Use time_t time(time_t * timer) :     Use only the C standard library (accurate to seconds)
Gets the number of seconds from the standard time point (usually midnight on January 1, 1970) to the current time
Calculate time difference: double difftime(time_t timer1, time_t timer0)
Struct tm *localtime(const time_t *timer);   Gets the localtime, and the result obtained at localtime is returned by the structure tm
The returned string can be formatted as follows:

An abbreviation for the day of the week. Eg: Tue
The full name of the day of the week. Eg: Tuesday
Initials of month b.
Full name of month name.
%c local end date time is a better representation of a string.
%d is the number of days of the month (ranging from 00 to 31). The date of
%H is expressed as a 24-hour number (ranging from 00 to 23).
%I is expressed in 12-hour Numbers (range 01 to 12).
%j is the number of days of the year (range 001 to 366).
The number for the month of %m (range from 1 to 12).
% M minutes.
%p represents the local end time as 'AM' or 'PM'.
% S number of seconds.
The U number represents the week of the year, with the first week beginning on the first Sunday.
The %W number represents the week of the year, with the first week beginning on the first Monday.
%w is the number of days of the week (0 is Sunday).
%x date representation without time.
%X time representation without date. Eg: 15:26:30
The two digits %y represent the year (ranging from 00 to 99).
The full year numerical representation of %Y is four digits. Eg: 2008
%Z(% Z) time zone or abbreviation. Chinese standard time
The %% % character.

The above is all the content of this article, I hope you can enjoy it.


Related articles: