APUE学习笔记——6.10 时间与时间例程 time_t
typedef long time_t; /* 时间值time_t 为长整型的别名*/
1、获取/设置时间
1.1 time和time_t
#include <time.h>
time_t time(time_t *calptr);
Returns: value of time if OK,−1 on error
1.2 指定时钟类型
#include <time.h>
int clock_getres(clockid_t clk_id, struct timespec *res);
int clock_gettime(clockid_t clk_id, struct timespec *tp);
int clock_settime(clockid_t clk_id, const struct timespec *tp);
Link with -lrt.编译时使用链接库lrt
CLOCK_REALTIME:系统实时时间,使用此选项时功能相当于time(),如果系统时间被用户修改过,则对应的时间相应改变
CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响
CLOCK_PROCESS_CPUTIME_ID:本进程运行的CPU时间
CLOCK_THREAD_CPUTIME_ID:本线程运行的CPU时间
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
1.3 gettimeofday()与settimeofday()
#include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv, const struct timezone *tz);
2、时间格式转换

struct tm { /* a broken-down time */
int tm_sec; // 秒 【0~60】 60存在的原因是闰秒的存在
int tm_min; //分
int tm_hour; //时
int tm_mday; //日
int tm_mon; //月
int tm_year; //年 结构中记录的是从1900至今的年数
int tm_wday; //星期几 【0~6】
int tm_yday; // 一年中第几天
int tm_isdst; // 夏时制标志
};
2.1 time_t与struct tm相互转化
#include <time.h>
struct tm *gmtime(const time_t *calptr);
struct tm *localtime(const time_t *calptr);
Both return: pointer to broken-down time,NULLon error
#include <time.h>
time_t mktime(struct tm *tmptr);
Returns: calendar time if OK,−1 on error
#include<stdio.h>
#include <time.h>
#include<sys/time.h>
int main(void)
{
time_t calptr;
if (time(&calptr) == -1)
{
printf("Error: time() failed!\n");
exit(0);
}
printf("Time(time_t) now is: %d\n",(long) calptr);
/*
* 转换成可读到时间
*/
struct tm *gmptr; //国际标准时间
struct tm *localptr; //本地时间 /* 转换成国际标准时间 */
printf("\n\n ----------gm time--------------\n");
if (NULL == (gmptr = gmtime(&calptr)))
{
printf("Error: can't convert time_t to struct tm by gmtime()!\n");
exit(0);
}
printf("year:%d,\tMonth:%d,\tDay:%d,\tWeek:%d\n", gmptr->tm_year, gmptr->tm_mon, gmptr->tm_mday, gmptr->tm_wday);
printf("Hour:%d,\tMin:%d,\t,sec:%d\n", gmptr->tm_hour, gmptr->tm_min, gmptr->tm_sec);
/* 转换成本地标准时间 */
printf("\n\n ----------local time--------------\n");
if (NULL == (localptr = localtime(&calptr)))
{
printf("Error: can't convert time_t to struct tm by localtime()!\n");
exit(0);
}
printf("year:%d,\tMonth:%d,\tDay:%d,\tWeek:%d\n", localptr->tm_year, localptr->tm_mon, localptr->tm_mday, localptr->tm_wday);
printf("Hour:%d,\tMin:%d,\t,sec:%d\n", localptr->tm_hour, localptr->tm_min, localptr->tm_sec);
return 0;
}
windeal@ubuntu:~/Windeal/apue$ ./exe
Time(time_t) now is: 1409207607
----------gm time--------------
year:114, Month:7, Day:28, Week:4
Hour:6, Min:33, ,sec:27
----------local time--------------
year:114, Month:7, Day:28, Week:4
Hour:14, Min:33, ,sec:27
2.2 转换成格式化字符串
#include <time.h>
size_t strftime(char *restrict buf,size_t maxsize,
const char *restrict format,
const struct tm *restrict tmptr);
size_t strftime_l(char *restrict buf,size_t maxsize,
const char *restrict format,
const struct tm *restrict tmptr,locale_t locale);
——Both return: number of characters stored in array if room, 0 otherwise
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int
main(void)
{
time_t t;
struct tm *tmp;
char buf1[16];
char buf2[64];
time(&t);
tmp = localtime(&t);
if (strftime(buf1, 16, "time and date: %r, %a %b %d, %Y", tmp) == 0)
printf("buffer length 16 is too small\n");
else
printf("%s\n", buf1); if (strftime(buf2, 64, "time and date: %r, %a %b %d, %Y", tmp) == 0)
printf("buffer length 64 is too small\n");
else
printf("%s\n", buf2);
exit(0);
}
windeal@ubuntu:~/Windeal/apue$ ./exe
buffer length 16 is too small
time and date: 02:43:55 PM, Thu Aug 28, 2014
#include <time.h>
char *strptime(const char *restrictbuf,const char *restrictformat,struct tm *restricttmptr);
Returns: pointer to one character past last character parsed,NULLotherwise
关于转换符号:

