In the C language, there are two functions that get the current time: LocalTime and Getlocaltime. They are widely used in procedures such as reconciliation.
In this paper, localtime and getlocaltime are described in detail, and their usage is illustrated with examples.
1. Foreword
In C, the operation to get the current system time occurs. For example, in the reconciliation procedure, you need to add the date and time of the day to the reconciliation file name. Generally, there are two functions that are frequently used to get the current time: LocalTime and Getlocaltime.
The localtime and Getlocaltime functions are introduced in this paper, and their usage is illustrated with examples.
2. Introduction to LocalTime and Getlocaltime functions
Introduction to 2.1 localtime function
Function: Converts the number of seconds from 1970-1-1:00 (which starts in the program from 1900) to the current time system to the calendar time.
Description: The time of the TM structure obtained by this function has been transformed by time zone and become local time.
Usage: struct TM *localtime (const time_t *clock).
Return value: Returns a pointer to the TM structure body.
A TM structure is a structure defined in time.h for each amount of time, such as year, month, and so on, defined below:
#ifndef _tm_defined
struct TM
{
int tm_sec;//* sec-The value interval is [0,59]/
int tm_min;/* divided-range is [0,59]/
int Tm_hour; /* Time-value range is [0,23]/
int tm_mday;/* One month date-value range is [1,31]/
int tm_mon;/* Month (starting from January, 0 for January)-value range is [0,11]/
int Tm_year; /* Year, the value starting from 1900 *
/int tm_wday; * Week – The value range is [0,6], 0 for Sunday, 1 for Monday, and so on
/int tm_yday; * The number of days from January 1 of each year – the value range is [ 0,365], of which 0 represents January 1, 1 for January 2, and so on *
/int tm_isdst;/* Daylight Saving time identifier, when the implementation of daylight savings, TM_ISDST is positive, not the daylight saving times, TM_ISDST 0; When you don't know the situation, tm_ ISDST () is a negative/
long int tm_gmtoff/* Specifies the number of UTC East time zone positive seconds in Dateline Eastern time zone or the negative seconds of UTC West time zone/
const char *tm_zone; \ * Name of the current time zone ( Related to the environment variable TZ) * *
/};
#define _tm_defined
#endif
Introduction to 2.2 getlocaltime function
function function: This function is used to obtain the local current system date and time.
Function prototype: void Getlocaltime (Lpsystemtime lpsystemtime/address of the system times structure);
Parameter description:
Lpsystemtime: A variable that points to a user-defined type of systemtime containing date and time information that holds the time information obtained by the function.
The SYSTEMTIME structure is defined as follows:
typedef struct _SYSTEMTIME
{
WORD wyear; Years
WORD wmonth; Month
WORD Wdayofweek; Week, 0 for Sunday, 1 for Monday, 2 for Tuesday
... WORD wday; Daily
WORD Whour; When
WORD Wminute; Divide
WORD Wsecond; Seconds
WORD wmilliseconds; Millisecond
}systemtime,*psystemtime;