How does VC get system time?

xiaoxiao2021-03-06  62

From: http://s89.tku.edu.tw/~489191360/getsystime.txt

If the MFC can use the CTIME class, if not the method of introducing the following

In many cases, we must obtain the system time (like an operation ...),

But how do you get system time?

TurboC provides time (); (defined in ) This card is for us to obtain system time.

When we execute time (null); this function will pass back a TIME_T type (defined in )

The number, the unit is "second", so we should first declare a variable by Time_T to receive the return value of TIME ():

Time_t t; // Declared variable T to Time_T type ///

T = Time (null); // Variable T is equal to the return value of TIME (NULL) ///

So the variable t received a large integer of "second", and the meaning representative of this integer is:

From the January 1, 1970, 0:0am and 0 seconds to the number of seconds passing through.

Wow! Isn't this the system time we want ....

But if you can't replace the indication is meaningless.

It is difficult to obtain the system time, which is a long-minded, and the mathematical classmates may want to rule out.

Use exquisite division, solve the problem of leap years, one by one / month / day / hour / minute ...

However, this test topic may have to take home to write,

TurboC provides a localtime (); (defined in ) this file is divided

The function of Time_T type information is roughly as follows from ON-Line Help.

StructTM * localtime (TIME_T *);

--------------------------

From here you can see that LocalTime needs to know the address of a TIME_T type,

Then transmit the location of the STRUCT TM structure.

This problem is coming, to tell LOCALTIME () The address of the time_t variable is simple, the method is as follows:

Time_t t; // Declare a Time_T type variable T //

T = Time (null); // t = system time //

Localtime (& T); // Plute the location & t of T to LOCALTIME () //

This is fine, but what is Struct TM?

(Struct is the structure, usage similar to Class but no Member function)

The introduction of TM in ON-Line Help is as follows:

// is defined by ///

StructTM {

INTTM_SEC; / * Second (0--59) * /

INTTM_MIN; / * points (0--59) * /

INTTM_HOUR; / * Time (0 - 23) * /

INTTM_MDAY; / * Daily of Month (1--31) * /

INTTM_MON; / * month (0--11 remember ourselves 1) * /

INTTM_YEAR; / * year (to bring this value 1900 is the teenage age) * /

INTTM_WDAY; / * OF WEEK (0--6; Sunday = 0) * /

IntTm_yday; / * day of year (0--365) * /

INTTM_ISDST; / * 0 If Daylight Savings Time Is Not in Effect) * /

}

Seeing this, you can know that localtime () first converts our Time_T data to adult / month / day / hour / minute / minute, and then in the order in which the structure TM is in the memory,

The memory order is returned.

It turns out, then we don't just declare an indicator of a TM type, then point to this indicator

LocalTime () return value, then read the content of this indicator is ok ^^ Try it right away:

#include

#include

Void main (void)

{

Int year, mon, day; // to use //

Int Hour, min, sec; // to be used for time //

Time_t T; // To store system time //

StructTM * TimeP; // To refer to the location of LOCALTIME () transferred //

T = Time (null); // save the system time to t //

Timep = localtime (& T); // TimeP refers to the location site where localtime is good.

// Let's take a look at the place you refer to TIMEP ^^ //

Year = (TimeP-> TM_YEAR) 1900;

MON = (TimeP-> TM_MON) 1;

Day = (TimeP-> TM_MDAY);

Hour = (TimeP-> TM_HOUR);

min = (TimeP-> TM_MIN);

Sec = (TimeP-> TM_SEC);

Cout << Year << "/" << mon << "/" << day << endl;

COUT << Hour << ":" << min << ":" << sec << endl;

}

The result is that LocalTime () has helped us call T. (touch ~~),

And the members we point out from TimeP also pass the correct data, no problem in even the date ^^

In short, we have learned how to get the system time.

The rest is how you use it.

转载请注明原文地址:https://www.9cbs.com/read-85569.html

New Post(0)