VC + + Gain system time, program run time (accurate to seconds, milliseconds) Five ways

Source: Internet
Author: User
Tags month name

1. Use the CTime class (get system current time, accurate to seconds)
CString str; // Get system Time CTime tm;tm=ctime::getcurrenttime (); // Gets the system date Str=tm. Format (" now time is%y year%m month%d day%x"); MessageBox (STR,NULL,MB_OK);
A, extracted from the Ctimet days and seconds
CTime T =ctime::getcurrenttime ();intD=t.getday ();//Get What   intY=t.getyear ();//Get year   intM=t.getmonth ();//Get Current Month   intH=t.gethour ();//gets the current when   intMm=t.getminute ();//Get minutes   intS=t.getsecond ();//Get seconds   intW=t.getdayofweek ();//get day of the week, note 1 for Sunday, 7 for Saturday
b, to calculate the difference between two periods of time, you can use the CTimeSpan class, using the following methods:
CTime T1 (1999,3, +, A, the,0 ); CTime T=Ctime::getcurrenttime (); CTimeSpan span=T-T1;//calculates the interval between the current system time and time T1intIday=span. GetDays ();//get how many days are there in this time intervalintIhour=span. Gettotalhours ();//get a total of how many hoursintImin=span. Gettotalminutes ();//get a total of how many minutesintIsec=span. Gettotalseconds ();//get how many seconds in total
C, gets the current date and time, and can be converted to CString
CTime tm=ctime::getcurrenttime (); CString str=TM. Format ("%y-%m-%d"); // Show Month 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 system time information inside the SYSTEMTIME structure.
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; // milliseconds } Systemtime,*psystemtime;
Cases:
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 from the operating system boot to the current elapsed (elapsed), and its return value is DWORD. (Accurate to milliseconds)
//Get program run timeLongT1=gettickcount ();//System Run time (MS) before program section startsSleep ( -);LongT2=gettickcount ();();//System Run time (MS) after the end of the program segmentStr. Format ("Time:%dms", T2-T1);//the difference between the front and back is the program run timeAfxMessageBox (str);//Get system Run timeLongt=GetTickCount (); CString str,str1;str1. Format ("when the system is running%d", t/3600000); Str=str1;t%=3600000; str1. Format ("%d minutes", t/60000); Str+=str1;t%=60000; str1. Format ("%d seconds", t/ +); Str+=str1; AfxMessageBox (str);
4. Use time_t time (time_t * timer): Use only the C standard library (accurate to the second) to get the number of seconds from the standard chronograph (typically midnight January 1, 1970) to the current moment: Double Difftime (time_t timer1, time_t timer0) struct TM *localtime (const time_t *timer); Get local time, localtime the resulting string returned by the structure TM can be formatted according to the following format:%a abbreviation for the day of the week. Eg:tue%A The full name of the week. Eg:tuesday the abbreviation for the%b month name. The full name of the%B month name. The%c local end datetime is a better representation of the string. %d numbers indicate the day ordinal of the month (range 00 to 31). Date%H A 24-hour number representing the number of hours (range 00 to 23). %I A 12-hour number indicates the number of hours (range 01 to 12). %j indicates the day of the Year in Numbers (range 001 to 366). Number of%m months (range from 1 to 12). %M minutes. %p indicates local time with ' AM ' or ' PM '. %s Number of seconds. The number of%u is expressed as the week of the current year, and the first one weeks start in Sunday. The%W number is expressed as the week of the current year, and the first one weeks start in Monday. %w numbers indicate the day of the week (0 is Sunday). %x does not include date notation for time. %x does not include a time representation of the date. The eg:15:26:30%y Two-digit number represents the year (range from 00 to 99). %Y a full year number representation, that is, four digits. eg:2008%Z (%Z) time zone or name abbreviation. Eg: Chinese standard Time percent% character. Contains: Years, months, days, weeks, hours, minutes, seconds, milliseconds. []http://blog.const.net.cn/a/16370.htm on the net of drinking small wine

VC + + Gain system time, program run time (accurate to seconds, milliseconds) Five ways

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.