linux中时间函数
linux下常用时间类型有四种: time_t 、 struct tm、 struct timeval 、 struct timespec
1、time_t 时间函数
time_t 类型在time.h中定义:
#ifndef __TIME__T
#define __TIME_T
typedef long time_t
#endif
可见, time_t 实际上是一个长整型,其值表示从1970年1月1日00时00分00秒(linux系统的Epoch时间)到当前时刻的秒数,由于time_t类型长度限制;
(long型),它所表示的时间不能晚于2038年1月19日03时14分07秒(UTC),(64位机器不存在此问题,想想why)
使用time()函数获取当前时间的time_t值,使用ctime()函数将time_t 转为当地时间字符串(返回值是字符串);
备注:UTC时间有时也称为GMT时间,其实UTC和GMT两者几乎是同一概念。它们都是指格林尼治标准时间,只不过UTC的称呼更为正式一点。两者区别在于前者是天文上的概念,而后者是基于一个原子钟。
1.2 struct tm 时间类型
struct tm结构在time.h 中定义:
#ifndef __TM_DEFINED
struct tm{
int tm_sec; //秒
int tm_min; //分
int tm_hour; //时
int tm_mday; //日
int tm_mon; //月份
int tm_year; //年份
int tm_wday; //星期
int tm_yday; /*从每年的1月1日开始的天数-取值区间为[0, 365],0代表1月1日*/
int tm_isdst; /*夏令时标识符,使用夏令时,tm_isdst为正,不使用夏令时,tm_isdst为0,不了解情况时>,tm_isdst为负*/
};
#define __TM_DEFINED
#endif
ANSI C标准称使用tm结构的这种时间表示为分解时间(broken-down time)
gmtime() / localtime() 将time_t 时间类型转换为tm结构体;
mktime()将tm结构体转换为time_t时间类型;
asctime()将struct tm转换为字符串形式;
1.3 struct timeval时间类型
struct timeval结构体在time.h中的定义:
struct timeval{
time_t tv_sec; //秒
suseconds_t tv_usec; //微秒(10^-6)s //毫秒(10^-3)s
};
设置时间函数settimeofday( )与获取时间函数gettimeofday( )均使用该事件类型作为传参。
1.4 struct timespec 时间类型
struct timespec 结构体time.h定义:
struct timespec{
time_t tv_sec; //秒
long tv_nsec; //纳秒(10^-9)s
};
2、Linux下常用时间函数
Linux下常用时间函数有:time( )、ctime( )、gmtime( )、localtime( )、mktime( )、asctime( )、difftime( )、gettimeofday( )、settimeofday( )
2.1 time( )函数
头文件:#include <time.h>
函数定义:time_t time(time_t *timer)
功能描述:该函数返回从1970年1月1日00时00分00秒至今所经过的秒数。如果time_t *timer非空指针,函数也会将返回值存到timer指针指向的内存。
返回值:成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于errno中。
例:
- time_t seconds;
- seconds = time((time_t *)NULL);
2.2 ctime( )函数
头文件:#include <time.h>
函数定义:char *ctime(const time_t *timep);
功能描述:ctime( )将参数timep指向的time_t时间信息转换成实际所使用的时间日期表示方法,并以字符串形式返回。字符串格式为:"Wed Jun 20 21:00:00 2012\n"。
例:
- time_t timep;
- tmep = time(NULL);
- printf("%s\n", ctime(&timep));
2.3 gmtime( )函数
头文件:#include <time.h>
函数定义:struct tm *gmtime(const time_t *timep)
功能描述:gmtime( )将参数timep指向的time_t时间信息转换成以tm结构体表示的GMT时间信息,并以struct tm*指针返回。
GMT:GMT是中央时区,北京在东8区,相差8个小时,所以北京时间=GMT时间+8小时。
例:
- int main(void)
- {
- char *wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
- time_t timep;
- struct tm *p_tm;
- timep = time(NULL);
- p_tm = gmtime(&timep); /*获取GMT时间*/
- printf("%d-%d-%d ", (p_tm->tm_year+1900), (p_tm->mon+1), p_tm->tm_mday);
- printf("%s %d:%d:%d\n", wday[p_tm->tm_wday], p_tm->tm_hour, p_tm->tm_min, p_tm->tm_sec);
- }
2.4 localtime( )函数
头文件:#include <time.h>
函数定义:struct tm *localtime(const time_t *timep);
功能描述:localtime( )将参数timep指向的time_t时间信息转换成以tm结构体表示的本地时区时间(如北京时间= GMT+小时)。
例:
- int main(void)
- {
- char *wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
- time_t timep;
- struct tm *p_tm;
- timep = time(NULL);
- p_tm = localtime(&timep); /*获取本地时区时间*/
- printf("%d-%d-%d ", (p_tm->tm_year+1900), (p_tm->mon+1), p_tm->tm_mday);
- printf("%s %d:%d:%d\n", wday[p_tm->tm_wday], p_tm->tm_hour, p_tm->tm_min, p_tm->tm_sec);
- return 0;
- }
2.5 mktime( )函数
头文件:#include <time.h>
函数定义:time_t mktime(struct tm *p_tm);
功能描述:mktime( )将参数p_tm指向的tm结构体数据转换成从1970年1月1日00时00分00秒至今的GMT时间经过的秒数。
例:
- int main(void)
- {
- time_t timep:
- struct tm *p_tm;
- timep = time(NULL);
- pintf("time( ):%d\n", timep);
- p_tm = local(&timep);
- timep = mktime(p_tm);
- printf("time( )->localtime( )->mktime( ):%d\n", timep);
- return 0;
- }
2.6 asctime( )函数
头文件:#include <time.h>
函数定义:char *asctime(const struct tm *p_tm);
功能描述:asctime( )将参数p_tm指向的tm结构体数据转换成实际使用的时间日期表示方法,并以字符串形式返回(与ctime函数相同)。字符串格式为:"Wed Jun 20 21:00:00 2012\n"。
例:
- int main(void)
- {
- time_t timep;
- timep = time(NULL);
- printf("%s\n", asctime(gmtime(&timep)));
- return 0;
- }
2.7 difftime( )函数
头文件:#include <time.h>
函数定义:double difftime(time_t timep1, time_t timep2);
功能描述:difftime( )比较参数timep1和timep2时间是否相同,并返回之间相差秒数。
例:
- int main(void)
- {
- time_t timep1, timep2;
- timep1 = time(NULL);
- sleep(2);
- timep2 = time(NULL);
- printf("the difference is %f seconds\n", difftime(timep1, timep2));
- return 0;
- }
2.8 gettimeofday( )函数
头文件:#include <sys/time.h>
#include <unistd.h>
函数定义:int gettimeofday(struct timeval *tv, struct timezone *tz);
功能描述:gettimeofday( )把目前的时间信息存入tv指向的结构体,当地时区信息则放到tz指向的结构体。
struct timezone原型:
- struct timezone{
- int tz_minuteswest; /*miniutes west of Greenwich*/
- int tz_dsttime; /*type of DST correction*/
- };
例:
- struct timeval tv;
- struct timeval tz;
- gettimeofday(&tv, &tz);
附:
使用time函数族获取时间并输出指定格式字符串例子(strftime( )函数):
- int main(void)
- {
- char strtime[20] = {0};
- time_t timep;
- struct tm *p_tm;
- timep = time(NULL);
- p_tm = localtime(&timep);
- strftime(strtime, sizeof(strtime), "%Y-%m-%d %H:%M:%S", p_tm);
- return 0;
- }
2.9 settimeofday( )函数
头文件:#include <sys/time.h>
#include <unistd.h>
函数定义:int settimeofday(const struct timeval *tv, const struct timezone *gz);
功能描述:settimeofday( )把当前时间设成由tv指向的结构体数据。当前地区信息则设成tz指向的结构体数据。
例:
- int main(void)
- {
- char t_string[] = "2012-04-28 22:30:00";
- struct tm time_tm;
- struct timeval time_tv;
- time_t timep;
- int ret = 0;
- sscanf(t_string, "%d-%d-%d %d:%d:%d", &time_tm.tm_year, &time_tm.tm_mon, &time_tm.tm_mday, &time_tm.tm_hour, &time_tm.tm_min, &time_tm.tm_sec);
- time_tm.tm_year -= 1900;
- time_tm.tm_mon -= 1;
- time_tm.tm_wday = 0;
- time_tm.tm_yday = 0;
- time_tm.tm_isdst = 0;
- timep = mktime(&time_tm);
- time_tv.tv_sec = timep;
- time_tv.tv_usec = 0;
- ret = settimeofday(&time_tv, NULL);
- if(ret != 0)
- {
- fprintf(stderr, "settimeofday failed\n");
- return -1;
- }
- return 0;
- }
举例:获取当天零点的时间:
#include <time.h>
#include <stdio.h> int main()
{
time_t nowtime;
static time_t time_0 = ;
time(&nowtime);
printf("%d\n",nowtime); struct tm mytm = *localtime(&nowtime);
mytm.tm_hour = ;
mytm.tm_min = ;
mytm.tm_sec = ;
time_0 = mktime(&mytm); printf("%d\n", time_0); return ;
}
linux中时间函数的更多相关文章
- 【转】linux 中fork()函数详解
在看多线程的时候看到了这个函数,于是学习了下,下面文章写的通俗易懂,于是就开心的看完了,最后还是很愉快的算出了他最后一个问题. linux 中fork()函数详解 一.fork入门知识 一个进程,包括 ...
- linux中probe函数传递参数的寻找(下)
点击打开链接 linux中probe函数传递参数的寻找(下) 通过追寻driver的脚步,我们有了努力的方向:只有找到spi_bus_type的填充device即可,下面该从device去打通,当两个 ...
- linux中 probe函数的何时调用的?
点击打开链接 linux中 probe函数何时调用的 所以的驱动教程上都说:只有设备和驱动的名字匹配,BUS就会调用驱动的probe函数,但是有时我们要看看probe函数里面到底做了什么,还有传递给p ...
- open()函数 linux中open函数使用
来源:http://www.cnblogs.com/songfeixiang/p/3733855.html linux中open函数使用 open函数用来打开一个设备,他返回的是一个整型变量,如果 ...
- Linux 中write()函数的出错情况及处理
write函数首先将进程需要发送的数据先放在进程缓冲区中,然后向socket的发送缓冲区进行拷贝,在此,可能出现这样情况,即当进程缓冲区中的数据量大于此时发送缓冲区中所能接受的数据量时,若此时处于阻塞 ...
- linux中probe函数中传递的参数来源(上)
点击打开链接 上一篇中,我们追踪了probe函数在何时调用,知道了满足什么条件会调用probe函数,但probe函数中传递的参数我们并不知道在何时定义,到底是谁定义的,反正不是我们在驱动中定义的(当然 ...
- Linux中fork()函数详解(转载)
linux中fork()函数详解 一.fork入门知识 一个进程,包括代码.数据和分配给进程的资源.fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程,也就是两个进程可以做完全相同的事, ...
- Linux中open函数以及退出进程的函数
open函数的flag详解1 读写权限:O_RDONLY O_WRONLY O_RDWR (1)linux中文件有读写权限,我们在open打开文件时也可以附带一定的权限说明 (譬如O_RDONLY就表 ...
- php 中时间函数date及常用的时间计算
曾在项目中需要使用到今天,昨天,本周,本月,本季度,今年,上周上月,上季度等等时间戳,趁最近时间比较充足,因此计划对php的相关时间知识点进行总结学习 1,阅读php手册date函数 常用时间函数: ...
随机推荐
- 深入Android媒体存储服务(二):磁盘扫描流程
简介: 本文是<深入Android媒体存储服务>系列第二篇,简要介绍媒体存储服务扫描文件的流程.文中介绍的是 Android 4.2. Android 有一套媒体存储服务,进程名是 and ...
- 如何評鑑一家SMT代工廠
我們一般稱專業的「電子代工廠」為 EMS(Electronics Manufacturing Service,電子製造服務業) 或 CM(Contract Manufacturer,合同製造廠),這些 ...
- android 设置头像以及裁剪功能
在android的开发过程中,经常遇到设置用户头像以及裁剪图像大小的功能.昨天我遇到了设置用户头像的功能,开始不知道怎么搞,在技术群里问也没人回 答,就研究了微信用户设置头像的功能,了解到用户设置图像 ...
- 菜鸟级SQL Server21天自学通(文档+视频)
SQL语言的主要功能就是同各种数据库建立联系,进行沟通.按照ANSI(美国国家标准协会)的规定,SQL被作为关系型数据库管理系统的标准语言.SQL语句可以用来执行各种各样的操作,例如更新数据库中的数据 ...
- saiku导出excel单元格格式与中文列宽自动适应
在saiku导出excel后打开发现单元格的整数也显示为小数,并且含有中文的列宽没有自动适应,解决办法如下: 打开ExcelWorksheetBuilder.java文件,找到applyCellFor ...
- JavaScript之call()和apply()方法详解
简介:apply()和call()都是属于Function.prototype的一个方法属性,它是JavaScript引擎内在实现的方法,因为属于Function.prototype,所以每个Func ...
- JavaSE思维导图(三)
- Win8.1重装win7或win10中途无法安装
一.有的是usb识别不了,因为新的机器可能都是USB3.0的,安装盘是Usb2.0的. F12更改系统BIOS设置,我改了三个地方: 1.设置启动顺序为U盘启动 2.关闭了USB3.0 control ...
- <%=id%>是什么意思
<%=% > 这里可以绑定后台的一个PUBLIC变量 <% % > 如果没有等号 可以在里面写C#语句
- SSIS: Lookup组件高级用法,生成推断成员(inferred member)
将数据导入事实表如果无法匹配维度表的记录一般有两种处理方式. 一是将不匹配记录输出到一个表中待后续处理,然后重新导入.二是先生成维度Key,后续再完善维度key,本文指导各位使用第二种方式. 背景 比 ...