Time (null) and getlocaltime get the current time

Source: Internet
Author: User

To a time () function and localtime () function in the header file of # inlcude <time. h>

Function prototype: time_t time (time_t * timer)
Function purpose: Obtain the calendar time of the machine or set the calendar time.
Header file: Time. h
If the input parameter is Timer: = NULL, the calendar time of the machine is obtained. If the input parameter is = Time, the calendar time is set;

Time_t is a long type

Function prototype: struct TM * localtime (const time_t * timer)
Function purpose: return the machine time information expressed in the TM structure.
Header file: Time. h
Input parameter: Timer: The machine time obtained by using the time () function;

The structure TM is defined:
Struct TM
{
Int tm_sec;/* seconds: 0-59 (K & R says 0-61 ?) */
Int tm_min;/* minutes: 0-59 */
Int tm_hour;/* hours since midnight: 0-23 */
Int tm_mday;/* day of the month: 1-31 */
Int tm_mon;/* months * Since * January: 0-11 */
Int tm_year;/* years since 1900 */
Int tm_wday;/* Days since Sunday (0-6 )*/
Int tm_yday;/* Days since Jan. 1: 0-365 */
Int tm_isdst;/* + 1 daylight savings time, 0 No DST,
*-1 don't know */
};

For example:
# Include "stdafx. H"
# Include <time. h>
# Include <stdio. h>
# Include <wtypes. h>

Int main (INT argc, char * argv [])
{
Time_t T;
TM * TP;
 
T = time (null );
// Since time_t is actually a long integer, to a future day, from a time point (usually January 1, 1970 00:00:00)
// The number of seconds (that is, the calendar time) at that time is beyond the range of the number that can be expressed by a long integer. What should I do? For values of the time_t data type,
// It indicates that the time cannot be later than January 18, 2038. Some compiler vendors
// A 64-bit or longer integer is introduced to save the calendar time. For example, Microsoft uses the _ time64_t data type in Visual C ++.
// Save the calendar time and use the _ time64 () function to obtain the calendar time (instead of using the 32-bit time () function ).
// You can use this data type to save the time before 00:00:00, January 1, January 1, 3001 (excluding this time point.

TP = localtime (& T );
Printf ("% d/N", TP-> tm_mon + 1, TP-> tm_mday, TP-> tm_year + 1900 );
Printf ("% d: % d/N", TP-> tm_hour, TP-> tm_min, TP-> tm_sec );
 
Systemtime stcurtime = {0 };
: Getlocaltime (& stcurtime );

// Differentiate getsystemtime (& stcurtime );
Printf ("% d/N", stcurtime. wmonth, stcurtime. wday, stcurtime. wyear );
Printf ("% d: % d/N", stcurtime. whour, stcurtime. wminute, stcurtime. wsecond );
Return 0;
}

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.