How to get time in C language? How accurate?
1 using time_t time (time_t * timer) accurate to seconds
2 using clock_t clock () to get the CPU time accurate to 1/clocks_per_sec seconds
3 calculation time difference using double difftime (time_t timer1, time_t Timer0)
4 using DWORD GetTickCount () to milliseconds
5 If you use MFC's CTime class, you can use Ctime::getcurrenttime () to be accurate to seconds
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)
Gets the value of the counter
Then divide the difference of two times by dividing the frequency to get the time.
7 Multimedia Timer Functions
The following functions is used with multimedia timers.
Timebeginperiod/timeendperiod/timegetdevcaps/timegetsystemtime
//*********************************************************************
A function to get the current system time using standard C
A. Time () function
The time (&rawtime) function gets the number of seconds, in seconds, of the current interval of January 1, 1970, stored in Rawtime.
#include "time.h"
void Main ()
{
time_t Rawtime;
struct TM * TIMEINFO;
Time (&rawtime);
Timeinfo = LocalTime (&rawtime);
printf ("/007the Date/time is:%s", Asctime (Timeinfo));
Exit (0);
}
=================
#include--The 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 is defined 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 (&rawtime); --acquisition time, in seconds, from January 1, 1970, deposited in Rawtime
LocalTime (&rawtime); --Convert to local time, TM time structure
Asctime ()--Convert to standard ASCII time format:
Day of the Week: minute: Second year
-----------------------------------------------------------------------------
Two. The clock () function, using the clock () function, to get the millisecond time after the system is started, 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 seem very accurate, need to test validation;
---------------------------------------------------------------------------
Three. gettime (&t); It is said that tc2.0 's time structure 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, where the member function Ti_hund is milliseconds ...
--------------------------------------------------------------------------------
Four. GetTickCount (), which is commonly used in Windows to calculate the program run time function;
DWORD Dwstart = GetTickCount ();
Run your program code here
DWORD dwend = GetTickCount ();
(Dwend-dwstart) is your program run time, in milliseconds
This function is only accurate to 55ms,1 tick is 55ms.
--------------------------------------------------------------------------------
Five. timeGetTime () T,imegettime () is basically equal to GetTickCount (), but with higher accuracy
DWORD Dwstart = timeGetTime ();
Run your program code here
DWORD dwend = timeGetTime ();
(Dwend-dwstart) is your program run time, in milliseconds
Although the returned value unit should be MS, the legendary accuracy is only 10ms.
=========================================
Unix
# #unix时间相关, also the standard library
//*********************************************************************
The 1.TIMEGM function simply turns the struct TM structure into a time_t structure without using time zone information;
time_t timegm (struct TM *tm);
2.mktime Using time zone information
time_t mktime (struct TM *tm);
The Timelocal function is the GNU extension equivalent to the POSIX function mktime
time_t timelocal (struct TM *tm);
The 3.gmtime function simply turns the time_t structure into a struct TM structure without using time zone information;
struct TM * gmtime (const time_t *clock);
4.localtime Using time zone information
struct TM * localtime (const time_t *clock);
1.time get 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 said 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. Setting the time zone is recommended using Setup;
6. Setting the time zone can also be notconsistent the settings in/etc/sysconfig/clock before ln-fs/usr/share/zoneinfo/xxxx/xxx/etc/localtime can be re-effective
time_t can only represent the 68 range, that is, Mktime can only return 1970-2038 of this range of time_t
See if your system has time_t64, it can represent a larger time range.
Windows
# #Window里面的一些不一样的
//*********************************************************************
I. CTime () class
VC programming generally uses the CTime class to get the current date and time
ctime t = GetCurrentTime ();
SYSTEMTIME The structure contains the 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 be used in Windows
Char t[11];
_strtime (t);
puts (t);
//*****************************
Get the current date and time
CTime Tm=ctime::getcurrenttime ();
CString Str=tm. Format ("%y-%m-%d");
In VC, we can use the CTime time class to get the current date of the system, using the following methods:
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 (); Gets the current when
int Mm=t.getminute (); Get Current minute
int S=t.getsecond (); Gets 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 method:
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 (); Get how many days are there in this time interval
int Ihour=span. Gettotalhours (); Get a total of how many hours
int Imin=span. Gettotalminutes ();//Get a total of how many minutes
int Isec=span. Gettotalseconds ();//Gets the 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 said to MS, someone testing, as if only to 16ms!
Four. Set the timer
Defining the Timer ID
#define Timerid_jisuanfangshi 2
Set the clock where appropriate, where it needs to begin;
SetTimer (Timerid_jisuanfangshi,200,null);
Destroy the clock when the timer is not needed
KillTimer (Timerid_jisuanfangshi);
Message map corresponding to 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 to SetSystemTime () also not
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 to SetSystemTime () also not
MessageBox ("OK!");
Else
MessageBox ("Error!");
Go C Language Acquisition system time