2009-12-09 20:54
Time.h
Some people summed up in such a few words, is indeed a classic, their own good programming to try the effect of it,
Two types:
time_t: Represents the number of seconds from 1970-01-01 00:00:00 from UTC time. Also known as the calendar.
struct TM: is typically used to store local time.
Several functions:
Time: Gets the time_t value of the current system times (UTC time).
CTime: Converts the time_t value to a string representing the local time.
GmtTime: Converts the time_t value to a string representing the GMT time.
LocalTime: Converts the time_t to a STRUNCT TM structure that represents the local time.
Mktime: Converts the struct TM that represents the local time to time_t.
Asctime: Converts the struct TM to a string form.
Difftime: The difference between the two calendars.
========================================================================
@ Function Name: localtime
Function prototypes: struct TM *localtime (const time_t *timer)
function function: Returns a machine time information expressed in the TM structure
function returns: The time that is expressed in the TM structure, the structure TM is defined as follows:
struct tm{
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;
};
Parameter description: timer-using the time () function to obtain the machine times
File belongs: <time.h>
#include <time.h>
#include <stdio.h>
#include <dos.h>
int main ()
{
time_t timer;
struct TM *tblock;
Timer=time (NULL);
Tblock=localtime (&timer);
printf ("Local time is:%s", Asctime (Tblock));
return 0;
}
@ Function Name: Asctime
Function prototypes: char* asctime (struct TM * ptr)
function function: Get machine time (date time converted to ASCII code)
function return: The time string returned is in the form of: Week, month, day, hour: minutes: seconds, years
Parameter description: The structure pointer ptr should be obtained by Function localtime () and Gmtime ()
File belongs: <time.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
int main ()
{
struct TM T;
Char str[80];
T.tm_sec=1;
t.tm_min=3;
t.tm_hour=7;
t.tm_mday=22;
t.tm_mon=11;
t.tm_year=56;
t.tm_wday=4;
t.tm_yday=0;
t.tm_isdst=0;
strcpy (Str,asctime (&t));
printf ("%s", str);
return 0;
}
@ Function Name: CTime
Function prototype: Char *ctime (long time)
function function: Get calendar Time
function return: Returns the string format: week, month, day, hour: minutes: seconds, years
Parameter description: time-This parameter should be obtained by function time
File belongs: <time.h>
#include <stdio.h>
#include <time.h>
int main ()
{
time_t T;
Time (&t);
printf ("Today's date and time:%s", CTime (&t));
return 0;
}
@ Function Name: Difftime
Function prototypes: Double Difftime (time_t time2, time_t time1)
function function: Get two times Machine time difference, the unit is seconds
function return: Time difference, in seconds
Parameter description: time1-machine time, time2-machine time two. This parameter should be obtained using the
File belongs: <time.h>
#include <time.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
int main ()
{
time_t, second;
CLRSCR ();
First=time (NULL);
Delay (2000);
Second=time (NULL);
printf ("The difference is:%f seconds", Difftime (Second,first));
Getch ();
return 0;
}
@ Function Name: Gmtime
Function prototypes: struct TM *gmtime (time_t *time)
function function: Get the time information represented by the structure TM
function return: A time information pointer represented by a structure TM
Parameter description: time-with function time () to get the information
File belongs: <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <dos.h>
Char *tzstr= "TZ=PST8PDT";
int main ()
{
time_t T;
struct TM *gmt, *area;
Putenv (TZSTR);
Tzset ();
T=time (NULL);
Area=localtime (&t);
printf ("local time is:%s", Asctime (area));
Gmt=gmtime (&t);
printf ("GMT is:%s", Asctime (GMT));
return 0;
}
@ Function Name: time
Function prototype: time_t time (time_t *timer)
function function: Get the machine calendar time or set the calendar time
function return: Machine calendar time
Parameter description: Timer=null when the machine calendar time, timer= time value, to set the calendar time, time_t is a long type
File belongs: <time.h>
#include <time.h>
#include <stdio.h>
#include <dos.h>
int main ()
{
time_t T;
T=time ();
printf ("The number of seconds since January 1,1970 is%ld", t);
return 0;
}
@ Function Name: Tzset
Function prototype: void Tzset (void)
function function: Unix compatibility function, used to get time zone, no use in DOS environment
function returns:
Parameter description:
File belongs: <time.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
int main ()
{
time_t TD;
Putenv ("TZ=PST8PDT");
Tzset ();
Time (&TD);
printf ("Current time=%s", Asctime (LocalTime (&TD));
return 0;
}
=================================================================================
1.Coordinated Universal Time (UTC): When coordinating the world, also known as world standard Times, or Greenwich Mean Time (Greenwich Mean time,gmt), the time difference between the Chinese mainland and UTC is +8, that is, the utc+ 8, the United States for UTC-5.
2.Calendar Time: Calendar time, which is represented by the "number of seconds elapsed from a standard point in time to this time". Standard time points may be different for different compilers, but standard time is constant for a compiled system.
3.epoch: Point in time. is an integer in standard C + + that is represented by the number of seconds (that is, the calendar time) that differs between the time and the standard point in time.
4.clock tick: Clock timer (not called clock tick), the length of a clock timer is controlled by the CPU, a clock tick is not a clock cycle of the CPU, but a basic timing unit of C/S.
5. Timing function: clock_t clock (void)//return value: The number of CPU clock timing units (clock tick) between the "Open this program process" and the call to clock () function in the program, which is called the Wall Clock Time (Wal-clock) in MSDN, which
Time.h
#ifndef _clock_t_defined
typedef long CLOCK_T;
#define _clock_t_defined
#endif
Time.h
The #define CLOCKS_PER_SEC ((clock_t) 1000)//clock_per_sec indicates how many clock ticks will be in a second, and the smallest unit of time in C + + is one millisecond, available through clock ()/clock_t_ SEC gets the process itself running time
6. Data structure TM storing time and date:
Time.h
#ifndef _tm_defined
struct TM
{
int tm_sec;//seconds, [0-59]
int tm_min;//minutes, [0-59]
int tm_hour;//, [0-23]
int tm_mday;//date in one months, [1-31]
int tm_mon;//month, [0-11]
int tm_year;//year with a value of actual year-1900
int tm_wday;//week, [0-6]
int tm_yday;//count starting from January 1 of each year, [0-365]
int tm_isdst;//Daylight identifier, implement daylight saving time, tm_isdst to be positive; do not practice daylight saving, TM_ISDST is 0; When you do not know the situation, TM_ISDST is negative
};
#define _tm_defined
#endif
7. Calendar time is stored in time_t:
Time.h
#ifndef _time_t_defined
typedef long time_t;
#define _time_t_defined
#endif
The calendar time represented by time_t can only be stored until January 18, 2038 19:14 07 seconds, in order to be able to represent a longer time, some compilers introduce 64-bit or even longer reshaping number to save the calendar time, such as VC in the use of _time64_t data type, through _TIME64 () function to get the calendar time (not 32-bit time ()) to calculate the date before January 1, 3001 0:0 0 seconds (excluding that point)
8. You can use the Time.h header file in the ANSI standard library, which defines the methods used for the time and date, and in the time.h header, we can see functions that are either time_t as parameter types or return value types:
Double Difftime (time_t time1, time_t TIME0);//accurate to second,
time_t mktime (struct TM * timeptr);//The time represented by the TM structure is converted to a calendar time, and the return value is the converted calendar time, which, while many compilers can pass through the time before January 1, 1970, will terminate the operation abnormally
time_t Time (time_t * timer)//Get the Calendar times from one point to the present (calendar date)
char * asctime (const struct TM * timeptr);//In a fixed format to display time, the return time format is: Days of the Day of the week: minutes: the second year \n\0;
char * CTime (const time_t *timer);//The time in a fixed format, which is the time of day of the week: the day of the month: minutes: \n\0, where \ n is a newline, and the end of the string, converting the calendar time to local time, and then generating a formatted string
In addition, Time.h also provides two different functions to convert the calendar time (an integer represented by time_t) to the time format TM:
struct TM * gmtime (const time_t *timer)//Convert calendar time to standard Time, return the TM structure to save this time, where the timer can be returned with the value
struct TM * localtime (const time_t * timer)//Converts the calendar time to local time, where the value returned by the timer () can be
By looking at MSDN, we can see that the value of the time point (the value of the Time_t object) in Microsoft C/s + + 7.0 is the number of seconds elapsed from December 31, 1899 0:0 0 seconds to that point, while the other various versions of Microsoft C + + And all different versions of Visual C + + are calculated by the number of seconds from January 1, 1970 0:0 0 seconds to that point in time.
8. Custom time Format: Use the strftime () function to format the time as we want
size_t strftime (char *strdest,size_t maxsize,const char *format,const struct TM *timeptr); Place the time information stored in the timeptr in the strdest pointed string according to the format command in the string, and hold the maxsize character up to strdest. The function returns the number of characters placed in the string pointed to strdest. Similar to sprintf (), identifies a set of formatting commands that begin with a percent sign (%), and the formatted output is placed in a string. The format command describes the exact representation of the various date and time information in the string strdest. Other characters in the format string are placed in the string as is. The formatting commands are listed below, and they are case-sensitive.
%a Day of the week
%A the full name of the week
%b of the Month
Full name of%B month
Time series for the%c standard date
Rear two digits of%c year
The first day of the month in%d decimal notation
%d Month/day/year
%e in a two-character field, the first day of the month in decimal notation
%F year-month-day
Two digits of%g year, using week-based years
%G years, using weeks based
%h Abbreviated month name
%H 24-hour system hours
%I 12-hour hours
%j decimal representation of the first day of the year
Month in%m decimal notation
Number of minutes represented by%m 10 o'clock
%n New Line character
%p the equivalent display of a local AM or PM
%r 12 hours.
%R display hours and minutes: hh:mm
%s decimal number of seconds
%t Horizontal Tab
%T when displayed: Hh:mm:ss
%u days of the week, Monday is the first day (values from 0 to 6, Monday to 0)
%u week of the year, make Sunday the first day (values from 0 to 53)
%V the first weeks of the year, using a week based year
%w decimal representation of the week (values from 0 to 6, Sunday to 0)
%w Week of the year, Monday is the first day (value from 0 to 53)
The date string of the%x standard
%x Standard Time series
%y decimal Year without century (values from 0 to 99)
%Y decimal year with the century part
%z,%z the time zone name and returns a null character if the time zone name cannot be obtained.
Percent percent%
Such as:
#include <stdio.h>
#include <time.h>
void Main (void)
{
struct TM *newtime;
Char tmpbuf[128];
time_t LT1;
Time (&LT1);
Newtime=localtime (&LT1);
Strftime (TMPBUF, 128, "This is%A, day%d '%B in the year%y.\n", NewTime);
printf (TMPBUF);
}
Run Result:
This is Saturday, and the July in the year 2005.
Http://hi.baidu.com/shallfun/blog/item/924d8f2ba21ad027d52af160.html