APUE学习笔记——6.10 时间与时间例程 time_t的更多相关文章
- APUE学习笔记——3.10文件共享
基本概念 内核使用3个数据结构描述一个打开的文件:进程表.文件表.V节点表 首先了解3种数据结构的概念 1 进程表 每一个进程有一个进程表.进程表里是一组打开的文件描述符,如标 ...
- APUE学习笔记——10.9 信号发送函数kill、 raise、alarm、pause
转载注明出处:Windeal学习笔记 kil和raise kill()用来向进程或进程组发送信号 raise()用来向自身进程发送信号. #include <signal.h> int k ...
- MYSQL学习笔记三:日期和时间函数
MYSQL学习笔记三:日期和时间函数 1. 获取当前日期的函数和获取当前时间的函数 /*获取当前日期的函数和获取当前时间的函数.将日期以'YYYY-MM-DD'或者'YYYYMMDD'格式返回 */ ...
- 【python学习笔记】10.充电时刻
[python学习笔记]10.充电时刻 任何python都可以作为模块倒入 *.pyc:平台无关的经过编译的的python文件, 模块在第一次导入到程序中时被执行,包括定义类,函数,变量,执行语句 可 ...
- APUE学习笔记3_文件IO
APUE学习笔记3_文件IO Unix中的文件IO函数主要包括以下几个:open().read().write().lseek().close()等.这类I/O函数也被称为不带缓冲的I/O,标准I/O ...
- Java程序猿的JavaScript学习笔记(10—— jQuery-在“类”层面扩展)
计划按例如以下顺序完毕这篇笔记: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScript ...
- Linux学习笔记(10)linux网络管理与配置之一——主机名与IP地址,DNS解析与本地hosts解析(1-4)
Linux学习笔记(10)linux网络管理与配置之一——主机名与IP地址,DNS解析与本地hosts解析 大纲目录 0.常用linux基础网络命令 1.配置主机名 2.配置网卡信息与IP地址 3.配 ...
- SpringBoot学习笔记(10):使用MongoDB来访问数据
SpringBoot学习笔记(10):使用MongoDB来访问数据 快速开始 本指南将引导您完成使用Spring Data MongoDB构建应用程序的过程,该应用程序将数据存储在MongoDB(基于 ...
- Flutter学习笔记(10)--容器组件、图片组件
如需转载,请注明出处:Flutter学习笔记(10)--容器组件.图片组件 上一篇Flutter学习笔记(9)--组件Widget我们说到了在Flutter中一个非常重要的理念"一切皆为组件 ...
随机推荐
- Linux远程管理器xshell和xftp使用教程,以及遇到关闭Xshell后项目也停止的解决方法
1.xshell 是一个强大的安全终端模拟软件,它支持SSH1, SSH2, 以及Microsoft Windows 平台的TELNET 协议. 2.是一个基于 MS windows 平台的功能强大的 ...
- php下获取http状态的实现代码
在项目开发中,有时我们需要知道远程的URL地址是否能访问正常,判断其正常与否后进行下一步的操作,那么在PHP中如何获取远程HTTP的状态呢? 文件preg.php header("HTTP/ ...
- ubuntu下 gedit中文乱码
Gedit 3.x 版本设置 (适用于Ubuntu 11.10及以后) 命令方式 gsettings set org.gnome.gedit.preferences.encodings auto-de ...
- MySQL中表复制:create table like 与 create table as select
1 CREATE TABLE A LIKE B此种方式在将表B复制到A时候会将表B完整的字段结构和索引复制到表A中来. 2. CREATE TABLE A AS SELECT * FROM ...
- LeetCode——Integer Replacement
Question Given a positive integer n and you can do operations as follow: If n is even, replace n wit ...
- mathematical method
mathematical method 曲线拟合 指数 \(lnY = lna + bX\) 对数 \(Y = blnX + a\) 幂函数 \(lgY=lga+blgX\) 多元线性回归模型 回归分 ...
- 解题报告:poj 3264 最基本的线段树
2017-10-07 17:54:55 writer:pprp /* @theme: 最基本的线段树 @writer:pprp @end:17:38 @attention:记录的数组应该从1开始,不能 ...
- Python学习札记(二十一) 函数式编程2 map/reduce
参考:map/reduce Note 1.map():map()函数接收两个参数,一个是函数,一个是Iterable.map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回. ...
- 解决 libnanomsg.so.0: cannot open shared object file: No such file or directory 无法找到libnanomsg动态链接库
参考: [11]缺少动态连接库.so--cannot open shared object file: No such file or directory Importing Issues: cann ...
- winform中的ListBox和ComboBox绑定数据
将集合数据绑定到ListBox和ComboBox控件,界面上显示某个属性的内容 //... //自定义了Person类(有Name,Age,Heigth等属性) List<Person> ...