C language Gets the current time (Linux environment)

Source: Internet
Author: User

in the Linux environment to write programs in C language to get the current time as long as the call its internal function. These functions are in the Time.h header file, the first function prototype:

① time_t Time (time_t *t), the Linux man is also handy to find instructions for this function:

In the command-line mode of the Linux environment, you can find the description of the time function by entering man 2 time, which calculates the total number of seconds from January 1, 1970 to the current.

A function prototype for the second function is:

②STRUCT TM *localtime (const time_t *TIMEP)

A description of the time function can be found by entering man localtime in the command-line mode of the Linux environment. With these two functions you can write the program, the program is as follows:

  1. #include <stdio.h>
  2. #include <time.h>
  3. int main (void)
  4. {
  5. time_t t;
  6. T = time (NULL);
  7. printf ("Time in Seconds:%d\n", t);
  8. struct tm *p = localtime (&t);
  9. printf ("%d-", 1900+ p->tm_year); Year needs to add 1900
  10. printf ("%d-", 1+p->tm_mon); Month needs to add 1
  11. printf ("%d\t", p->tm_mday); Day
  12. printf ("%d:", p->tm_hour); Hour
  13. printf ("%d:", p->tm_min); Minute
  14. printf ("%d\t", p->tm_sec); Second
  15. printf ("week=%d\n", p->tm_wday); Week
  16. return 0;
  17. }

The result of the operation is:

C language Gets the current time (Linux environment)

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.