linux 获取当前时间的相关函数

几个关于时间的概念

Coordinated Universal Time(UTC)

协调世界时,又称为世界标准时间,通常也指的是格林威治标准时间(Greenwich Mean Time,GMT)。地球被分为24个时区,相邻时区相差一个小时。比如中国上海是+8区,意思就是,假如格林威治现在是27日1点0分0秒,而中国上海就是27日早上9点0分0秒。中国内地的时间与UTC的时差为+8,也就是UTC+8,美国则是UTC-5。

Calendar Time

日历时间,是用“从一个标准时间点到此时的时间经过的秒数”来表示的时间。


linux 时间函数

  • time / time_t:秒
  • gettimeofday / struct timeval:微妙
  • clock_gettime / struct timespec:纳秒
  • tdstc :纳秒
time_t 和 time 函数
#include <time.h>

typedef long  time_t;   // 一般用来记录从1970年1月1日0时0分0秒到现在时刻的秒数

time_t time(time_t *calptr)                 
  函数返回值:时间戳(秒数)
                                                     
  如果calptr非空,那么时间值也会写入到calptr所指的内存中
timeval 和 gettimeofday 函数
#include <time.h>

struct timeval {
    time_t tv_sec;         // 秒
    suseconds_t tv_usec    // 微妙
}
  
#include <sys/time.h>

int gettimeofday(struct timeval *restrict tp,  void *restrict tzp);
  函数返回值:总是返回0

  tzp唯一合法参数是NULL

gettimeofday返回距1970年1月1日0时0分0秒的时间,将其存放于timeval(微秒)结构中。gettimeofday 在x86-64上已经不是系统调用,相比于clock_gettime调用开销小,而相比于time精度高,适合推荐。

timespec 和 clock_gettime 函数
#include <time.h>

struct timespec {
    time_t tv_sec;       // 秒
    long int tv_nsec     // 纳秒
}
运用
#include <sys/time.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>

time_t get_now_time_sec()
{
    time_t t;
    time(&t);
    return t;
}

unsigned long long get_now_time_usec()
{
    struct timeval val;
    val.tv_sec = 0;
    val.tv_usec = 0;

    gettimeofday(&val, NULL);
    return val.tv_sec * 1000000 + val.tv_usec;
}

unsigned long long get_now_time_nsec()
{
    struct timespec spec;
    spec.tv_sec = 0;
    spec.tv_nsec = 0;

    clock_gettime(CLOCK_REALTIME, &spec);
    return spec.tv_sec * 1000000000 + spec.tv_nsec;
}

时间格式化

struct tm
#include <time.h>

struct tm {
    int tm_sec;
    int tm_min;
    int tm_hour;
    int tm_mday;    // day of month  [1 -~ 31] 
    int tm_mon;     // month since January [0 ~ 11]
    int tm_year;    // years since 1900
    int tm_wday;    // days since Sunday  [0 ~ 6]
    int tm_yday;    // days since 1月1日 [0 ~ 265]
    int tm_isdst;
};
gmtime和localtime函数
#include <time.h>

struct tm* gmtime(const time_t *calptr);
    函数返回值:指向分解的tm结构的指针;若出错,返回 NULL
    gmtime 将时间戳转换协调统一时间的tm结构的指针

struct tm* localtime(const time_t *calptr);
    函数返回值:指向分解的tm结构的指针;若出错,返回 NULL
    localtime 将时间戳转换成本地时间的tm结构的指针
    
strftime和strftim_l函数
#include <time.h>

size_t strftime(char *restrict buf, size_t maxsize, 
    const char* restrict format,   const struct tm* restrict tmptr);

size_t strfime_l(char *restrict buf, size_t maxsize, 
    const char* restrict format,   const struct tm *restrict tmptr, 
    locale_t locale); 

    2个函数的返回值:若有空间,返回存入数组的字符数。否则,返回0
常用格式:
格式 说明 实例
%F YYYY-MM-DD 2017-08-29
%T %H:%M:%S 21:24:52
%Y 2018
%m 08
%b 缩写的月名 Jan
%d 月日 30
%a 缩写的周日名 Thu
代码示例:
#include <time.h>
#include <stdio.h>

int main()
{
    time_t t;
    struct tm *tmp,*tmp2;
    char buf[64], buf2[64];

    time(&t);
      
    tmp = localtime(&t);
    strftime(buf, 64, "time and date: %F %T", tmp);
    printf("%s\n", buf);

    tmp2 = gmtime(&t);
    strftime(buf2, 64, "time and date: %F %T", tmp2);
    printf("%s\n", buf2);
}
输出:
time and date: 2018-04-28 22:05:40
time and date: 2018-04-28 14:05:40
#include <stdio.h>
#include <sys/time.h>
#include <time.h>

int main(int argc, char**argv)
{
    struct timeval ti;
    struct tm *tmp;
    char buf[64];

    gettimeofday(&ti, NULL);
    tmp = localtime(&(ti.tv_sec));

    printf("%ld, %ld\n", ti.tv_sec, ti.tv_usec);
    strftime(buf, 64, "time: %F %T", tmp);
    printf("time is now: %s.%ld\n", buf, ti.tv_usec);
}
转换关系
转换关系.jpg

参考资料
1、《UNIX环境高级编程》[美] W. Richard Stevens Stephen A. Rago
2、

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容