c/c++ 时间函数总结 linux
Linux c/c++关于时间函数的总结
很想花点时间整理下Linux c/c++关于时间函数,今天…
关于时间的存储
linux下存储时间常见的有两种存储方式,一个是从1970年到现在经过了多少秒,一个是用一个结构来分别存储年月日时分秒的。
time_t 这种类型就是用来存储从1970年到现在经过了多少秒,要想更精确一点,可以用结构struct timeval,它精确到微妙。
struct timeval
{
long tv_sec; /*秒*/
long tv_usec; /*微秒*/
};
而直接存储年月日的是一个结构:
struct tm
{
inttm_sec; /*秒,正常范围0-59, 但允许至61*/
inttm_min; /*分钟,0-59*/
inttm_hour; /*小时, 0-23*/
inttm_mday; /*日,即一个月中的第几天,1-31*/
inttm_mon; /*月, 从一月算起,0-11*/ 1+p->tm_mon;
inttm_year; /*年, 从1900至今已经多少年*/ 1900+ p->tm_year;
inttm_wday; /*星期,一周中的第几天, 从星期日算起,0-6*/
inttm_yday; /*从今年1月1日到目前的天数,范围0-365*/
int tm_isdst; /* 夏令时标识符,夏令时tm_isdst为正;不实行夏令时tm_isdst为0;*/
};
注:年份是从1900年起至今多少年,而不是直接存储如2013年,月份从0开始的,0表示一月,星期也是从0开始的, 0表示星期日,1表示星期一。
下面介绍一下我们常用的时间函数:
#include <time.h>
char *asctime(const struct tm* timeptr)
将结构中的信息转换为真实世界的时间,以字符串的形式显示
char *ctime(const time_t *timep)
将timep转换为真是世界的时间,以字符串显示,它和asctime不同就在于传入的参数形式不一
#include<iostream>
#include<ctime>
using namespace std;
int main()
{ time_t t; t=time(NULL); cout <<ctime(&t); return 0;
}
double difftime(time_t time1, time_t time2)
返回两个时间相差的秒数
int stime(time_t *tp);
本函数用于设定电脑系统之日期与时间,传回值为0
格式化日期时间(strftime)
size_tstrftime(char *s, size_t maxsize, const char *fmt, const struct tm *t)
将t结构之资料依格式fmt方式设定给字串s,字串长度最长为maxsize,若转换错误则传回0,fmt之输出格式以ANSI方式为之。 下列为ANSI内定格式指定字及说明,使用在strftime内之格式。
%%:显示字元%
%a:星期之缩写
%A:星期之全名
%b:月份之缩写
%B:月份之全名
%c:日期与时间
%d:两位数之日(01 - 31)
%H:两位数之24小时制(00 - 23)
%I :两位数之12小时制(01 - 12)
%j:三位数之日数(001 - 366)
%m:两位数之月(1 - 12)
%M:两位数之分(00 - 59)
%p:代表AM或PM
%S:两位数之秒(00 - 59)
%U:两位数之星期数(00 - 53),以星期日为第一天
%w:星期之表示(0 - 6),0为星期日
%W:两位数之星期数(00 - 53),以星期一为第一天(00 - 53)
%x:日期, %X:时间, %y:两位数之年(00to 99)
%Y:西元年, %Z:时间区名称
类似于snprintf函数,我们可以根据format指向的格式字符串,将struct tm结构体中信息输出到s指针指向的字符串中,最多为max个字节。当然s指针指向的地址需提前分配空间,比如字符数组或者malloc开辟的堆空间
简单使用方法如下:
#include<stdio.h>
#include<string.h>
#include<time.h>
int main( void )
{struct tm *newtime;char tmpbuf[128];time_t test;time(&test);newtime=localtime(&test);strftime(tmpbuf, 128, "Today is %A, day %d of %B in the year %Y.\n", newtime);printf(tmpbuf);return 0;
}
char *_strtime(char *buf)
将现在时刻以HH:MM:SS方式输出放在buf内,字串至少9个Bytes长
char *_strtdate(char *buf);
取得目前PC系统日期及时间
#include<iostream>
#include<time>
using namespace std;
void main()
{
charsdate[9];
charstime[9];
_strdate(sdate);
_strtime(stime);
cout<<"目前日期:"<<sdate<<endl<<"现在时刻:"<<stime<<endl;
}
支持windows
struct tm* gmtime(const time_t *timep)
将time_t表示的时间转换为没有经过时区转换的UTC时间(在计算机中看到的utc时间都是从(1970年01月01日 0:00:00)开始计算秒数的。所看到的UTC时间那就是从1970年这个时间点起到具体时间共有多少秒。),是一个struct tm结构指针
stuct tm* localtime(const time_t *timep)
和gmtime类似,但是它是经过时区转换的时间。
time_t mktime(struct tm* timeptr)
将structtm 结构的时间转换为从1970年至今的秒数
time_t time(time_t *t)
取得从1970年1月1日至今的秒数。
参考代码:
#include<time.h>
int main()
{char *wday[] = {"Sun","Mon", "Tue", "Wed", "Thu","Fri", "Sat"};time_t timep;struct tm *p;time(&timep); /*获得time_t结构的时间,UTC时间*/p = localtime(&timep); /*转换为struct tm结构的当地时间*/printf("%d/%d/%d ", 1900 + p->tm_year, 1 +p->tm_mon, p->tm_mday);printf("%s %d:%d:%d\n", wday[p->tm_wday], p->tm_hour,p->tm_min, p->tm_sec);return 0;
}
clock函数
#include<time.h>
clock_tclock(void);
返回从程序开始运行到程序中调用clock()函数之间的CPU时钟计时单元数
例子如下:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{long loop = 10000000L;double duration;clock_t start, end;printf("Time to do %ld empty loops is", loop);start = clock();while(loop--) ;end = clock();duration =(double)(end-start)/CLOCKS_PER_SEC;printf("%f seconds\n", duration);return(0);
}
utime函数
更改文件的存取和修改时间
#include<time.h>
intutime(const char pathname, const struct utimbuf *times);
返回值:成功返回0,失败返回-1
times为空指针,存取和修改时间设置为当前时间
#include<stdio.h>
#include<time.h>
int main(int argc, char *argv[])
{if(argc < 2){fprintf(stderr, "Error:usging command file_path");exit(1);}utime(argv[1], NULL);return(0);
}
编译、运行:
$touch file_test
$ ls-al file_test // 先创建一个文件file_test,查看一下他的创建时间
-rw-r--r--1 hongdy hongdy 3431 05-01 05:59 file_test
$ gccutime.c –o utime
$./utime file_test
$ ls-al file_test
-rw-r--r--1 hongdy hongdy 3431 05-01 06:00 file_test
settimeofday函数
设置目前时间
#include<sys/time.h>
#include<unistd.h>
int settimeofday ( const& nbspstruct timeval*tv,const struct timezone *tz);
函数说明 settimeofday()会把目前时间设成由tv所指的结构信息,当地时区信息则设成tz所指的结构。详细的说明请参考gettimeofday()。注意,只有root权限才能使用此函数修改时间。
返回值 成功则返回0,失败返回-1,错误代码存于errno。
错误代码 EPERM 并非由root权限调用settimeofday(),权限不够。
EINVAL时区或某个数据是不正确的,无法正确设置时间。
times为空指针,存取和修改时间设置为当前时间
/************************************************
设置操作系统时间
参数:*dt数据格式为"2013-10-20 20:30:30"
调用方法:
char*pt="2013-10-20 20:30:30";
SetSystemTime(pt);
**************************************************/
intSetSystemTime(char *dt)
{
structrtc_time tm;
structtm _tm;
structtimeval tv;
time_ttimep;
sscanf(dt,"%d-%d-%d %d:%d:%d", &tm.tm_year,
&tm.tm_mon,&tm.tm_mday,&tm.tm_hour,
&tm.tm_min,&tm.tm_sec);
_tm.tm_sec= tm.tm_sec;
_tm.tm_min= tm.tm_min;
_tm.tm_hour= tm.tm_hour;
_tm.tm_mday= tm.tm_mday;
_tm.tm_mon= tm.tm_mon - 1;
_tm.tm_year= tm.tm_year - 1900;timep= mktime(&_tm);
tv.tv_sec= timep;
tv.tv_usec= 0;
if(settimeofday(&tv, (struct timezone *) 0) < 0)
{
printf ("Setsystem datatime error!\n");
return -1;
}
return 0;
}
gettimeofday函数取得目前的时间
#include<time.h>
intgettimeofday ( struct& nbsptimeval * tv , struct timezone * tz );
函数说明 gettimeofday()会把目前的时间有tv所指的结构返回,当地时区的信息则放到tz所指的结构中。
timeval结构定义为:
structtimeval{
longtv_sec; /*秒*/
longtv_usec; /*微秒*/
};
timezone结构定义为:
structtimezone{
inttz_minuteswest; /*和Greenwich时间差了多少分钟*/
inttz_dsttime; /*日光节约时间的状态*/
};
上述两个结构都定义在/usr/include/sys/time.h。tz_dsttime 所代表的状态如下
DST_NONE/*不使用*/
DST_USA/*美国*/
DST_AUST/*澳洲*/
DST_WET/*西欧*/
DST_MET/*中欧*/
DST_EET/*东欧*/
DST_CAN/*加拿大*/
DST_GB/*大不列颠*/
DST_RUM/*罗马尼亚*/
DST_TUR/*土耳其*/
DST_AUSTALT/*澳洲(1986年以后)*/
返回值 成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tv和tz所指的内存空间超出存取权限。
times为空指针,存取和修改时间设置为当前时间
gettimeofday函数的例子
#include<sys/time.h>
#include<unistd.h>
main(){
structtimeval tv;
structtimezone tz;
gettimeofday(&tv , &tz);
printf(“tv_sec;%d\n”, tv,.tv_sec)
printf(“tv_usec;%d\n”,tv.tv_usec);
printf(“tz_minuteswest;%d\n”, tz.tz_minuteswest);
printf(“tz_dsttime,%d\n”,tz.tz_dsttime);
}
编译、运行:
tv_usec:136996
tz_minuteswest:-540
tz_dsttime:0
tv_sec:974857339
timer_struct结构
struct timer_struct {
unsigned long expires; //定时器被激活的时刻
void (*fn)(void); //定时器激活后的处理函数
}
c/c++ 时间函数总结 linux相关推荐
- linux内核时间函数us,Linux上系统时间函数、DST等相关有关问题总结
http://www.reader8.cn/jiaocheng/20120910/1995886.html 2012 Linux下系统时间函数.DST等相关问题总结1. 内核中时间的基本类型:在Lin ...
- linux内核时间函数us,linux内核中一个有趣的函数calibrate_delay ZZ
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 34 loops_per_sec &=~loopbit; 35 } 36 /* finally,adjust loops per second ...
- linux c++ 获取时间,详解Linux下的C++时间类型:time_t
Unix时间戳(Unix timestamp),或称Unix时间(Unix time).POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970年01月01日00时00 ...
- 三种睡眠时间函数的区别:linux 的sleep()、usleep()、nanosleep()函数
三种睡眠时间函数的区别:linux 的sleep().usleep().nanosleep()函数 (1)sleep()-------以秒为单位 unsigned int sleep(unsigned ...
- linux几种时间函数总结
一.linux时间函数总结 最近的工作中用到的时间函数比较频繁,今天抽时间总结一下,在linux下,常用的获取时间的函数有如下几个: asctime, ctime, gmtime, localti ...
- linux '$^t' 时间,Linux C时间函数 time_t struct tm
Linux C时间函数 time_t struct tm #include 关于时间的类型: time_t long型,表示从1970年1月1日到现在经过的秒数. struct tm { int tm ...
- linux时间调整为dst,Linux上系统时间函数、DST等相关有关问题总结
Linux下系统时间函数.DST等相关问题总结 1. 内核中时间的基本类型: 在Linux内核中,常见的时间类型有以下两种:系统时间(system time)和实时时间(real time),其实,方 ...
- Linux时间函数札记
关于gmtime.gmtime_r.localtime.localtime_r 测试环境:vmware 7 + Redhat5.5,系统时间使用UTC,时区为上海. 1.函数功能介绍 使用man gm ...
- linux时间函数详解
一.时间相关说明 格林威治时间表示0时区的标准时间.其他时区的时间和此标准时间均有时间差.UTC(Universal Time Coordinated)是世界协调时间,是格林威治时间在互联网中的表示方 ...
最新文章
- OpenStack AMQP与kombu
- jmeter中文_JMeter安装配置
- 查询当天数据_【财会人职场必备】发票勾选、查询、认证等25问!简直太全了!都收藏了!...
- grub2的配置文件grub.cfg详解
- 老师一定没有教!9个数学速算技巧!看到第1个就跪了!
- 吴恩达DeepLearningCourse2-改善深层神经网络:超参数调试、正则化以及优化
- ShellExecute 函数的用法和实例
- 前端学习(3209):react中类中方法的this指向
- xcode8 崩溃问题
- python3 多继承搜索__init__方法的两种策略
- 《Go学习笔记 . 雨痕》方法
- Python设计模式--策略模式
- 同济版《线性代数》再遭口诛笔伐,网友:它真的不太行
- Ubuntu运行坦克大战
- PMSM FOC控制 Matlab/Simulink仿真之Clark变换
- python识别二维码
- Redis和Memcached区别
- 自定义Openstack图标
- CSS实现最简洁的四角边框
- 商品详情页系统架构-笔记12 - 商品详情页整体架构组成+前端介绍