Linux C programming: get the current system time

Linux C programming: get the current system time

   Obtaining the current system time is very useful, for example, in software debugging, or software operation needs to obtain the current time for the compilation of run logs. The functions and data types related to the system time in Linux are defined in the header file <time.h> of the system. The following functions are mainly used when writing programs to obtain the system time:

Function name Annotation
time() The result returned by this function is the number of seconds from 0:00:00 on January 1, 1970 (usually) to the current time.
localtime() This function can convert the time_t type data returned by time() into the real time of the current system, and return the conversion result.
strftime() This function is used to format the local time/date according to the locale, or format a time string

The meanings of the relevant parameters involved    in using the strftime time formatting function are as follows:

parameter meaning
%F Format the time as year-month-day
%T Format the time to display hours, minutes and seconds: hh:mm:ss
%Y Format the time as a decimal year with century part
%m Format the time as a month in decimal
%d The day of the month formatting the time as a decimal
%H Format time as hour in 24-hour clock
%M Format the time as a decimal number of minutes
%S Format the time as a decimal number of seconds

For more parameter analysis, please refer to the introduction of strftime .

   Definition of data types related to time variables:

type of data meaning
time_t In C, time_t represents a long integer data used to represent the number of seconds from 0:00:00 on January 1, 1970 (usually) to the current time

  Let us look for the relevant definition in the source file of the header file. First, look at where the <time.h> header file is, and use the whereis command to search for the file:

whereis time.h

   The result is as follows:
Where is the time
   In the header file, the structure tm contains basic information about time such as year, month, day, hour, minute, and second.

/* 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_MISC
  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
};

  The actual application of related functions for programming, to achieve the current system time in Linux, and output in the format of YYYY-MM-dd hh:mm:ss .
The experiment started

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

char* getNowtime(void) 
{
    
    
	static char s[30]={
    
    0};
    char YMD[15] = {
    
    0};
    char HMS[10] = {
    
    0};
    time_t current_time;
    struct tm* now_time;

    char *cur_time = (char *)malloc(21*sizeof(char));
    time(&current_time);
    now_time = localtime(&current_time);

    strftime(YMD, sizeof(YMD), "%F ", now_time);
    strftime(HMS, sizeof(HMS), "%T", now_time);
    
    strncat(cur_time, YMD, 11);
    strncat(cur_time, HMS, 8);

    printf("\nCurrent time: %s\n\n", cur_time);
	memcpy(s, cur_time, strlen(cur_time)+1);
    free(cur_time);

    cur_time = NULL;

    return s;
}


int main(void)
{
    
    
	getNowtime();

	return 0;
}
  

The running result after compilation is shown in the figure below:
operation hours

Guess you like

Origin blog.csdn.net/qq_33475105/article/details/106279572