How to obtain time functions in linux

Source: Internet
Author: User

//-------------------------------------------------------------//
Asctime (represents the time and date in string format)
# Include <time. h>
Define functions
Char * asctime (const struct tm * timeptr );
Function Description
Asctime () converts the information in the tm structure referred to by the timeptr parameter to the time and date representation method used in the real world, and then returns the result in string form. This function has been converted from the time zone to the local time. The string format is "Wed Jun 30 21:49:08 1993 \ n"
Return Value
If you call the related time and date functions again, this string may be damaged. The difference between this function and ctime is that the input parameters are different structures.
Additional instructions
Returns a string representing the current local time and date.
Example Copy codeThe Code is as follows: # include <time. h>
Main ()
{
Time_t timep;
Time (& timep );
Printf ("% s", asctime (gmtime (& timep )));
}
Run
Sat Oct 28 02:10:06 2000

//-------------------------------------------------------------//
Ctime (Representation of time and date in string format)
Header file
# Include <time. h>
Define functions
Char * ctime (const time_t * timep );
Function Description
Ctime () converts the information in the time_t structure referred to by the timep parameter to the time and date representation method used in the real world, and then returns the result in string form. This function has been converted from the time zone to the local time. The string format is "Wed Jun 30 21: 49: 08 1993 \ n ". If you call the related time and date functions again, this string may be damaged.
Return Value
Returns a string representing the current local time and date.
ExampleCopy codeThe Code is as follows: # include <time. h>
Main ()
{
Time_t timep;
Time (& timep );
Printf ("% s", ctime (& timep ));
}
Run
Sat Oct 28 10: 12: 05 2000

Gettimeofday (get current time)
Header file
# Include <sys/time. h>
# Include <unistd. h>
Define functions
Int gettimeofday (struct timeval * TV, struct timezone * tz)
Function Description
Gettimeofday () will return the current time with the structure indicated by TV, and put the information of the local time zone in the structure indicated by tz.
The timeval structure is defined:
Struct timeval {
Long TV _sec;/* seconds */
Long TV _usec;/* microseconds */
};
The timezone structure is defined:
Struct timezone {
Int tz_minuteswest;/* the time difference between Greenwich and */
Int tz_dsttime;/* state of daylight saving time */
};
The above two structures are defined in/usr/include/sys/time. h. The status of tz_dsttime is as follows:
DST_NONE/* not used */
DST_USA/* United States */
DST_AUST/* Australia */
DST_WET/* Western Europe */
DST_MET/* Central Europe */
DST_EET/* Eastern Europe */
DST_CAN/* Canada */
DST_GB/* Great Britain */
DST_RUM/* Romania */
DST_TUR/* Turkey */
DST_AUSTALT/* Australia (after January 1, 1986 )*/
Return Value
If the call succeeds, 0 is returned. If the call fails,-1 is returned. The error code is stored in errno. Note that the memory space specified by the EFAULT pointer TV and tz exceeds the access permission.
ExampleCopy codeThe Code is as follows: # include <sys/time. h>
# Include <unistd. h>
Main (){
Struct timeval TV;
Struct timezone tz;
Gettimeofday (& TV, & tz );
Printf ("TV _sec; % d \ n", TV,. TV _sec );
Printf ("TV _usec; % d \ n", TV. TV _usec );
Printf ("tz_minuteswest; % d \ n", tz. tz_minuteswest );
Printf ("tz_dsttime, % d \ n", tz. tz_dsttime );
}
Run
TV _sec: 974857339
TV _usec: 136996
Tz_minuteswest:-540
Tz_dsttime: 0

