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

#include <time.h>

1. c获取时间戳

#include <stdio.h>
#include <time.h> // 格林威治时间戳 void my_time(){
// 这里最好是long int
int time1 = ;
// 方法1
time(&time1);
printf("time1 is :%d \n", time1); // time1 is :1548137793 // 方法2
time_t time2 = ;
time2 = time(NULL);
printf("time2 is :%d \n", time2); // time1 is :1548137793 } int main(){
my_time();
return ;
}

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\n", test); //
// 将时间戳转换成字符串
newtime=localtime(&test); strftime(tmpbuf, N, "%Y-%m-%d %H:%M:%S\n", newtime);
printf(tmpbuf); // 2019-01-22 14:32:08 return ;
}

那为什么呢?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:两位数之日( - )

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

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

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

%m:两位数之月( - )

%M:两位数之分( - )

%p:代表AM或PM

%S:两位数之秒( - )

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

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

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

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

%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\n", t);
free(tmp_time);// 释放内存
return ;
}

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

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

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

#include <stdio.h>
#include <time.h>
#include <stdlib.h> #define N 20 int main(int argc, const char *argv[]) {
time_t timeValue = ;
struct tm *p = NULL;
char tmpbuf[N]; // 1.获得时间戳
time(&timeValue);
printf("时间戳 = %ld\n", timeValue); // timeValue:1548141000 // 2.将时间戳转换成字符串
p = localtime(&timeValue);
strftime(tmpbuf, N, "%Y-%m-%d %H:%M:%S", p);
printf("字符串时间 = %s\n",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\n", t);
free(tmp_time);// 释放内存
return ;
}
localtime的例子

#include <stdio.h>
#include <time.h> #define N 20 int main(void)
{
time_t timeValue = ;
struct tm *p = NULL;
char tmpbuf[N]; // 时间戳
time(&timeValue);
printf("时间戳 = %ld\n",timeValue);
//将时间戳转换成字符串
p = localtime(&timeValue);
strftime(tmpbuf, N, "%Y-%m-%d %H:%M:%S", p);
printf("字符串时间 = %s\n",tmpbuf);
// 将字符串转化成时间戳
timeValue = mktime(p);
printf("转换之后的时间秒数是 = %ld\n",timeValue); return ;
}

6.模块包含的其他函数

time,ctime,gmtime,localtime,asctime,mktime

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

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

Linux c time模块函数库的更多相关文章

  1. 【Linux】动态链接函数库

    动静区别 1. gcc –c mylib.c –o mylib.o 2. gcc -shared -fPIC mylib.o -o libmylib.so 3. 将制作好的libmylib.so 复制 ...

  2. Linux 安装python 模块及库

    转载于https://blog.csdn.net/csdn_am/article/details/79924744 有时我们使用下载python 自带的pip 安装一些工具包时,会报如下错误 找不到满 ...

  3. Linux下创建C函数库

    http://blog.163.com/hitperson@126/blog/static/130245975201151552938133 http://blog.sina.com.cn/s/blo ...

  4. Linux 函数库

    概述 函数库其实就是函数,只不过是系统所调用的函数.这样说吧,我写了一个软件,所有的功能都需要我自己完成吗?其实是不需要的,因为很多功能是别人已经写好的,我只需要拿来用就好了.这些有独立功能并且可以被 ...

  5. linux中C的静态库和动态库分析

    从开始学C语言写第一个"hello world"历程到现在,我依然困惑于到底这个程序完整的执行流程是什么样的.不过,现在我正在尝试一点一点的揭开它的面纱.现在,我尝试分析linux ...

  6. C 函数库 (libc,glibc,uClibc,newlib)

    glibc glibc和libc都是Linux下的C函数库,libc是Linux下的ANSI C的函数库:glibc是Linux下的GUN C的函数库:GNU C是一种ANSI C的扩展实现.ANSI ...

  7. linux 函数库使用

    程序函数库可分为3种类型:静态函 数库(static libraries).共享函数库(shared libraries)和动态加载函数库(dynamically loaded libraries) ...

  8. Linux学习笔记13——使用curses函数库

    一 安装curses库 如果你的Linux系统中curses库,直接敲入命令sudo apt-get install libncurses5-dev,然后就会自动安装curses库,安装好之后敲入命令 ...

  9. linux curses函数库

    fedora20,安装yum install ncurses-devel 编译时:-lncurses 头文件:#include<curses.h> 参考:man ncurses \linu ...

随机推荐

  1. python模拟银行家算法

    前言: 大二第一学期学习了操作系统,期末实验课题要求模拟算法.遂根据自己学习的python写下此文.以此锻炼自己编码能力.虽说是重复造轮子,但还是自己的思路体现 代码及注释如下(银行家算法不再赘述): ...

  2. Git——常用场景解析

    总结:本篇文章从初识GitHub.Git,实践GitHub的五种常用场景,分别是:git for windows安装,git配置,克隆远程代码到本地,上传本地代码到远程以及Git的常用指令.相信Jam ...

  3. [PA2014]Bazarek

    [PA2014]Bazarek 题目大意: 有\(n(n\le10^6)\)件商品,\(m(m\le10^6)\)次询问.每次询问若选出其中的\(k\)个,要求它们的总价为奇数,求最大可能的总价. 思 ...

  4. shell脚本使用技巧3--调试

    1.使用-x,开启shell脚本的跟踪调试功能 ex:bash -x script.sh or sh -x script.sh 2.使用set -x 和 set +x对脚本进行部分调试(输入中间的内容 ...

  5. jmeter接口测试实例1-添加学生信息

    jmeter实例1:添加学生信息 进入jmeter,添加线程组改名称为添加学生信息(为了好区分接口),添加http请求,输入IP,方法,路径,在body data中输入json串,同上面postman ...

  6. Idea-每次修改JS文件都需要重启Idea才能生效解决方法

    最近开始使用Idea,有些地方的确比eclipse方便.但是我发现工程每次修改JS或者是JSP页面后,并没有生效,每次修改都需要重启一次Tomcat这样的确不方便.我想Idea肯定有设置的方法,不可能 ...

  7. ES6_入门(1)_let命令

    1. let声明变量只在let命令所在的代码区内有效. "use strict"; /*如果不加"use strict";会报错:Uncaught Syntax ...

  8. IO流(2)—知识结构

    结构: 注:此IO包下主要介绍: 节点流:(字节流)FileInputStream.FileOutputStream.(字符流)Filereader.FileWriter 处理流(缓冲流):(字节流) ...

  9. java获得上下周及本周日期

    public static SimpleDateFormat getFormat(String format) { return new SimpleDateFormat(format); } /** ...

  10. 【springboot】【redis】springboot结合redis,操作List集合实现时间轴功能

    springboot结合redis,操作List集合实现时间轴功能