C Language Acquisition system time of several ways __c language

Source: Internet
Author: User
Tags local time set time time interval


How to get time in C language . how accurate.
1 accurate to seconds using time_t time (time_t * timer)
2 using clock_t clock (), the CPU time is accurate to 1/clocks_per_sec seconds.
3 Calculating time difference using double difftime (time_t timer1, time_t Timer0)
4 Using the DWORD GetTickCount () to the millisecond
5 If you use MFC's CTime class, you can use Ctime::getcurrenttime () to get the exact second
6 to get high precision time, you can use
BOOL QueryPerformanceFrequency (Large_integer *lpfrequency)
Gets the frequency of the system's counters
BOOL QueryPerformanceCounter (Large_integer *lpperformancecount)
Get the value of a counter
Then use the difference of two times the counter divided by frequency to get the time.
7 Multimedia Timer functions
The following functions are used with multimedia timers.
Timebeginperiod/timeendperiod/timegetdevcaps/timegetsystemtime
//*********************************************************************
The function of obtaining the current system time with standard C implementation

A. Time () function

The

     time (&rawtime) function gets the number of seconds in the current time from January 1, 1970, in seconds Count units, in Rawtime.
#include "time.h"  
Void Main ()  

time_t rawtime; 
Struct TM * timeinfo; 
Time (&rawtime);  
Timeinfo = localtime (&rawtime);  
printf (/007the the current date/time is :%s, Asctime (Timeinfo));  
Exit (0);  

================= 
#include  -- Required time function header file  
time_t--Time type (time.h definition is typedef long time_t;  traced, time_t is long)
struct TM--time structure, time.h definition As follows:  
int tm_sec; 
int tm_min; 
int tm_hour; 
int tm_mday; 
int Tm_mon;  
int tm_year; 
int tm_wday; 
int tm_yday; 
int tm_isdst; 
Time (&rawti ME); --acquisition of time, in seconds, starting from January 1, 1970, in rawtime 
LocalTime (&rawtime);--to local time, TM time structure  
Asctime ()--to standard ASC II Time Format: &NBSP
Day of the week: minutes: Second year

-----------------------------------------------------------------------------
Two. Clock () function, using the clock () function, to get the system after the start of the millisecond time, and then divided by clocks_per_sec, can be replaced by "seconds", the standard C function.
clock_t clock (void);
#include
clock_t t = clock ();
Long sec = t/clocks_per_sec;
He is recording the clock cycle, the implementation does not appear to be very accurate, need to test validation;
---------------------------------------------------------------------------
Three. gettime (&t); It is said that the time structure of tc2.0 contains millisecond information
#include
#include
int main (void)
{
struct time t;
GetTime (&t);
printf ("The current time is:%2d:%02d:%02d.%0 2d/n ",
T.ti_hour, T.ti_min, T.ti_sec, T.ti_hund);
return 0;
}
Time is a struct in which the member function Ti_hund is milliseconds ...

--------------------------------------------------------------------------------
Four. GetTickCount (), this is a function commonly used in Windows to calculate the running time of the program;
DWORD Dwstart = GetTickCount ();
Here to run your program code
DWORD dwend = GetTickCount ();
Then (Dwend-dwstart) is your program run time, in milliseconds
This function is only accurate to 55ms,1 a tick is 55ms.
--------------------------------------------------------------------------------
Five. timeGetTime () T,imegettime () is basically equal to GetTickCount (), but the precision is higher
DWORD Dwstart = timeGetTime ();
Here to run your program code
DWORD dwend = timeGetTime ();
Then (Dwend-dwstart) is your program run time, in milliseconds
Although the returned value unit should be MS, the legendary precision is only 10ms.
=========================================
Unix
# #unix时间相关, it's a standard library.
//*********************************************************************
The 1.TIMEGM function simply converts the struct TM structure into a time_t structure and does not use time zone information;
time_t timegm (struct TM *tm);
2.mktime Use time zone information
time_t mktime (struct TM *tm);
The Timelocal function is the GNU extended equivalent to the POSIX function mktime
time_t timelocal (struct TM *tm);
The 3.gmtime function simply converts the time_t structure into a struct TM structure and does not use time zone information;
struct TM * gmtime (const time_t *clock);
4.localtime Use time zone information
struct TM * localtime (const time_t *clock);
1.time acquisition time, Stime set time
time_t T;
T = time (&t);
2.stime its parameter should be GMT time, set to local time according to local timezone;
int stime (time_t *tp)
3.utc=true indicates the use of daylight saving time;
4. File modification time and other information are all used GMT time storage, different systems in the modified time after the localtime conversion cost time;
5. Set time zone recommended to use setup;
6. Set time zone can also be accounts/etc/sysconfig/clock in the setting before ln-fs/usr/share/zoneinfo/xxxx/xxx/etc/localtime can be again effective
Time_t can only express the 68 range, that mktime can only return 1970-2038 of this range of time_t
See if your system has time_t64, it can indicate a larger time range
Windows
# #Window里面的一些不一样的
//*********************************************************************

