Linux时间相关
获取时间
使用date命令
输入date,显示如下
Thu Jul 20 09:47:07 CST 2023
使用函数,相关的函数如下,
#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, char *buf);
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);
#include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz);
//相关结构体定义如下
struct tm {
int tm_sec; /* Seconds (0-60) */
int tm_min; /* Minutes (0-59) */
int tm_hour; /* Hours (0-23) */
int tm_mday; /* Day of the month (1-31) */
int tm_mon; /* Month (0-11) */
int tm_year; /* Year - 1900 */
int tm_wday; /* Day of the week (0-6, Sunday = 0) */
int tm_yday; /* Day in the year (0-365, 1 Jan = 0) */
int tm_isdst; /* Daylight saving time */
};
对于各个成员的说明如下
tm_sec The number of seconds after the minute, normally in the range 0 to 59, but can be up to 60 to allow for leap seconds.
tm_min The number of minutes after the hour, in the range 0 to 59.
tm_hour The number of hours past midnight, in the range 0 to 23.
tm_mday The day of the month, in the range 1 to 31.
tm_mon The number of months since January, in the range 0 to 11.
tm_year The number of years since 1900.
tm_wday The number of days since Sunday, in the range 0 to 6.
tm_yday The number of days since January 1, in the range 0 to 365.
tm_isdst A flag that indicates whether daylight saving time is in effect at the time described. The value is positive if daylight saving time
is in effect, zero if it is not, and negative if the information is not available.
time_t实际类型一般为long, 指的是自 Epoch, 1970-01-01 00:00:00 +0000 (UTC).以来的秒数
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
此结构体表示自 Epoch以来的秒数和毫秒数
struct timezone {
int tz_minuteswest; /* minutes west of Greenwich */
int tz_dsttime; /* type of DST correction */
};
一般把timezone 参数设为NULL
对上面api的测试
void print_tm(struct tm *tm)
{
if(tm){
printf("tm_year = %04d\n", tm->tm_year);
printf("tm_mon = %02d\n", tm->tm_mon);
printf("tm_mday = %02d\n", tm->tm_mday);
printf("tm_hour = %02d\n", tm->tm_hour);
printf("tm_min = %02d\n", tm->tm_min);
printf("tm_sec = %02d\n", tm->tm_sec);
printf("tm_wday = %d\n", tm->tm_wday);
printf("tm_yday = %03d\n", tm->tm_yday);
printf("tm_isdst = %d\n", tm->tm_isdst);
printf("%04d-%02d-%02d %02d:%02d:%02d\n",\
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,\
tm->tm_hour, tm->tm_min, tm->tm_sec);
}else{
printf("NULL tm!\n");
}
}
void print_tv(struct timeval *tv)
{
if(tv){
printf("tv->tv_sec = %ld\n", tv->tv_sec);
printf("tv->tv_usec = %ld\n", tv->tv_usec);
}
}
void print_tz(struct timezone *tz)
{
printf("tz->tz_minuteswest = %d\n", tz->tz_minuteswest);
printf("tz->tz_dsttime = %d\n", tz->tz_dsttime);
}
void print_tz_info(void)
{
printf("tzname[0] = %s\n", tzname[0]);
printf("tzname[1] = %s\n", tzname[1]);
printf("timezone = %ld\n", timezone);
}
int test_get_time_apis(void)
{
int r=-1;
char buf[100];
char* buf_p=NULL;
time_t time_t_var;
struct tm struct_tm_var;
struct tm* struct_tm_p=NULL;
struct timeval tv;
struct timezone tz;
//time() returns the time as the number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
time_t_var = time(NULL);
printf("time() --> %ld\n",time_t_var);
/*
The gmtime() function converts the calendar time timep to broken-down time representation, expressed in Coordinated
Universal Time (UTC). It may return NULL when the year does not fit into an integer. The return value points to a
statically allocated struct which might be overwritten by subsequent calls to any of the date and time functions. The
gmtime_r() function does the same, but stores the data in a user-supplied struct.
*/
struct_tm_p = gmtime(&time_t_var);
printf("gmtime():\n");
print_tm(struct_tm_p);
printf("\n\n");
struct_tm_p = gmtime_r(&time_t_var,&struct_tm_var);
printf("gmtime_r(): struct_tm_p==&struct_tm_var :%d\n",struct_tm_p == ( &struct_tm_var) );
print_tm(struct_tm_p);
printf("\n\n");
/*
the localtime() function converts the calendar time timep to broken-down time representation, expressed relative to
the user's specified timezone. The function acts as if it called tzset(3) and sets the external variables tzname with
information about the current timezone, timezone with the difference between Coordinated Universal Time (UTC) and
local standard time in seconds, and daylight to a nonzero value if daylight savings time rules apply during some part
of the year. The return value points to a statically allocated struct which might be overwritten by subsequent calls
to any of the date and time functions. The localtime_r() function does the same, but stores the data in a user-sup‐
plied struct. It need not set tzname, timezone, and daylight.
*/
struct_tm_p = localtime(&time_t_var);
printf("localtime():\n");
print_tm(struct_tm_p);
printf("\n\n");
struct_tm_p = localtime_r(&time_t_var,&struct_tm_var);
printf("localtime_r(): struct_tm_p==&struct_tm_var :%d\n",struct_tm_p == ( &struct_tm_var) );
print_tm(struct_tm_p);
printf("\n\n");
/* The asctime() function converts the broken-down time value tm into a null-terminated string with the same format as
ctime(). The return value points to a statically allocated string which might be overwritten by subsequent calls to
any of the date and time functions. The asctime_r() function does the same, but stores the string in a user-supplied
buffer which should have room for at least 26 bytes.
*/
printf("asctime():%s\n",asctime(struct_tm_p));
memset(buf,0,sizeof(buf));
buf_p = asctime_r(struct_tm_p,buf);
printf("asctime_r():%d %s\n",buf==buf_p,buf);
/*
The functions gettimeofday() and settimeofday() can get and set the time as well as a timezone. The tv argument is a
struct timeval (as specified in <sys/time.h>):
*/
r= gettimeofday(&tv ,&tz);
if(r <0) {
printf("gettimeofday failed,:%s\n",strerror(errno));
}
printf("gettimeofday():\n");
print_tv(&tv);
print_tz(&tz);
printf("\n\n");
return 0;
}
测试函数的输出如下
time() --> 1688837481
gmtime():
tm_year = 0123
tm_mon = 06
tm_mday = 08
tm_hour = 17
tm_min = 31
tm_sec = 21
tm_wday = 6
tm_yday = 188
tm_isdst = 0
2023-07-08 17:31:21
gmtime_r(): struct_tm_p==&struct_tm_var :1
tm_year = 0123
tm_mon = 06
tm_mday = 08
tm_hour = 17
tm_min = 31
tm_sec = 21
tm_wday = 6
tm_yday = 188
tm_isdst = 0
2023-07-08 17:31:21
localtime():
tm_year = 0123
tm_mon = 06
tm_mday = 09
tm_hour = 01
tm_min = 31
tm_sec = 21
tm_wday = 0
tm_yday = 189
tm_isdst = 0
2023-07-09 01:31:21
localtime_r(): struct_tm_p==&struct_tm_var :1
tm_year = 0123
tm_mon = 06
tm_mday = 09
tm_hour = 01
tm_min = 31
tm_sec = 21
tm_wday = 0
tm_yday = 189
tm_isdst = 0
2023-07-09 01:31:21
asctime():Sun Jul 9 01:31:21 2023
asctime_r():1 Sun Jul 9 01:31:21 2023
gettimeofday():
tv->tv_sec = 1688837481
tv->tv_usec = 921097
tz->tz_minuteswest = 0
tz->tz_dsttime = 0
设置时间
//设置时间
int test_set_time_apis(void)
{
int r=-1;
struct timeval tv;
time_t secs=0;
printf("before settimeofday\n");
system("date -R");
tv.tv_sec = time(NULL)+60*60;
r= settimeofday(&tv ,NULL);
if(r <0) {
printf("gettimeofday failed,:%s\n",strerror(errno));
}
printf("after settimeofday\n");
system("date -R");
printf("before stime\n");
system("date -R");
secs=3600+time(NULL);
r = stime(&secs);
if(r!=0){
printf("stime failed,%s\n",strerror(errno));
}
printf("after stime\n");
system("date -R");
return 0;
}
test test_set_time_apis
before settimeofday
Thu, 01 Jan 1970 11:27:51 +0800
after settimeofday
Thu, 01 Jan 1970 12:27:51 +0800
before stime
Thu, 01 Jan 1970 12:27:51 +0800
after stime
Thu, 01 Jan 1970 13:27:51 +0800