Linux programming file and I/O (7): Time Function Summary

Source: Internet
Author: User

Obtain time from system clock

Time Function introduction:

 

Time. hIs the date and time header file in C/C ++.

Sample Code

# Include <stdio. h>

# Include <time. h>

Int main (void)

{

Time_t timer = time (NULL );

Printf ("ctime is % s \ n", ctime (& timer); // obtain the calendar time

Return 0;

}

Obtain time from system clock

Time_tTime(Time_t *Timer)

Obtain the number of seconds from the standard time point (generally midnight on January 1, January 1, 1970) to the current time.

Clock_tClock(Void)

Obtain the total number of milliseconds from the start of the program to the function call.

Time Function Introduction 1. Function Name: localtime

Function prototype: struct tm * localtime (const time_t * timer)

Function: returns the machine time information expressed in the tm structure.

Function return: The time expressed in the tm structure. The tm structure is defined as follows:

Struct tm {

Int tm_sec;

Int tm_min;

Int tm_hour;

Int tm_mday;

Int tm_mon;

Int tm_year;

Int tm_wday;

Int tm_yday;

Int tm_isdst;

};

Parameter description: timer-machine time obtained using the time () function

File: <time. h>

# Include <time. h>

# Include <stdio. h>

# Include <dos. h>

Int main ()

{

Time_t timer;

Struct tm * tblock;

Timer = time (NULL );

Tblock = localtime (& timer );

Printf ("Local time is: % s", asctime (tblock ));

Return 0;

}

2. Function Name: asctime

Function prototype: char * asctime (struct tm * ptr)

Function: Get the machine time (date and time are converted to ASCII code)

Function return: The returned time string is in the format of week, month, day, hour: minute: Second, year

Parameter description: The structure pointer ptr should be obtained through the localtime () and gmtime () functions.

3. Function Name: ctime

Function prototype: char * ctime (long time)

Function: Get the calendar time.

Function return: Return string format: week, month, day, hour: minute: Second, year

Parameter description: time-this parameter should be obtained by the function time.

File: <time. h>

# Include <stdio. h>

# Include <time. h>

Int main ()

{

Time_t t;

Time (& t );

Printf ("Today's date and time: % s", ctime (& t ));

Return 0;

}

4. Function Name: difftime

Function prototype: double difftime (time_t time2, time_t time1)

Function: returns the time difference between two machines, in seconds.

Function return: time difference, in seconds

Parameter description: time1-machine time 1, time2-machine time 2. This parameter should be obtained using the time function.

File: <time. h>

# Include <time. h>

# Include <stdio. h>

# Include <dos. h>

# Include <conio. h>

Int main ()

{

Time_t first, second;

Clrscr ();

First = time (NULL );

Delay (2000 );

Second = time (NULL );

Printf ("The difference is: % fseconds", difftime (second, first ));

Getch ();

Return 0;

}

5. Function Name: gmtime

Function prototype: struct tm * gmtime (time_t * time)

Function: Obtain the time information in the format of the structured tm.

Function return: Time Information pointer expressed in the structure tm

Parameter description: time-time information obtained using the time () function

File: <time. h>

# Include <stdio. h>

# Include <stdlib. h>

# Include <time. h>

# Include <dos. h>

Char * tzstr = "TZ = PST8PDT ";

Int main ()

{

Time_t t;

Struct tm * gmt, * area;

Putenv (tzstr );

Tzset ();

T = time (NULL );

Area = localtime (& t );

Printf ("Local time is: % s", asctime (area ));

Gmt = gmtime (& t );

Printf ("GMT is: % s", asctime (gmt ));

Return 0;

}

6. Function Name: time

Function prototype: time_t time (time_t * timer)

Function: Get the calendar time of the machine or set the calendar time.

Function return: Machine calendar time

Parameter description: When timer = NULL, the calendar time of the machine is obtained. When timer = time value, it is used to set the calendar time. time_t is a long type.

File: <time. h>

# Include <time. h>

# Include <stdio. h>

# Include <dos. h>

Int main ()

{

Time_t t;

T = time ();

Printf ("The number of seconds since January 1,1970 is % ld", t );

Return 0;

}

7. Function Name: tzset

Function prototype: void tzset (void)

Function: a unix-compatible function used to obtain the time zone. It is useless in a DOS environment.

Function return:

Parameter description:

File: <time. h>

# Include <time. h>

# Include <stdlib. h>

# Include <stdio. h>

Int main ()

{

Time_t td;

Putenv ("TZ = PST8PDT ");

Tzset ();

Time (& td );

Printf ("Current time = % s", asctime (localtime (& td )));

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.