Linux c time模块函数库
时间模块需要引入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 ;
}
#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模块函数库的更多相关文章
- 【Linux】动态链接函数库
动静区别 1. gcc –c mylib.c –o mylib.o 2. gcc -shared -fPIC mylib.o -o libmylib.so 3. 将制作好的libmylib.so 复制 ...
- Linux 安装python 模块及库
转载于https://blog.csdn.net/csdn_am/article/details/79924744 有时我们使用下载python 自带的pip 安装一些工具包时,会报如下错误 找不到满 ...
- Linux下创建C函数库
http://blog.163.com/hitperson@126/blog/static/130245975201151552938133 http://blog.sina.com.cn/s/blo ...
- Linux 函数库
概述 函数库其实就是函数,只不过是系统所调用的函数.这样说吧,我写了一个软件,所有的功能都需要我自己完成吗?其实是不需要的,因为很多功能是别人已经写好的,我只需要拿来用就好了.这些有独立功能并且可以被 ...
- linux中C的静态库和动态库分析
从开始学C语言写第一个"hello world"历程到现在,我依然困惑于到底这个程序完整的执行流程是什么样的.不过,现在我正在尝试一点一点的揭开它的面纱.现在,我尝试分析linux ...
- C 函数库 (libc,glibc,uClibc,newlib)
glibc glibc和libc都是Linux下的C函数库,libc是Linux下的ANSI C的函数库:glibc是Linux下的GUN C的函数库:GNU C是一种ANSI C的扩展实现.ANSI ...
- linux 函数库使用
程序函数库可分为3种类型:静态函 数库(static libraries).共享函数库(shared libraries)和动态加载函数库(dynamically loaded libraries) ...
- Linux学习笔记13——使用curses函数库
一 安装curses库 如果你的Linux系统中curses库,直接敲入命令sudo apt-get install libncurses5-dev,然后就会自动安装curses库,安装好之后敲入命令 ...
- linux curses函数库
fedora20,安装yum install ncurses-devel 编译时:-lncurses 头文件:#include<curses.h> 参考:man ncurses \linu ...
随机推荐
- MyBatis 命名空间与命名解析
命名空间 使用完全限定名来进一步区分语句. 命名解析 为了减少输入量,MyBatis 对所有的命名配置元素(包括语句,结果映射,缓存等)使用如下的命名解析规则: 完全限定名(比如“com.mypack ...
- Ubuntu1404安装eclipse(目的是为了运行python,当然java更可以)
原创文章,不得转载 我是彩印网(www.caiyin.com) 的技术开发人员,在这里首先请允许我做下业务的介绍 彩印网是由北京出彩电子商务有限公司运营的专业印刷电商,集线上购物.支付.设计.制作.配 ...
- pygame-KidsCanCode系列jumpy-part6-主角挂掉重新开始
游戏的虚拟世界中,最让人happy的一个因素就是主角挂了,而且重来,只要restart就行了,不象现实中人的生命只有1次.回顾上节的效果,如果方块向下落时,挡板没接住,整个游戏就跪了: 如果我们希望方 ...
- 【转】java日志组件介绍(common-logging,log4j,slf4j,logback )
common-logging common-logging是apache提供的一个通用的日志接口.用户可以自由选择第三方的日志组件作为具体实现,像log4j,或者jdk自带的logging, comm ...
- 集合总结--ArrayList、LinkedList、HashMap
一.概述 ArrayList:数组集合. 查询.修改.新增(尾部新增)快,删除.新增(队列中间)慢,适用于查询.修改较多的场景. LinkedList:双向链表集合.查 ...
- MySQL中exists和in的区别及使用场景
exists和in的使用方式: 1 #对B查询涉及id,使用索引,故B表效率高,可用大表 -->外小内大 1 select * from A where exists (select * fro ...
- SharePoint 修改用户属性User Name
前言 最近,碰到一个奇怪的事情,在SharePoint里的用户,如果显示方式显示为登录名(Account)的方式,显示为空.如下图: 1.经过查找,发现是因为用户属性 User name为空造成的,如 ...
- CoordinatorLayout使用详解: 打造折叠悬浮效果
1.简介 CoordinatorLayout遵循Material 风格,包含在 support Library中,结合AppbarLayout, CollapsingToolbarLayout等 可 ...
- spring mvc 实战化项目之三板斧
laravel实战化项目之三板斧 spring mvc 实战化项目之三板斧 asp.net mvc 实战化项目之三板斧 接上文希望从一张表(tb_role_info 用户角色表)的CRUD展开spri ...
- Deep Learning.ai学习笔记_第三门课_结构化机器学习项目
目录 第一周 机器学习策略(1) 第二周 机器学习策略(2) 目标:学习一些机器学习优化改进策略,使得搭建的学习模型能够朝着最有希望的方向前进. 第一周 机器学习策略(1) 搭建机器学习系统的挑战:尝 ...