I. CTime () class
VC programming typically uses the CTime class to get the current date and time

CTime t = getcurrenttime ();  
SYSTEMTIME structure contains millisecond information
typedef struct _SYSTEMTIME { 
WORD wyear; 
Word wmonth; 
Word wdayofweek; 
Word wday; 
Word whour; 
Word wminute; 
Word wsecond; 
Word wmilliseconds;
} SYSTEMTIME, *psystemtime;
SYSTEMTIME T1;
GetSystemTime (&t1)  
CTime curtime (t1);  
WORD ms = T1.WMILLISECONDS; 
SYSTEMTIME Systm;
:: Getlocaltime (&SYSTM);
_strtime ()//in time.h can only use
Char t[11] in Windows;
_strtime (t);
puts (t);

//*****************************
Get current date and time
CTime Tm=ctime::getcurrenttime ();
CString Str=tm. Format ("%y-%m-%d");
In VC, we can use CTime time class, get the system current date, the concrete usage method is as follows:
CTime t = ctime::getcurrenttime (); Get the system date, stored in t
int D=t.getday (); Get Current date
int Y=t.getyear (); Get Current year
int M=t.getmonth (); Get Current Month
int H=t.gethour (); Get the current for when
int Mm=t.getminute (); Get the current minute
int S=t.getsecond (); Get the current second
int W=t.getdayofweek (); Get day of the week, note 1 for Sunday, 7 for Saturday

Two. CTimeSpan class
If you want to calculate the difference between two periods of time, you can use the CTimeSpan class, using the following methods:
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 (); How many days are there to get this time interval
int Ihour=span. Gettotalhours (); Get how many hours a total
int Imin=span. Gettotalminutes ()//Get the total number of minutes
int Isec=span. Gettotalseconds ()//Get total number of seconds


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

Three. _timeb () function
_timeb defined in Sys/timeb. H, there are four fields
Dstflag
Millitm
Time
TimeZone
void _ftime (struct _timeb *timeptr);
struct _TIMEB timebuffer;
_ftime (&timebuffer);
Take the current time: The document can be spoken to MS, someone tested, as if only to 16ms!

Four. Set Timer
Define Timer ID
#define Timerid_jisuanfangshi 2
Set the clock in place where it is needed to begin its function;
SetTimer (Timerid_jisuanfangshi,200,null);
Destroy the clock when the timer is not needed
KillTimer (Timerid_jisuanfangshi);
The message map of the corresponding VC program
void Cjisuan::ontimer (UINT nidevent)
{switch (nidevent)}
---------------------------------------------------------------------------------------
# #如何设定当前系统时间---------------------------------------windows
SYSTEMTIME M_mylocaltime,*lpsystemtime;
m_mylocaltime.wyear=2003;
M_MYLOCALTIME.WM;
M_mylocaltime.wday=1;
m_mylocaltime.whour=0;
m_mylocaltime.wminute=0;
M_mylocaltime.wsec;
M_mylocaltime.wmillisec;
lpsystemtime=&m_mylocaltime;
if (Setlocaltime (lpsystemtime))//Here is replaced by SetSystemTime ()
MessageBox ("OK!");
Else
MessageBox ("Error!");
SYSTEMTIME M_mylocaltime,*lpsystemtime;
m_mylocaltime.wyear=2003;
M_MYLOCALTIME.WM;
M_mylocaltime.wday=1;
lpsystemtime=&m_mylocaltime;
if (setdate (lpsystemtime))//Here is replaced by SetSystemTime ()
MessageBox ("OK!");
Else
MessageBox ("Error!");


See the original: http://blog.163.com/gao_junfu/blog/static/91177329200962211185037/


Related Article

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.