time processing in LInux (C language)

In Linux system applications, it is often necessary to obtain the current time information. The Linux kernel provides some corresponding functions for operation. The standard calling format is described as follows:

#include <time.h>

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);

struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);
struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);

time_t mktime(struct tm *tm);

int gettimeofday(struct timeval *tv, struct timezone *tz);
int gettimeofday(const struct timeval *tv, const struct timezone *tz);

The relationship description of each time function is shown in Figure 1, and its detailed description is as follows:

                          

Figure 1 Interrelationship of time functions in Linux

  • asctime function: Convert the information in the tm structure pointed to by the parameter timeptr into the time and date representation method used in the real world, and then return the structure as a string. This function has been converted from time zone to local time, the string format is: "Wed JUN 30 21:49:08 1990/n". The parameter of this function is the storage space pointed to by the tm pointer, and the return value is a string representing the current local time date.
  • asctime_r function: It is an extension of the asctime function, which provides a buffer device buf for storing the return value. The length of the buffer cannot be less than 26 bytes.
  • ctime function: Convert the information in the time_t structure pointed to by the parameter timep into the time and date representation method used in the real world, and then return the result in the form of a string. This function has been converted from time zone to local time, the string format is "Wed Oct 12 20:23:10 1997/n". If the related time and date functions are called again, this string may be broken.
  • // If the related time and date functions are called again, this string may be broken.
    // The difference between this function and the ctime function is that the parameters passed in are different structures
    // The tm time information structure involved in the asctime function is explained as follows.
    
    struct tm
    {
        int tm_sec;    // Second
        int tm_min;    // minute
        int tm_hour;   // Hour
        int tm_mday    // date
        int tm_mon;    // month
        int tm_year;   // years
        int tm_wday;   // Week
        int tm_yady;   // Numbering from January 1 to today's date
        int tm_isdst;
    
        // Daylight saving time identifier, when daylight saving time is in effect, tm_isdat is positive
        // When daylight saving time is not implemented, tm_isdst is 0
        // When not aware of the situation, tm_isdat() is negative
    };

     

  • char *ctime_r function: the same function as the ctime function, but also provides a buffer for storing the return value.
  • gmtime function: Convert the information in the pointed time_t structure into the time and date representation method used in the real world to store the return value.
  • gmtime_r function: similar to the gmtime function, but also provides a memory space pointed to by the result pointer
  • localtime function: the current local time and date, which converts the information in the structure pointed to by the parameter timep into the time and date representation method used in the real world, and then returns.
  • localtime_r function: Similar to the localtime function, it also provides a memory space pointed to by the result pointer for storing the return value.
  • mktime function: Convert the structure data pointed to by the parameter tm to the number of seconds elapsed since January 1, 1970, 0:0:0:0, and return.
  • gettimeofday function: Get the current time and time zone information. This requires superuser permissions. The tz parameter is used to point to the buffer that stores the returned time information. Its structure is explained as follows:
  • Struct timeval {
        time_t      tv_sec;  // Second
        suseconds_t tv_used; // subtle
    }

    Precautions:

// tz is used to store the corresponding clock information, as follows

struct timezone {
    int tz_minuteswest;    // minutes west of Greenwich
    int tz_destime;        // type of DST correction

}
  • settimeofday function: used to set the current time and time zone information, its parameters and usage can refer to gettimeofday.

 

Example 1: Print the current Linux system time information

This is an application example of the system time function, which calls the time, localtime and asctime functions to print the current Linux system time on the screen. The application code first calls the gmtime function to obtain the second data of the current time, and then uses the asctime function to convert the second data to the normal display format:

// Print the current clock of the system

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

int main(void){
	time_t timetemp; // define a time structure variable
	char *wday[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
	struct tm *p;	 // structure pointer
	time(&timetemp); // get time parameter

	printf("%s",asctime(gmtime(&timetemp)));
	
	p = localtime(&timetemp);

	printf("%d:%d:%d\n", (1900+p->tm_year),(1+p->tm_mon),p->tm_mday);

	printf("%s  %d:%d:%d\n", wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);

	return 0;
}

The result of the operation is (running on Ubuntu, version number: 1804):

Tags: Linux

Posted by w4designs on Sat, 14 May 2022 14:16:42 +0300