做电商网站一般需要什么流程,吉林 网站备案 照相,制作网站 个人,文章wordpress一、时间类型
linux中编程通常需要用到时间变量#xff0c;和相关的时间操作函数。常用的时间类型有#xff1a;
time_t 、struct timeval、struct timespec、struct tm。
在用到相关的类型和函数时#xff0c;需要加上头文件#xff1a;#include time.h
…一、时间类型
linux中编程通常需要用到时间变量和相关的时间操作函数。常用的时间类型有
time_t 、struct timeval、struct timespec、struct tm。
在用到相关的类型和函数时需要加上头文件#include time.h
time_t: 存储从1970年到现在经过了多少秒。格式为long int。UTC时间。struct timeval提供秒和微秒单位最高精度是微秒。
/* A time value that is accurate to the nearestmicrosecond but also has a range of years. */
struct timeval{__time_t tv_sec; /* Seconds. */__suseconds_t tv_usec; /* Microseconds. */};
# endif /* struct timeval */
struct timespec提供秒和纳秒单位最高精度是纳秒
struct timespec{__time_t tv_sec; /* Seconds. */__syscall_slong_t tv_nsec; /* Nanoseconds. */};
struct tm详细时间的结构体
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_MISClong int tm_gmtoff; /* Seconds east of UTC. */const char *tm_zone; /* Timezone abbreviation. */
# elselong int __tm_gmtoff; /* Seconds east of UTC. */const char *__tm_zone; /* Timezone abbreviation. */
# endif
};
二、时间函数
char *asctime(const struct tm* timeptr);
//将结构中的信息转换为真实世界的UTC时间以字符串的形式显示char *ctime(const time_t *timep);
//将timep转换为真是世界的时间以字符串显示它和asctime不同就在于传入的参数形式不同double difftime(time_t time1, time_t time2);
//返回两个时间相差的秒数int gettimeofday(struct timeval *tv, struct timezone *tz);
//返回当前距离1970年的秒数和微秒数后面的tz是时区一般不用struct tm* gmtime(const time_t *timep);
//将time_t表示的时间转换为没有经过时区转换的UTC时间是一个struct tm结构指针stuct tm* localtime(const time_t *timep);
//和gmtime类似但是它是经过时区转换的时间。time_t mktime(struct tm* timeptr);
//将struct tm 结构的时间转换为从1970年至今的秒数time_t time(time_t *t);
//取得从1970年1月1日至今的秒数
//eg,totalsec time(NULL) or time(totalsec)int clock_gettime(clockid_t clk_id, struct timespec* tp);
/*可以根据需要获取不同要求的精确时间
参数
clk_id : 检索和设置的clk_id指定的时钟时间。
CLOCK_REALTIME:系统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时,
中间时刻如果系统时间被用户改成其他,则对应的时间相应改变
CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响
CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间
CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间*///eg. 获得从开机到现在的时间clock_gettime(CLOCK_MONOTONIC,tspec);三、示例
1、设置系统时间
struct timeval tv;
tv.tv_sec (time_t)gps_utctime_sec;
tv.tv_usec 0;
if(settimeofday (tv, NULL) 0){ MSG([ GPS ]:Set system datatime error,may need root permissions!\n);
}
else{MSG([ GPS ]:GPS valid,update system time!\n);
2、读取当前时间tick
/*! get the current system time tick, second unit */
static uint32_t getCurrSysTick(void)
{time_t timep;time (timep);return timep;//seconds from 1970-1-1:0:0:0
}
3、读取当前时间字符串Fri Dec 20 14:44:18 2019
static char* getCurrSysTime(void)
{time_t timep;time (timep);return asctime(localtime(timep));
}
4、设置延时
static void delay_ms(unsigned int dms)
{struct timespec sleeper, dummy ;sleeper.tv_sec (time_t)(dms / 1000) ;sleeper.tv_nsec (long)(dms % 1000) * 1000000 ;nanosleep (sleeper, dummy);//delay 1ms
}
5、获取当前时间tm
struct tm* t_tm;
t_tm localtime((time_t *)meas_gps_utctime.tv_sec);
/* test gps */
MSG(today is %4d-%02d-%02d %02d:%02d:%02d\n,t_tm-tm_year1900,t_tm-tm_mon1,t_tm-tm_mday,t_tm-tm_hour,t_tm-tm_min,t_tm-tm_sec); Linux时间转换图 参考
c 时间类型详解 time_tlinux应用time和timezonelinux下的clock_gettime()获取时间函数