VC get time function Summary

Source: Internet
Author: User

How to get the time? How accurate?

A:

1
Use time_t time (time_t * timer) to be accurate to seconds

Calculate the time difference using double
Difftime (time_t timer1, time_t timer0)

2
When clock_t clock () is used, the CPU time is accurate to 1/clocks_per_sec.

3
By using DWORD gettickcount (), the system running time is accurate to milliseconds.

4
If you use the ctime class of MFC, you can use ctime: getcurrenttime () to precise to seconds.

5
You can use


Bool queryperformancefrequency (large_integer * lpfrequency)


Bool queryperformancecounter (large_integer * lpperformancecount) gets the counter value


Then, divide the difference between two counters by frequency to get the time.

6
There are also methods mentioned in David's article:


Multimedia Timer Functions


The following functions are used with multimedia timers.


Timebeginperiod/timeendperiod/timegetdevcaps/timegetsystemtime


Timegettime/timekillevent/timeproc/timesetevent has high precision

Q: Is the gettickcount () function true or accurate to 55 milliseconds?

A:

Gettickcount () and getcurrenttime () are both accurate to 55 ms (one tick is 55 ms ). To be accurate to milliseconds, use the timegettime function or the queryperformancecounter function. For more information, see qa001022.
"Using High-precision timer in VC ++" and qa001813
"How to Implement accurate timing in Windows" and qa004842
"Timegettime function delay is not allowed ".

Q: How does VC ++ obtain the system time and what type of variables are returned?

Getsystemtime returns the standard greenweizhi time.

Getlocaltime, As used above, returns the time of your region, and returns Beijing time in China.

Void
Getsystemtime (

Lpsystemtime
Lpsystemtime // address of System Time Structure

);

You can obtain the function, in which lpsystemtime
Is a struct

Including: year, month, day, week, hour, minute, second, millisecond.

The following is the time msdn document:

Compatibility in
The introduction.

Libraries

 

Libc. Lib Single thread static library, retail version
Libcmt. Lib Multithread static library, retail version
Msvcrt. Lib Import library for msvcrt. dll, retail
Version

 

Return
Value

Time
Returns the time in elapsed seconds. There is no error return.

Parameter

Timer

Storage
Location for time

Remarks

The
Time function returns the number of seconds elapsed since midnight (00:00:00 ),
January 1, 1970, Coordinated Universal Time, according to the system clock.
Return Value is stored in the location given by timer. This parameter may be
Null, in which case the return value is not stored.

Example

/*
Times. C using strates various time and date functions including:

*
Time _ ftime ctime asctime

*
Localtime gmtime mktime _ tzset

*
_ Strtime _ strdate strftime

*

*
Also the global variable:

*
_ Tzname

*/

# Include
<Time. h>

# Include
<Stdio. h>

# Include
<Sys/types. h>

# Include
<Sys/timeb. h>

# Include
<String. h>

Void
Main ()

{


Char tmpbuf [128], ampm [] = "am ";


Time_t ltime;


Struct _ timeb tstruct;


Struct TM * today, * GMT, Xmas = {0, 0, 12, 25, 11, 93 };


/* Set Time Zone from TZ environment variable. If tz Is not set,


* The operating system is queried to obtain the default value


* For the variable.


*/


_ Tzset ();


/* Display Operating System-style Date and time .*/


_ Strtime (tmpbuf );


Printf ("OS time:/T % s/n", tmpbuf );


_ Strdate (tmpbuf );


Printf ("OS Date:/T % s/n", tmpbuf );


/* Get Unix-style time and display as number and string .*/


Time (& ltime );


Printf ("time in seconds since UTC 1/1/70:/T % LD/N", ltime );


Printf ("UNIX time and date:/T % s", ctime (& ltime ));


/* Display UTC .*/


GMT = gmtime (& ltime );


Printf ("Coordinated Universal Time:/T % s", asctime (GMT ));


/* Convert to Time Structure and adjust for PM if necessary .*/


Today = localtime (& ltime );


If (today-> tm_hour> 12)


{


Strcpy (ampm, "PM ");


Today-> tm_hour-= 12;


}


If (today-> tm_hour = 0)/* adjust if midnight hour .*/


Today-> tm_hour = 12;


/* Note how pointer addition is used to skip the first 11


* Characters and printf is used to trim off terminating


* Characters.


*/


Printf ("12-hour time:/T %. 8 S % s/n ",


Asctime (today) + 11, ampm );


/* Print additional time information .*/


_ Ftime (& tstruct );


Printf ("plus milliseconds:/T % u/N", tstruct. millitm );


Printf ("zone difference in seconds from UTC:/T % u/N ",


Tstruct. timezone );


Printf ("Time Zone name:/T % s/n", _ tzname [0]);


Printf ("Daylight Savings:/T % s/n ",


Tstruct. dstflag? "Yes": "no ");


/* Make time for noon Christmas, 1993 .*/


If (mktime (& Xmas )! = (Time_t)-1)


Printf ("Christmas/T % s/n", asctime (& Xmas ));


/* Use time structure to build a customized time string .*/


Today = localtime (& ltime );


/* Use strftime to build a customized time string .*/


Struts time (tmpbuf, 128,


"Today is % A, day % d of % B in the year % Y./N", today );


Printf (tmpbuf );

}

 

Output

OS
Time: 21:51:03

OS
Date: 05/03/94

Time
In seconds since UTC 1/1/70: 768027063

UNIX
Time and date: Tue May 03 21:51:03 1994

Coordinated
Universal Time: Wed May 04 04:51:03 1994

12-hour
Time: 09:51:03

Plus
Milliseconds: 279

Zone
Difference in seconds from UTC: 480

Time
Zone Name:

Daylight
Savings: Yes

Christmas
Sat Dec 25 12:00:00 1993

Today
Is Tuesday, day 03 of May in the year 1994.

 

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.