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结构为:

 1   struct tm {
 2                int tm_sec;         /* seconds */
 3                int tm_min;         /* minutes */
 4                int tm_hour;        /* hours */
 5                int tm_mday;        /* day of the month */
 6                int tm_mon;         /* month */
 7                int tm_year;        /* year */
 8                int tm_wday;        /* day of the week */
 9                int tm_yday;        /* day in the year */
10                int tm_isdst;       /* daylight saving time */
11            };

需要注意的几个点:

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

  2)tm_wday是星期,tm_yday是

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

测试程序:

 1 #include <stdio.h>
 2 #include <time.h>
 3 
 4 int main(int argc, const char *argv[])
 5 {
 6     time_t sec;
 7     struct tm *p_tm = NULL;
 8 
 9     time(&sec);
10     p_tm = localtime(&sec);
11     if (p_tm == NULL) {
12         return 0;
13     }
14 
15     printf("%d-%d-%d %d:%d:%d\n",
16             p_tm->tm_year + 1900,
17             p_tm->tm_mon + 1,
18             p_tm->tm_mday,
19             p_tm->tm_hour,
20             p_tm->tm_min,
21             p_tm->tm_sec);
22     printf("tm_wday = %d\n", p_tm->tm_wday);
23     printf("tm_yday = %d\n", p_tm->tm_yday);
24     printf("tm_isdst = %d\n", p_tm->tm_isdst);
25 
26     return 0;
27 }

执行结果:

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。

测试程序:

 1 #include <stdio.h>
 2 #include <time.h>
 3 
 4 int main(int argc, const char *argv[])
 5 {
 6     time_t sec;
 7     char *p_str = NULL;
 8 
 9     time(&sec);
10     p_str = ctime(&sec);
11     if (p_str == NULL) {
12         return 0;
13     }
14 
15     printf("%s\n", p_str);
16 
17     return 0;
18 }

asctime

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

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

参数:

  tm:日历时间地址

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

测试程序:

 1 #include <stdio.h>
 2 #include <time.h>
 3 
 4 int main(int argc, const char *argv[])
 5 {
 6     time_t sec;
 7     struct tm *p_tm = NULL;
 8     char *p_str = NULL;
 9 
10     time(&sec);
11     p_tm = localtime(&sec);
12     if (p_tm == NULL) {
13         return 0;
14     }
15     
16     p_str = asctime(p_tm);
17     if (p_str == NULL) {
18         return 0;
19     }
20 
21     printf("%s\n", p_str);
22 
23     return 0;
24 }

 mktime

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

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

参数:

  tm:日历时间

返回:秒数

小结

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

 

最后提一下,有些函数直接返回一个指针,这种方法不常用,觉得这个指针可能是函数内部动态申请得到的,想着最后要不要用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权限。