linux实时时钟相关函数

时间:2023-03-10 03:37:39
linux实时时钟相关函数

time

功能:获取1970年1月1日00:00:00到现在的秒数

原型:time_t time(time_t *t);

参数:

  t:获取到的秒数

返回:获取到的秒数

说明:在time.h中定义了time_t类型就是long类型。通过形参或返回值获取到的值是一样的。

localtime

功能:将1970年1月1日00:00:00到现在的秒数转换为当地日历时间

原型:struct tm *localtime(const time_t *timep);

参数:

  timep:保存秒数的地址

返回:成功返回日历时间结构体指针,失败返回NULL。

struct tm结构为:

   struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};

需要注意的几个点:

  1)tm_year是从年开始,tm_mon是从0开始。

  2)tm_wday是星期,tm_yday是

  3)tm_isdst是指示夏令时在所述时间是否有效的标志。如果夏令时有效,则该值为正值;如果夏令时无效,则该值为零;如果信息不可用,则该值为负值。

测试程序:

 #include <stdio.h>
#include <time.h> int main(int argc, const char *argv[])
{
time_t sec;
struct tm *p_tm = NULL; time(&sec);
p_tm = localtime(&sec);
if (p_tm == NULL) {
return ;
} printf("%d-%d-%d %d:%d:%d\n",
p_tm->tm_year + ,
p_tm->tm_mon + ,
p_tm->tm_mday,
p_tm->tm_hour,
p_tm->tm_min,
p_tm->tm_sec);
printf("tm_wday = %d\n", p_tm->tm_wday);
printf("tm_yday = %d\n", p_tm->tm_yday);
printf("tm_isdst = %d\n", p_tm->tm_isdst); return ;
}

执行结果:

linux实时时钟相关函数

gmtime

gmtime与localtime非常相似,区别就是localtime得到的是当地时间,而gmtime得到的是0时区的时间。

功能:功能:将1970年1月1日00:00:00到现在的秒数转换为0时区的日历时间

原型:struct tm *gmtime(const time_t *timep);

参数:

  timep:保存秒数的地址

返回:成功返回日历时间,失败返回NULL。

ctime

功能:将时间秒数转化为字符串

原型:char *ctime(const time_t *timep);

参数:

  timep:保存秒数的地址

返回:成功返回获得时间的字符串首地址,失败返回NULL。

测试程序:

 #include <stdio.h>
#include <time.h> int main(int argc, const char *argv[])
{
time_t sec;
char *p_str = NULL; time(&sec);
p_str = ctime(&sec);
if (p_str == NULL) {
return ;
} printf("%s\n", p_str); return ;
}

asctime

功能:将日历时间结构体转换为字符串时间

原型:char *asctime(const struct tm *tm);

参数:

  tm:日历时间地址

返回:成功字符串时间地址,失败返回NULL。

测试程序:

 #include <stdio.h>
#include <time.h> int main(int argc, const char *argv[])
{
time_t sec;
struct tm *p_tm = NULL;
char *p_str = NULL; time(&sec);
p_tm = localtime(&sec);
if (p_tm == NULL) {
return ;
} p_str = asctime(p_tm);
if (p_str == NULL) {
return ;
} printf("%s\n", p_str); return ;
}

mktime

功能:将日历时间转换为1970年1月1日00:00:00到现在的秒数

原型:time_t mktime(struct tm *tm);

参数:

  tm:日历时间

返回:秒数

小结

各种时间的转换关系如下图所示:

linux实时时钟相关函数

最后提一下,有些函数直接返回一个指针,这种方法不常用,觉得这个指针可能是函数内部动态申请得到的,想着最后要不要用free释放,但是用valgrind检查发现并没有内存泄漏,hhh,具体他怎么实现的就不知道了。

gettimeofday

功能:获取当前时间和时区信息

原型:int gettimeofday(struct timeval *tv, struct timezone *tz);

参数:

  tv:时间信息

  tz:时区信息

  若传入某个参数为NULL表明对该项不感兴趣

返回:成功返回0,失败返回-1

timeval 结构如下:

struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};

其中tv_sec是1970年1月1日00:00:00到现在的秒数,tv_usec则是微秒数。

timezone 结构如下:

struct timezone {
int tz_minuteswest; /* minutes west of Greenwich */
int tz_dsttime; /* type of DST correction */
};

其中tz_minuteswest指的是与Greenwich时间差了多少分钟,tz_dsttime在linux中不使用。

注意使用该函数需要包含头文件sys/time.h

settimeofday

功能:设置当前时间和时区信息

原型:int settimeofday(const struct timeval *tv, const struct timezone *tz);

参数:

  tv:时间信息

  tz:时区信息

返回:成功执行时,返回0。失败返回-1,errno被设为以下的某个值
  EFAULT:tv或tz其中某一项指向的空间不可访问
  EINVAL:时区格式无效
  EPERM:权限不足,调用进程不允许使用settimeofday设置当前时间和时区值

执行settimeofday函数的进程要有root权限。