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 ...
随机推荐
- python系统编程(三)
multiprocessing 如果你打算编写多进程的服务程序,Unix/Linux无疑是正确的选择.由于Windows没有fork调用,难道在Windows上无法用Python编写多进程的程序? 由 ...
- c++语言的设计和演化---在线函数
开始的c++语言中引入inline函数的目的是处理一些实时的情况,而普通的函数调用的开销无法被接受. 起初是在类的声明中定义inline函数,也只支持成员函数,后来才支持非成员函数:
- [jzoj]3760.【BJOI2014】Euler
Link https://jzoj.net/senior/#main/show/3760 Description 欧拉函数 φ(n) 定义为不超过正整数 n 并且与 n 互素的整数的数目. 可以证 ...
- js实现60s倒计时效果
适用于获取验证码等其他场景,下面代码直接粘贴句可以使用 // 60s获取验证码的js与html var timer = null; var count = 60; $('.box>button' ...
- js 操作数组
数组的增删 数组的尾部插入一个元素 var nums = [0,1,2,3,4]; nums.push(5,6,6); //[0,1,2,3,4,5,6,6] 删除数组尾部的元素 var nums = ...
- ios真机中Text组件出现多余边框
问题 ios真机中Text组件出现多余边框(模拟器不会出现,真机会出现该问题). 原因 在ios启动页设置中,预设的尺寸要求与设置中图片尺寸不符合导致屏幕精度计算出现问题(启动屏分辨率错误设置会导致手 ...
- D. Cutting Out 二分
题意是给你n个数字的序列,让你从中找含k个数字的序列,要求这k个数字要尽可能多次的从n个数字的序列中减去. 解法就是从1到n,二分查找可以删除的最大次数. http://codeforces.com/ ...
- Java利用ShutDownHook关闭系统资源
Java关闭钩子 在Java程序中能够通过加入关闭钩子,实如今程序退出时关闭资源的功能. 使用Runtime.addShutdownHook(Thread hook)向JVM加入关闭钩子 public ...
- linux下gflags的安装
gflags是google开发的一套命令行参数解析工具,被很多软件系统所依赖,应该算是一个基础的库,安装其实很简单,但是如果在网上找的一些教程大部分都是安装后不能被其他软件调用的,因为默认使用cmak ...
- Spark2.3(四十二):Spark Streaming和Spark Structured Streaming更新broadcast总结(二)
本次此时是在SPARK2,3 structured streaming下测试,不过这种方案,在spark2.2 structured streaming下应该也可行(请自行测试).以下是我测试结果: ...