Linux c time模块函数库

时间模块需要引入time.h头文件

#include <time.h>

1. c获取时间戳

#include <stdio.h>
#include <time.h>

// 格林威治时间戳

void my_time(){
    // 这里最好是long int
    int time1 = 0;
    // 方法1
    time(&time1);
    printf("time1 is :%d 
", time1); // time1 is :1548137793

    // 方法2
    time_t time2 = 0;
    time2 = time(NULL);
    printf("time2 is :%d 
", time2); // time1 is :1548137793

}

int main(){
    my_time();
    return 0;
}

2. c 获得时间字符串,或者将时间戳转换成字符串

#include<stdio.h>
#include<time.h>

#define N 20
int main( void ) { struct tm *newtime; char tmpbuf[N]; time_t test; time(&test); printf("%d ", test); // 1548138728 // 将时间戳转换成字符串 newtime=localtime(&test); strftime(tmpbuf, N, "%Y-%m-%d %H:%M:%S ", newtime); printf(tmpbuf); // 2019-01-22 14:32:08 return 0; }

那为什么呢?C语言定义了结构体struct tm

/* ISO C `broken-down time' structure.  */
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_MISC
  long int tm_gmtoff;        /* Seconds east of UTC.  */
  const char *tm_zone;        /* Timezone abbreviation.  */
# else
  long int __tm_gmtoff;        /* Seconds east of UTC.  */
  const char *__tm_zone;    /* Timezone abbreviation.  */
# endif
};
源码

常用组合: 

%Y-%m-%d %H:%M:%S
%%:显示字元%

%a:星期之缩写

%A:星期之全名

%b:月份之缩写

%B:月份之全名

%c:日期与时间

%d:两位数之日(01 - 31)

%H:两位数之24小时制(00 - 23)

%I :两位数之12小时制(01 - 12)

%j:三位数之日数(001 - 366)

%m:两位数之月(1 - 12)

%M:两位数之分(00 - 59)

%p:代表AM或PM

%S:两位数之秒(00 - 59)

%U:两位数之星期数(00 - 53),以星期日为第一天

%w:星期之表示(0 - 6),0为星期日

%W:两位数之星期数(00 - 53),以星期一为第一天(00 - 53)

%x:日期, %X:时间, %y:两位数之年(00to 99)

%Y:西元年, %Z:时间区名称

3. 将字符串转换成时间戳

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define N 20

int main(int argc, const char *argv[]) {

    // 3. 将字符串转换成时间戳
    struct tm *tmp_time = (struct tm *) malloc(sizeof(struct tm));
    //上面只是为了测试程序,注释这个是经常用的
    strptime("2019-01-22 15:10:00", "%Y-%m-%d %H:%M:%S", tmp_time); //时间24时制
    time_t t = mktime(tmp_time); // 将对象转出时间戳
    printf("%ld
", t);
    free(tmp_time);// 释放内存
    return 0;
}

4.获得UTC时间(通过结构图调用参数的方式)

#include <stdio.h>
#include <time.h>
 
int main(void)
{
    time_t timeValue = 0;
    struct tm *p = NULL;
 
    time(&timeValue);
    p = gmtime(&timeValue);
    printf("现在的时间(UTC)是 = %d年%d月%d日 星期%d %d时%d分%d秒 
",
           p->tm_year+1900,
           p->tm_mon+1,
           p->tm_mday,
           p->tm_wday,
           p->tm_hour,
           p->tm_min,
           p->tm_sec);
 
    return 0;
}

5.各种时间类型相互转换

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define N 20

int main(int argc, const char *argv[]) {
    time_t timeValue = 0;
    struct tm *p = NULL;
    char tmpbuf[N];

    // 1.获得时间戳
    time(&timeValue);
    printf("时间戳 = %ld
", timeValue); // timeValue:1548141000

    // 2.将时间戳转换成字符串
    p = localtime(&timeValue);
    strftime(tmpbuf, N, "%Y-%m-%d %H:%M:%S", p);
    printf("字符串时间 = %s
",tmpbuf); // tmpbuf:2019-01-22 15:10:00

    // 3. 将字符串转换成时间戳
    struct tm *tmp_time = (struct tm *) malloc(sizeof(struct tm));
    strptime(tmpbuf, "%Y-%m-%d %H:%M:%S", tmp_time); //时间24时制, 将时间转出对象形式
    //上面只是为了测试程序,注释这个是经常用的
    // strptime("2019-01-22 15:10:00", "%Y-%m-%d %H:%M:%S", tmp_time); //时间24时制
    time_t t = mktime(tmp_time); // 将对象转出时间戳
    printf("%ld
", t);
    free(tmp_time);// 释放内存
    return 0;
}
localtime的例子
#include <stdio.h>
#include <time.h>

#define N 20

int main(void)
{
    time_t timeValue = 0;
    struct tm *p = NULL;
    char tmpbuf[N];

    // 时间戳
    time(&timeValue);
    printf("时间戳 = %ld
",timeValue);
    //将时间戳转换成字符串
    p = localtime(&timeValue);
    strftime(tmpbuf, N, "%Y-%m-%d %H:%M:%S", p);
    printf("字符串时间 = %s
",tmpbuf);
    // 将字符串转化成时间戳
    timeValue = mktime(p);
    printf("转换之后的时间秒数是 = %ld
",timeValue);

    return 0;
}

6.模块包含的其他函数

time,ctime,gmtime,localtime,asctime,mktime

相比我觉得用的不是特别多,上面的例子就够用了。

参考blog链接:https://blog.csdn.net/qq_33706673/article/details/78907161

原文地址:https://www.cnblogs.com/renfanzi/p/10303828.html