Linux Time function

Source: Internet
Author: User
Tags function prototype

The Linux time function is in the time.h header file.

1. header file

The time-related header files are as follows:


Time.h
Sys/time.h
Sys/times.h
Sys/timeb.h
Sys/timex.h

Time.h is the header file for the C standard library, and the rest of Sys starts with the Linux system's own header files.

/usr/include/time.h defines the commonly used time functions.

To view these files in the/usr/include/sys directory:

Sys/time.h defines the time functions of the TIMEZONE structure and the Linux system.

SYS/TIMES.H defines the structure TMS that processes use CPU time.

Sys/timeb.h defines the structural body Timeb for the return value of the Ftime function.

Sys/timex.h defines the structural body Timex for the clock adjustment algorithm.

2. Common functions and structures

Time function prototype (in time.h):


time_t time (time_t *calptr);

Parameters:

A pointer to the time_t type variable.

return value:

The time_t type corresponds to the number of seconds (the current time of the system) that a long,time uses to take the epoch to the present year, and the epoch begins January 1, 1970. The time that is taken is present in the variable to which the pointer is pointing.

localtime function prototype (in time.h):


struct TM *localtime (const time_t *calptr);

Parameters:

A pointer to the time_t type variable.

return value:

Pointer type that points to the TM struct.

The effect is to convert the value of time_t to a TM structure. You can then print the output.

TM structure (in time.h):

/* Used by other time functions.  */struct tm{  int tm_sec;/* seconds.[ 0-60] (1 leap second) */  int tm_min;/* minutes.[ 0-59] */  int tm_hour;/* hours.[ 0-23] */  int tm_mday;/* day.[ 1-31] */  int tm_mon;/* month.[ 0-11] */  int tm_year;/* Year-1900.  */  int tm_wday;/* Day of week. [0-6] */  int tm_yday;/* days in year.[ 0-365]*/  int tm_isdst;/* dst.[ -1/0/1]*/#ifdef__USE_BSD  Long int tm_gmtoff;/* Seconds east of UTC.  */  __const char *tm_zone;/* Timezone abbreviation.  */#else  long int __tm_gmtoff;/* Seconds east of UTC.  */  __const char *__tm_zone;/* Timezone abbreviation.  */#endif};

Ftime function Prototype (timeb.h):


int ftime (struct timeb *tp);

Parameters:

A pointer to a TIMEB struct variable.

return value:

The current system time is stored in the TIMEB structure, including seconds and milliseconds. The function is to get the current time exactly to milliseconds.

Timeb Structural Body (sys/timeb.h):

/* Structure returned by the ' ftime ' function.  */struct timeb  {    time_t time;/* Seconds since epoch, as from ' time '.  */    unsigned short int millitm;/* Additional milliseconds.  */short    int timezone;/* Minutes west of GMT.  */short    int dstflag;/* nonzero if daylight Savings time used.  */  };

Times function Prototype:


clock_t times (struct TMS *buf);

Parameters:

A pointer to the TMS struct-body variable.

return value:

Clock_t is equivalent to the long type. Used to get the CPU time when the process is running.

TMS struct (in sys/times.h):

/* Structure describing CPU time used by a process and its children.  */struct TMS  {    clock_t tms_utime;/* User CPU time.  */    clock_t tms_stime;/* System CPU time.  */    clock_t tms_cutime;/* User CPU time of dead children.  */    clock_t tms_cstime;/* System CPU time of dead children.  */  };
#include <stdio.h> #include <time.h> #include <sys/time.h> #include <sys/times.h> #include <  Sys/timeb.h> #include <unistd.h>int main (void) {int i = 0;  int sum = 0;  Long TCK = 0;  Long lbegintime = 0;    Long lendtime = 0;  time_t Curr;  struct TM * TTM;  struct TMS Ttms;    struct TIMEB ttimeb;    Tzset ();  Time function gets the number of seconds time (&curr);    printf ("Current time is%ld seconds\n", Curr);  localtime function Conversion time_t TTM = localtime (&curr); printf ("%4d-%02d-%02d%02d:%02d:%02d\n", Ttm->tm_year + 1900, Ttm->tm_mon + 1, ttm->tm_mday, TTM->tm_hour, t    Tm->tm_min, ttm->tm_sec);  Ftime function Acquisition time includes milliseconds Ftime (&AMP;TTIMEB);  TTM = LocalTime (&ttimeb.time); printf ("%4d-%02d-%02d%02d:%02d:%02d:%3d\n", Ttm->tm_year + 1900, Ttm->tm_mon + 1, ttm->tm_mday, TTM->tm_ho    ur, ttm->tm_min, ttm->tm_sec, TTIMEB.MILLITM);  Use the Times function to calculate the time spent in the following loop operation Lbegintime = &ttms;  printf ("Lbegintime =%ld\n", lbegintime); while (1) {    i = i + 1;  if (i = = 0) break;  } Lendtime = times (&AMP;TTMS);  printf ("Lendtime =%ld\n", lendtime);  printf ("CPU time used for cycle:%ld\n", lendtime-lbegintime);    TCK = sysconf (_SC_CLK_TCK);//Get the system clock (how many in 1 seconds) printf ("Convert to Seconds:%f\n", ((lendtime-lbegintime)/(double) TCK)); return 0;}

Execution Result:


[Email protected] ~]#./test10
Current time is 1421644980 seconds
2015-01-19 00:23:00
2015-01-19 00:23:00:781
Lbegintime = 708268851
Lendtime = 708270107
The CPU time used for the cycle is: 1256
Convert to seconds: 12.560000

      • This article is from: Linux Learning Tutorial Network

Linux Time function

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.