How to obtain the current system time

Source: Internet
Author: User

Required header file

# Include <time. h>
First, let's take an example of getting the current system time and explain the functions used.
Sample Code:
# Include <stdio. h>

# Include <time. h>

Int main ()
{
Time_t timep; // defines the time_t type variable timep, the type is long int type

Time (& timep); // store the obtained time

// Printf ("% s", asctime (localtime (& timep )));
// Convert the local time to ASCII code and print it out.
 
Return 0;
}
Function Definition
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,
The result is returned as a string. The current time zone of this function is converted to the local time.
 
Localtime ()

Function: converts the number of seconds offset from 00:00 to the current time to the local time,
The time after the gmtimes function is converted does not pass the time zone conversion, which is the UTC time.

Note: The time of the tm struct obtained by this function is the calendar time.

Usage: struct tm * localtime (const time_t * clock );

Return Value: returns the pointer to the tm struct. The tm struct is the struct defined in time. h used to separately store the amount of time (year, month, day, and so on.
 
The following describes a detailed sample code for using localtime () and asctime ().


# Include <stdio. h>

# Include <time. h>

Int main ()
{
Time_t timep; // defines the time_t type variable timep, the type is long int type
Struct tm * block;

Timep = time (NULL); // returns the number of seconds between the current time and 00:00:00, January 1, January 1, 1970.

Printf ("% ld \ n", timep );

Block = localtime (& timep); // store the obtained time

Printf ("% s", asctime (block); // convert the local time to ASCII code and print it out.

Return 0;

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.