时间模块需要引入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. 【DWM1000】 code 解密2一 工程初始化代码分析

    instance_init 函数追下去,绝大多数的代码都在初始化如下结构体 typedef struct { INST_MODE mode; instance_init -ANCHOR //insta ...

  2. 理解Session缓存

    session的缓存有两大作用 (1)减少访问数据库的频率.应用程序从内存中读取持久化对象的速度显然比到数据库中查询数据的速度快多了,音系Session的缓存 可以提高数据库访问性能 (2)保证缓存中 ...

  3. python执行方式及变量

    .python执行方式 (1)交互式:调试方便,无法保存代码 (2)命令行方式:可以永久保存代码 (3)python执行阶段 先启动python解释器,解释器像文本编辑器一样将文件内容从硬盘读到内存, ...

  4. 3ds max学习笔记(十)-- 实例操作(镜像和对齐)

    1,镜像 选择物体对象然后点击: 偏移:新对象距离轴心所在的直线的距离: 2.对齐 栗子: 选择小球,点击[对齐];鼠标放置在图种位置,点击鼠标左键 出现弹框 调整位置: 先选择对齐位置-->当 ...

  5. Jupyter Notebook 介绍 安装和使用技巧

    Jupyter Notebook介绍.安装及使用教程 原文链接:https://www.jianshu.com/p/91365f343585 目录一.什么是Jupyter Notebook? 1. 简 ...

  6. Android requestcode resultcode的作用

    requestcode 一个页面的不同事件,激发不同的函数,startActivityForResult中传入不同的请求码的值以调用下一个界面,在被调用界面结束返回第一个界面时,请求码会自动返回(自动 ...

  7. 获取url参数的精简代码

    题目描述 获取 url 中的参数 指定参数名称,返回该参数的值 或者 空字符串 不指定参数名称,返回全部的参数对象 或者 {} 如果存在多个同名参数,则返回数组 输入例子: getUrlParam(' ...

  8. Codeforces899D Shovel Sale(思路)

    http://codeforces.com/problemset/problem/899/D 还是得tag一下,以下代码只有G++ 14 6.4.0能过,其他都过不了不知为什么? 思路:先求出最多的9 ...

  9. jQuery中动画常用的样式获取并改变方法-

    (1)Top 和 left 经常要用到jquery获取对象的位置,jquery top left,jquery css left是相对于父级元素中第一个position为relative或absolu ...

  10. javaScript系列 [02]-javaScript对象探析

    [02]-javaScript对象探析 题记:多年前,以非常偶然的方式关注了微信公众号“面向对象”,本以为这个公众号主要以分享面向对象编程的干货为主,不料其乃实实在在的猿圈相亲平台.通过查看公开资料, ...