System Time in C Language

Source: Internet
Author: User
Tags time and date

1. Definition of variables:

Struct TM
{
Int tm_sec;/* (0-61 )*/
Int tm_min;/* (0-59 )*/
Int tm_hour;/* (0-23 )*/
Int tm_mday;/* (1-31 )*/
Int tm_mon;/* (0-11 )*/
Int tm_year;/* Past 1900 */
Int tm_wday;/* (0-6 )*/
Int tm_yday;/* (0-365 )*/
Int tm_isdst;/* Daylight Savings flag */
};

2. One instance: reads the system time, and displays:

/*
Example of a function related to system time in C Language

*/
# Include <stdio. h>
# Include <stdlib. h>
# Include <time. h>
# Include <dos. h>

Void main (void)
{
/* Time */
Time_t timer;

/* Pointer to struct TM */
Struct TM * ptrtime;

/* Call the time () function to obtain the current time */
Timer = Time (null );

/* Call the localtime () function to convert the obtained system time to the struct directed to the struct TM pointer */
Ptrtime = localtime (& timer );

/* Use asctime () to convert the struct to string output */
Printf ("now is: % s", asctime (ptrtime ));
Getch ();
}

3. Related header files

# Include <time. h>

4. Other functions

Asctime ()
Related functions Time, ctime, gmtime, localtime
Header file # 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.
Gettimeofday (get current time)
Related functions Time, ctime, ftime, settimeofday
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.

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.