Linux에는 두 번째 수준의 시차를 얻을 수 있는 time() 함수, 마이크로초 수준의 시차를 얻을 수 있는 gettimeofday() 및 settimeofday() 함수, clock_gettime()과 같은 시간을 계산하는 함수가 있습니다. ) 나노초 수준의 시차 등을 얻을 수 있습니다.

이 튜토리얼의 운영 환경: linux7.3 시스템, Dell G3 컴퓨터.
다음은 Linux의 시간 관련 함수와 시간 변환 관련 함수를 설명합니다
1. 시간 관련 함수 가져오기
1.1 2차 시차 함수 가져오기
#include <time.h> time_t time(time_t *timer);//通过函数返回值或者timer 变量均可以获取到当前时间
time_t는 실제로 긴 정수형, UTC 시간(리눅스 시스템의 에포크 시간인 1970년 1월 1일 0:00:00)과 현재 시스템 시간까지의 초 수준의 시차를 나타냅니다.
1.2 마이크로초 수준의 시차를 구합니다. function
#include <sys/time.h>
#include <unistd.h>
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
struct timezone{
int tz_minuteswest; /*miniutes west of Greenwich*/
int tz_dsttime; /*type of DST correction*/
};
//函数执行成功返回0,失败返回-1. 其中timezone 是时区相关的结构体
int gettimeofday(struct timeval *tv, struct timezone *tz);
//用来设置制定的时间和时区信息
int settimeofday(const struct timeval *tv, const struct timezone *gz);1.3 나노초 시차 함수 가져오기
#include <time.h>
/*
其中clk_id 用来制定对应的时钟类型,不同的类型可以用来获取不同的时间值,具体有四种:
CLOCK_REALTIME: 系统实时时间,从UTC开始计时,若时间被用户更改计数时间相应改变;
CLOCK_MONOTONIC: 从系统启动开始计时,即使用户更改时间也没有影响;
CLOCK_PROCESS_CPUTIME_ID: 本进程开始到执行到当前程序系统CPU花费的时间;
CLOCK_THREAD_CPUTIME_ID: 本线程开始到执行到当前程序系统CPU花费的时间
*/
struct timespec{
time_t tv_sec; //s
long tv_nsec; //ns
};
int clock_gettime(clockid_t clk_id, struct timespec* tp);2. 시간 관련 함수 변환
2.1 위의 시간 획득 함수에서 얻은 시간 매개변수 time_t를 구조체로 변환
연도를 포함하는 struct tm 월, 일 등과 같은 매우 상세한 필드 아래와 같이:
#include <time.h>
struct tm{
int tm_sec; //秒
int tm_min; //分
int tm_hour; //时;取值区间为[0, 23]
int tm_mday; //日;取值区间为[1, 31]
int tm_mon; //月份;取值区间为[0, 11]; 0表示1月份依次递增到12月份
int tm_year; //年份;其值为1900年至今年数
int tm_wday; //星期;0代表星期天,1代表星期1,以此类推
int tm_yday; //日期;0代表1月1日
int tm_isdst; //夏令时标识符;使用夏令时为正,不使用t为0,不确定时为负*/
};time_t를 struct tm 구조로 변환하는 데 일반적으로 사용되는 함수는 다음과 같습니다.
#include <time.h> struct tm* gmtime(const time_t* timep); struct tm*localtime(const time_t* timep);
gmtime() 및 localtime() 함수는 time_t 데이터 형식을 tm 형식 유형으로 변환할 수 있습니다. 차이점은 gmtime()으로 변환한 결과는 GMT(중앙 시간대)에 해당하는 정보인 반면, localtime() 함수로 변환한 결과는 현재 시간대에 대한 정보라는 점입니다.
2.2 time_t를 우리가 습관적으로 사용하는 시간과 날짜 문자열로 변환
해당 변환 함수는 다음과 같습니다.
#include <time.h> char* ctime(time_t* timep);
2.3 struct tm을 time_t로 변환
해당 함수는 다음과 같습니다.
#include <time.h> time_t mktime(struct tm *p_tm);
2.4 struct tm을 우리가 습관적으로 사용하는 시간 및 날짜 문자열로 변환
해당 함수는 다음과 같습니다.
#include <time.h> char *asctime(const struct tm *p_tm); //习惯性字符串 Thu Dec 9 07:13:35 2021
2.5 시간 문자열을 struct tm 형식으로 변환
/************************************** ** description: 将struct tm 按照指定的format格式转化成字符串 ** parameter: ** *s : 需要被转换的时间字符串 ** *format:时间字符串的格式 ** *tm:转换后的tm时间 **************************************/ char *strptime(const char *s, const char *format, struct tm *tm);
2.6 struct tm을 지정된 형식 형식에 따라 변환 String
/************************************** ** description: 将struct tm 按照指定的format格式转化成字符串 ** parameter: ** *s : 生成的时间字符串 ** max: 字符串最大字符数(即最大可生成的字符数量) ** *format:生成的字符串格式 ** *tm:需要被转换的tm时间 **************************************/ size_t strftime(char *s, size_t max, const char *format,const struct tm *tm);
3.Example
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
int main(int argc, char *argv[])
{
char *pstr = NULL;
struct tm *ptm = NULL;
printf("************** 使用ctime获取时间time_t **************\n");
time_t times = 0;
time(×);
printf("time_t:%ld\n\n", times);
printf("************** 使用ctime转换time_t成我们习惯性使用的时间和日期字符串 **************\n");
pstr = ctime(×);
printf("ctime:%s\n", pstr);
printf("************** 使用gmtime转换time_t成struct tm 时间和日期**************\n");
ptm = gmtime(×);
printf("time : %d:%d:%d\n", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
printf("date: %d:%d:%d\n", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday);
printf("year: wday:%d yday:%d isdst:%d\n\n", ptm->tm_wday, ptm->tm_yday, ptm->tm_isdst);
printf("************** 使用asctime转换struct tm成我们习惯性使用的时间和日期字符串**************\n");
pstr = asctime(ptm);
printf("asctime:%s\n\n", pstr);
printf("************** 使用localtime转换time_t成struct tm 时间和日期**************\n");
ptm = localtime(×);
printf("time : %d:%d:%d\n", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
printf("date: %d:%d:%d\n", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday);
printf("year: wday:%d yday:%d isdst:%d\n", ptm->tm_wday, ptm->tm_yday, ptm->tm_isdst);
pstr = asctime(ptm);
printf("asctime:%s\n\n", pstr);
printf("************** 使用gettimeofday获取微秒级的时间**************\n");
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
ptm = localtime(&tv.tv_sec);
printf("time : %d:%d:%d\n", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
printf("date: %d:%d:%d\n", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday);
printf("year: wday:%d yday:%d isdst:%d\n", ptm->tm_wday, ptm->tm_yday, ptm->tm_isdst);
printf("tv_usec:%ld\n\n", tv.tv_usec);
printf("************** 使用clock_gettime获取纳秒级的时间**************\n");
struct timespec tp;
clock_gettime(CLOCK_REALTIME, &tp);
ptm = localtime(&tp.tv_sec);
printf("time : %d:%d:%d\n", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
printf("date: %d:%d:%d\n", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday);
printf("year: wday:%d yday:%d isdst:%d\n", ptm->tm_wday, ptm->tm_yday, ptm->tm_isdst);
printf("tp.tv_nsec:%ld\n\n", tp.tv_nsec);
return 0;
}특정 시간 문자 변환
int str_to_time(void)
{
char pstr[128] = {0};
struct tm t;
strptime("2021-04-23 12:34:56", "%Y-%m-%d %H:%M:%S", &t);
printf("**** tm_isdst: %d, tm_yday:%d, tm_wday%d,\n %d-%d-%d \n %d:%d:%d\n",
t.tm_isdst, t.tm_yday, t.tm_wday, t.tm_year+1900, t.tm_mon+1, t.tm_mday,
t.tm_hour, t.tm_min, t.tm_sec);
printf("mktime ts:%ld\n", mktime(&t));
printf("asctime:%s\n", asctime(&t));
strftime(pstr, sizeof(pstr), "%Y-%m-%d %H:%M:%S", &t);
printf("pstr:%s\n", pstr);
}
int time_to_str(void)
{
char pstr[128] = {0};
struct tm t = {
.tm_sec = 56, /* Seconds (0-60) */
.tm_min = 34, /* Minutes (0-59) */
.tm_hour = 12, /* Hours (0-23) */
.tm_mday = 23, /* Day of the month (1-31) */
.tm_mon = 4-1, /* Month (0-11) */
.tm_year = 2021-1900, /* Year - 1900 */
.tm_wday = 5, /* Day of the week (0-6, Sunday = 0) */
.tm_yday = 113, /* Day in the year (0-365, 1 Jan = 0) */
.tm_isdst = 0, /* Daylight saving time */
};
strftime(pstr, sizeof(pstr), "%Y-%m-%d %H:%M:%S", &t);
printf("pstr:%s\n", pstr);
}시스템 시작 시 시간을 알고 싶다면 사용자가 시간을 변경해도 영향을 받지 않습니다. , 예:
unsigned long long clock_systick_get(void)
{
int ret = -1;
unsigned long long time;
int cnt = 0;
struct timespec now = {0, 0};
while (ret < 0 && cnt < 3)
{
ret = clock_gettime(CLOCK_MONOTONIC, &now); //获取失败重试,最大执行3次
cnt++;
}
time = now.tv_sec * 1000 + now.tv_nsec / (1000000);
return time;
}관련 추천: "Linux 비디오 튜토리얼"
위 내용은 Linux에는 시간을 계산하는 기능이 있습니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!