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 ...
随机推荐
- PHP中让json_encode不自动转义斜杠“/”的方法
最近将使用爬虫爬取的链接保存到 mysql 数据库中时,发现我将链接使用 json_encode 保存时候,在数据库中却显示了转义字符,我并不需要这转义的,看起来不清晰而且占用存储空间. 后来发现在默 ...
- BZOJ4855 : [Jsoi2016]轻重路径
首先用树状数组维护dfs序来快速支持一个点子树大小的询问. 每次删掉一个叶子时,从根开始往叶子走,显然只有$2size[x]\leq size[father]$的点的父亲才有可能换重儿子. 从根开始往 ...
- VueJs初步学习,一个表格的增删改查
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- git常用命令2--- git rebase
git rebase:简单而言就是把某个分支上的提交commit嫁接到另一个commit的后面,在这个过程中这些commit的base相对就改变了,也就叫变基. git rebase <upst ...
- 限制标题字符串的长度,超过长度的截取并加上"..."
/// <summary> /// 限制标题字符串的长度,超过长度的截取并加上"..." /// </summary> /// <param name ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) E. DNA Evolution 树状数组
E. DNA Evolution 题目连接: http://codeforces.com/contest/828/problem/E Description Everyone knows that D ...
- django项目的新建相关的命令及配置
创建工程 django-admin startproject 工程名称 运行开发服务器 python manage.py runserver 创建子应用 python manage.py st ...
- 关于#!/bin/bash和#!/bin/sh
关于#!/bin/bash和#!/bin/sh #!/bin/bash是指此脚本使用/bin/bash来解释执行. 其中,#!是一个特殊的表示符,其后,跟着解释此脚本的shell路径. bash只 ...
- es6学习笔记一:迭代器和for-of循环
我们如何遍历一个数组呢?在20年前,我们是这样遍历一个数组的: var myArr = []; for (var i = 0; i < arr.length; i++) { console.lo ...
- django之模型层(model)--多表相关操作(图书管理小练习)
前面几篇随笔的数据库增删改查操作都是在单表的操作上的,然而现实中不可能都是单表操作,更多的是多表操作,一对一,一对多,多对多的表结构才是我们经常需要处理的,本篇将带我们了解多表操作的一些相关操作.也会 ...