Parsing the time function under Linux: Setting and acquiring the time method _c language

Source: Internet
Author: User
Tags local time set time
one, Time function
Copy Code code as follows:

time_t time (time_t *t);
Char *asctime (const struct TM *TM);
Char *asctime_r (const struct TM *tm, char *buf);
Char *ctime (const time_t *TIMEP);
Char *ctime_r (const time_t *TIMEP, char *buf);
struct TM *gmtime (const time_t *TIMEP); Acquired for the British time
struct TM *gmtime_r (const time_t *TIMEP, struct TM *result);
struct TM *localtime (const time_t *TIMEP); Get the local time, pay attention to the difference with English time.
struct TM *localtime_r (const time_t *TIMEP, struct TM *result);
time_t mktime (struct TM *tm);
Double Difftime (time_t time1, time_t TIME0);
int gettimeofday (struct timeval *tv, struct timezone);
int settimeofday (const struct Timeval *tv, const struct timezone);

ii. Setup and acquisition time
Copy Code code as follows:

#include <stdio.h>
#include <time.h>

int main (void)
{
time_t T1;
time_t T2;
struct TM *my_tm;
Char buf[128] = {0};

Number of seconds from epoch (00:00:00 UTC, January 1,1970)
T1 = time (&T1);
printf ("%d\n", T1); //1355905754
t2 = time (&t2);

Sleep (1);
printf ("%lf\n", difftime (T2, T1)); //t1,t2 difference: 1.000000, sometimes you can use this function to do the pseudo timer
printf ("%s\n", CTime (&T1)); //wed Dec 19 16:29:14 2012

Init TM
My_tm->tm_year = 2012-1900;
My_tm->tm_mon = 12-1;
My_tm->tm_mday = 12;
My_tm->tm_hour = 12;
My_tm->tm_min = 12;
My_tm->tm_sec = 12;
//Set time
T1 = mktime (MY_TM);
//Get time

My_tm = LocalTime (&AMP;T1);
sprintf (buf, "%04d-%02d-%02d%02d:%02d:%02d",
My_tm->tm_year + 1900, My_tm->tm_mon + 1, my_tm->tm_mday, My_tm->tm_hour, My_tm->tm_min, my_tm->tm_ SEC);
printf ("%s\n", buf); //2012-12-12 12:12:12

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.