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相关推荐

  1. linux内核时间函数us,Linux上系统时间函数、DST等相关有关问题总结

    http://www.reader8.cn/jiaocheng/20120910/1995886.html 2012 Linux下系统时间函数.DST等相关问题总结1. 内核中时间的基本类型:在Lin ...

  2. linux内核时间函数us,linux内核中一个有趣的函数calibrate_delay ZZ

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 34  loops_per_sec &=~loopbit; 35  } 36 /* finally,adjust loops per second ...

  3. linux c++ 获取时间,详解Linux下的C++时间类型:time_t

    Unix时间戳(Unix timestamp),或称Unix时间(Unix time).POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970年01月01日00时00 ...

  4. 三种睡眠时间函数的区别:linux 的sleep()、usleep()、nanosleep()函数

    三种睡眠时间函数的区别:linux 的sleep().usleep().nanosleep()函数 (1)sleep()-------以秒为单位 unsigned int sleep(unsigned ...

  5. linux几种时间函数总结

    一.linux时间函数总结 最近的工作中用到的时间函数比较频繁,今天抽时间总结一下,在linux下,常用的获取时间的函数有如下几个:  asctime,  ctime, gmtime, localti ...

  6. 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 ...

  7. linux时间调整为dst,Linux上系统时间函数、DST等相关有关问题总结

    Linux下系统时间函数.DST等相关问题总结 1. 内核中时间的基本类型: 在Linux内核中,常见的时间类型有以下两种:系统时间(system time)和实时时间(real time),其实,方 ...

  8. Linux时间函数札记

    关于gmtime.gmtime_r.localtime.localtime_r 测试环境:vmware 7 + Redhat5.5,系统时间使用UTC,时区为上海. 1.函数功能介绍 使用man gm ...

  9. linux时间函数详解

    一.时间相关说明 格林威治时间表示0时区的标准时间.其他时区的时间和此标准时间均有时间差.UTC(Universal Time Coordinated)是世界协调时间,是格林威治时间在互联网中的表示方 ...

最新文章

  1. os:进程与线程问题
  2. 数据库:MySQL索引总结
  3. 下一个嵌入式大神,就是你。
  4. 快速配置Ehcache
  5. jmeter web监听结果_Jmeter性能测试
  6. 织梦自适应php源码,DEDE织梦PHP源码响应式建筑设计类网站织梦模板(自适应手机端)...
  7. cropper.js插件做图片上传裁剪图片大小
  8. windows下定时任务设置
  9. mvc ajax验证登录页面,MVC 4 jquery Ajax调用返回登录页面html
  10. Hibernate 基本类型
  11. 未处理的异常: 0xC0000005: 读取位置 0x00000000 时发生访问冲突
  12. android 极简浏览器,XBrowser 一款Android下极简的浏览器,个人作品但不输商业产品...
  13. php用ckeditor无法上传大图片,php ckeditor上传图片文件大小限制修改
  14. 东周列国志美文摘录-晏平仲巧辩服荆蛮
  15. 计算机所有以太网适配的ip,演示win10电脑以太网没有有效的ip配置
  16. [BJDCTF2020]ZJCTF,不过如此
  17. 设计模式六大原则-java复读记录
  18. 计算机操作系统属于硬件系统吗,操作系统属于计算机的硬件系统吗
  19. python生成图像公章,用PPT就可以做印章?是的,超简单超逼真,教你一分钟搞定...
  20. 山东如意路嘉纳高级定制西装品牌惊艳亮相intertextile面料展 - 服装资讯中心 - 华衣网...

热门文章

  1. LeetCode 29. Divide Two Integers
  2. L3-005. 垃圾箱分布-PAT团体程序设计天梯赛GPLT(Dijkstra)
  3. 不属于计算机网络的资源词,计算机网络应用试题
  4. webstorm简单介绍,webstrom基本使用
  5. mysql组合索引与字段顺序
  6. Shell脚本8种字符串截取方法总结
  7. Android UI设计之十一自定义ViewGroup,打造通用的关闭键盘小控件ImeObser
  8. svn client
  9. 4种常用压缩格式在hadoop中的应用
  10. spring 中加载xml配置文件的方式