//-------------------------------------------------------------//
Gmtime (get current time and date)
Header file
# Include <time. h>
Define functions
Struct tm * gmtime (const time_t * timep );
Function Description
Gmtime () converts the information in the time_t structure referred to by the timep parameter to the time and date representation method used in the real world, and then returns the result from the structure tm.
The structure tm is defined
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;
};
Int tm_sec indicates the current number of seconds. The normal range is 0-59, but the value can be 61 seconds.
Int tm_min indicates the current score, range: 0-59
Int tm_hour, which is counted from midnight, ranges from 0 to 23.
The number of days in the current month of int tm_mday, ranging from 01 to 31.
Int tm_mon indicates the current month. The value ranges from 0 to 11 from January 1, January.
Int tm_year: The number of years since January 1, 1900
The number of days in a week for int tm_wday. The value ranges from 0 to 6 from Monday.
Int tm_yday indicates the number of days since January 1, January 1 this year. The value range is 0-365.
Int tm_isdst time saving flag
The time and date returned by this function are not converted by the time zone, but UTC time.
Return Value
The returned structure tm indicates the current UTC time.
ExampleCopy codeThe Code is as follows: # include <time. h>
Main (){
Char * wday [] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat "};
Time_t timep;
Struct tm * p;
Time (& timep );
P = gmtime (& timep );
Printf ("% d", (1900 + p-> tm_year), (1 + p-> tm_mon), p-> tm_mday );
Printf ("% s % d; % d \ n", wday [p-> tm_wday], p-> tm_hour, p-> tm_min, p-> tm_sec );
}
Run
2000/10/28 Sat 8:15:38

//-------------------------------------------------------------//
Localtime (obtain the current local time and date)
Header file
# Include <time. h>
Define functions
Struct tm * localtime (const time_t * timep );
Function Description
Localtime () converts the information in the time_t structure referred to by the timep parameter to the time and date representation method used in the real world, and then returns the result from the tm structure. For the definition of the structure tm, see gmtime (). The time date returned by this function has been converted to the local time zone.
Return Value
The returned structure tm indicates the current local time.
ExampleCopy codeThe Code is as follows: # include <time. h>
Main (){
Char * wday [] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat "};
Time_t timep;
Struct tm * p;
Time (& timep );
P = localtime (& timep);/* obtain the local time */
Printf ("% d", (1900 + p-> tm_year), (l + p-> tm_mon), p-> tm_mday );
Printf ("% s % d: % d \ n", wday [p-> tm_wday], p-> tm_hour, p-> tm_min, p-> tm_sec );
}
Run
2000/10/28 Sat 11:12:22

//-------------------------------------------------------------//
Mktime (the number of seconds after the time structure data is converted)
Header file
# Include <time. h>
Define functions
Time_t mktime (strcut tm * timeptr );
Function Description
Mktime () is used to convert the tm structure data referred to by the timeptr parameter to the number of seconds that have elapsed since 00:00:00, January 1, January 1, 1970 AD.
Return Value
Returns the number of seconds that have elapsed.
ExampleCopy codeThe Code is as follows:/* obtain the time (in seconds) with time () and use localtime ()
Convert to struct tm and then use mktine () to convert struct tm to the original number of seconds */
# Include <time. h>
Main ()
{
Time_t timep;
Strcut tm * p;
Time (& timep );
Printf ("time (): % d \ n", timep );
P = localtime (& timep );
Timep = mktime (p );
Printf ("time ()-> localtime ()-> mktime (): % d \ n", timep );
}
Run
Time (): 974943297
Time ()-> localtime ()-> mktime (): 974943297

//-------------------------------------------------------------//
Settimeofday (set the current time)
Header file
# Include <sys/time. h>
# Include <unistd. h>
Define functions
Int settimeofday (const struct timeval * TV, const struct timezone * tz );
Function Description
Settimeofday () sets the current time to the structure information indicated by TV, and the local time zone information to the structure indicated by tz. For more information, see gettimeofday (). Note: Only the root permission can use this function to modify the time.
Return Value
If the call succeeds, 0 is returned. If the call fails,-1 is returned. The error code is stored in errno.
Error Code
The root permission of EPERM is not enough to call settimeofday.
The EINVAL time zone or data is incorrect and the time cannot be set correctly.
//-------------------------------------------------------------//
Time (obtain the current time)
Header file
# Include <time. h>
Define functions
Time_t time (time_t * t );
Function Description
This function returns the number of seconds that have elapsed since January 1, 1970 ad utc time since 00:00:00. If t is not a null pointer, this function also saves the returned value to the memory indicated by t pointer.
Return Value
The number of seconds is returned for success, and (time_t)-1) is returned for failure. The cause of the error is stored in errno.
ExampleCopy codeThe Code is as follows: # include <time. h>
Mian ()
{
Int seconds = time (time_t *) NULL );
Printf ("% d \ n", seconds );
}

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.