Time functions in C + +

Source: Internet
Author: User

C + + Get Time function A lot, when should use what function, get what time? How to use it? Many people will be confused.

This article is I experienced several game client and server development, the game in time to get a little summary.

When learning the game client first, use two functions to get the most accurate time

BOOL QueryPerformanceFrequency (Large_integer *lpfrequency);

BOOL QueryPerformanceCounter (Large_integer *lpcount);

These two functions are to obtain the CPU clock frequency and CPU counter, is able to obtain the most accurate time difference. Using these two functions is the most accurate for the exact time it takes to get through each frame.

#include <windows.h>

Large_integer lfrequency;
QueryPerformanceFrequency (&lfrequency);

Large_integer Lbegincount;
QueryPerformanceCounter (&lbegincount);

Sleep (100);

Large_integer Lendcount;
QueryPerformanceCounter (&lendcount);

Double time = (double) (Lendcount.quadpart-lbegincount.quadpart)/(double) Lfrequency.quadpart;

In the time-processing module of the client-side code, each frame calls QueryPerformanceCounter to get the current counter to get the time each frame uses. (Of course there is unity now, it is estimated that no one is concerned about these two functions.)

Although these two functions can be used to accurately statistics the elapsed time, but can not get the current time, and the above two functions are unique to the Windows system, Unix/linux system does not have.

In order to obtain the current precise time of the system, another system function needs to be used

int gettimeofday (struct timeval *tv, struct timezone *tz);

Gets the time and timezone (UTC time) elapsed from January 1, 1970 to now (according to the official Linux documentation, the time zone is no longer in use and should normally be passed null).

#include <sys/time.h>

struct Timeval start_tv;

Gettimeofday (&START_TV, NULL);

Sleep (1000);

struct Timeval end_tv;

Gettimeofday (&END_TV, NULL);

Double time = (end_tv.tv_sec-start_tv.tv_sec) + (double) (END_TV.TV_USEC-START_TV.TV_USEC)/(double) 1000000;

This also gives you the time to go through every frame that is accurate to microseconds. The frame running mechanism on the server is usually the time function to calculate and synchronize.

If you do not need a very precise time, and as long as the exact seconds, you can use another time function

time_t time (time_t* timer);

The function returns a UTC timestamp and sets the timestamp value for the timer if the timer parameter is passed in.

However, the above two functions get the UTC timestamp, what if the time of the current time zone needs to be displayed in the game?

With LocalTime or localtime_r, the effect is the same, just get the result parameter position is different.

struct TM *localtime (const time_t *TIMEP); Incoming UTC timestamp, returns the TM structure pointer of the current time zone

struct TM *localtime_r (const time_t *TIMEP, struct TM *result);

The first function obtains a pointer to a static variable in the TM structure, and the second function passes in the address of a TM structure variable and assigns it a value. The resulting TM variable, which stores the time of the current time zone.

The structure of the TM is defined as follows and can be used directly for display.

struct TM {    int tm_sec;         /* seconds */    int tm_min;         /* minutes */    int tm_hour;        /* hours */    int tm_mday;        /* Day of the month */    int tm_mon;         /* Month */    int tm_year;        /* Year */    int tm_wday;        /* Day of the week */    int tm_yday;        /* Day in the year */    int tm_isdst;       /* Daylight Saving Time */};

In general, when the server and client communication synchronization time, not so many int, only a int64 UTC timestamp, to the client itself to the current time zone. In addition, the server does not tune localtime once every time. In general, both the server and the client are the UTC timestamps that maintain the current time of a int64, and the offset time of a current time zone (the client also periodically updates and the server's time error, when it is right).

Therefore, two other functions need to be used:

struct TM *gmtime (const time_t *TIMEP); time_t to TM conversion, UTC time before and after, no time zone conversion

struct TM *gmtime_r (const time_t *TIMEP, struct TM *result); Gmtime Incoming result mode

time_t mktime (struct TM *timeptr); LocalTime reverse operation, from the current time zone's TM to the time_t of UTC

When the process starts

time_t t0 = 0; The current time is 0 o'clock

time_t T1 = mktime (Gmtime (&t0)); Time in UTC

int timezone_diff = (int) (T0-T1); Time difference between the current timezone and UTC

Each time you need to get the current timezone, just use Utcstamp + Timezone_diff.

In addition to acquiring time, in order to format the display time, we can also use some system functions to format the output time string

Char *asctime (const struct TM *TM);

Char *asctime_r (const struct TM *tm, char *buf);

Char ctime (const time_t *TIMEP);

Char ctime_r (const time_t *TIMEP, char *buf);

In the actual application of the game, generally according to the specific needs to display the format of time, and even to do some special processing of the boundary time, so there is no detailed discussion of the format of the time display.

Time functions in C + +

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.