@[toc]数据库
freecplus是一个Linux系统下的C/C++开源框架,源代码请前往C语言技术网(www.freecplus.net)下载。框架
本文介绍的是freecplus框架的时间操做函数。ide
函数和类的声明文件是freecplus/_freecplus.h。函数
函数和类的定义文件是freecplus/_freecplus.cpp。操作系统
示例程序位于freecplus/demo目录中。.net
编译规则文件是freecplus/demo/makefile。code
UNIX操做系统根据计算机产生的年代和应用采用1970年1月1日做为UNIX的纪元时间,1970年1月1日0点做为计算机表示时间的是中间点,将从1970年1月1日开始通过的秒数用一个整数存放,这种高效简洁的时间表示方法被称为“Unix时间纪元”,向左和向右偏移均可以获得更早或者更后的时间。对象
在实际开发中,对日期和时间的操做场景很是多,例如程序启动和退出的时间,程序执行任务的时间,数据生成的时间,数据处理的各环节的时间等等。blog
在Linux系统中,自定义了time_t类型,以下:图片
typedef long time_t; // 时间值time_t为长整型long的别名。
取操做系统的时间,并把整数表示的时间转换为字符串表示的格式。
函数声明:
void LocalTime(char *out_stime,const char *in_fmt=0,const int in_interval=0);
参数说明:
stime:用于存放获取到的时间字符串。
timetvl:时间的偏移量,单位:秒,0是缺省值,表示当前时间,30表示当前时间30秒以后的时间点,-30表示当前时间30秒以前的时间点。
fmt:输出时间的格式,fmt每部分的含义:"yyyy"-年份;"mm"-月份;"dd"-日期;"hh24"-小时;"mi"-分钟;"ss"-秒,缺省是"yyyy-mm-dd hh24:mi:ss",目前支持如下格式:
"yyyy-mm-dd hh24:mi:ss" "yyyymmddhh24miss" "yyyy-mm-dd" "yyyymmdd" "hh24:mi:ss" "hh24miss" "hh24:mi" "hh24mi" "hh24" "mi"
注意:
1)小时的表示方法是hh24,不是hh,这么作的目的是为了保持与数据库的时间表示方法一致;
2)以上列出了经常使用的时间格式,若是不能知足您应用开发的需求,请修改源代码timetostr函数增长更多的格式支持;
3)调用函数的时候,若是fmt与上述格式都匹配,stime的内容将为空。
示例(demo24.cpp)
/* * 程序名:demo24.cpp,此程序演示freecplus框架中LocalTime时间函数的使用。 * 做者:C语言技术网(www.freecplus.net) 日期:20190525 */ #include "../_freecplus.h" int main() { char strtime[20]; memset(strtime,0,sizeof(strtime)); LocalTime(strtime,"yyyy-mm-dd hh24:mi:ss",-30); // 获取30秒前的时间。 printf("strtime1=%s\n",strtime); LocalTime(strtime,"yyyy-mm-dd hh24:mi:ss"); // 获取当前时间。 printf("strtime2=%s\n",strtime); LocalTime(strtime,"yyyy-mm-dd hh24:mi:ss",30); // 获取30秒后的时间。 printf("strtime3=%s\n",strtime); }
函数声明:
void timetostr(const time_t ltime,char *stime,const char *fmt=0);
参数说明:
ltime:整数表示的时间。
stime:字符串表示的时间。
fmt:输出字符串时间stime的格式,与LocalTime函数的fmt参数相同,若是fmt的格式不正确,stime将为空。
函数声明:
time_t strtotime(const char *stime);
参数说明:
stime:字符串表示的时间,格式不限,但必定要包括yyyymmddhh24miss,一个都不能少。
返回值:整数表示的时间,若是stime的格式不正确,返回-1。
示例(demo26.cpp)
/* * 程序名:demo26.cpp,此程序演示freecplus框架中整数表示的时间和字符串表示的时间之间的转换。 * 做者:C语言技术网(www.freecplus.net) 日期:20190525 */ #include "../_freecplus.h" int main() { time_t ltime; char strtime[20]; memset(strtime,0,sizeof(strtime)); strcpy(strtime,"2020-01-01 12:35:22"); ltime=strtotime(strtime); // 转换为整数的时间 printf("ltime=%ld\n",ltime); // 输出ltime=1577853322 memset(strtime,0,sizeof(strtime)); timetostr(ltime,strtime,"yyyy-mm-dd hh24:mi:ss"); // 转换为字符串的时间 printf("strtime=%s\n",strtime); // 输出strtime=2020-01-01 12:35:22 }
把字符串表示的时间加上一个偏移的秒数后获得一个新的字符串表示的时间。
函数声明:
bool AddTime(const char *in_stime,char *out_stime,const int timetvl,const char *fmt=0);
参数说明:
in_stime:输入的字符串格式的时间。
out_stime:输出的字符串格式的时间。
timetvl:须要偏移的秒数,正数日后偏移,负数往前偏移。
fmt:输出字符串时间out_stime的格式,与LocalTime函数的fmt参数相同。
注意:in_stime和out_stime参数能够是同一个变量的地址,若是调用失败,out_stime的内容会清空。
返回值:true-成功,false-失败,若是返回失败,能够认为是in_stime的格式不正确。
示例(demo28.cpp)
/* * 程序名:demo28.cpp,此程序演示freecplus框架中采用AddTime进行时间的运算。 * 做者:C语言技术网(www.freecplus.net) 日期:20190525 */ #include "../_freecplus.h" int main() { time_t ltime; char strtime[20]; memset(strtime,0,sizeof(strtime)); strcpy(strtime,"2020-01-01 12:35:22"); AddTime(strtime,strtime,0-1*24*60*60); // 减一天。 printf("strtime=%s\n",strtime); // 输出strtime=2019-12-31 12:35:22 AddTime(strtime,strtime,2*24*60*60); // 加两天。 printf("strtime=%s\n",strtime); // 输出strtime=2020-01-02 12:35:22 }
CTimer类是一个精确到微秒的计时器。
类声明:
// 这是一个精确到微秒的计时器。 class CTimer { private: struct timeval m_start; // 开始计时的时间。 struct timeval m_end; // 计时完成的时间。 // 开始计时。 void Start(); public: CTimer(); // 构造函数中会调用Start方法。 // 计算已逝去的时间,单位:秒,小数点后面是微秒。 double Elapsed(); };
CTimer建立对象后当即开始计时,每次调用Elapsed方法获取已逝去的时间(单位:秒,小数点后面是微秒),并从新开始计时。
示例(demo29.cpp)
/* * 程序名:demo29.cpp,此程序演示freecplus框架中的CTimer类(计时器)的用法。 * 做者:C语言技术网(www.freecplus.net) 日期:20190525 */ #include "../_freecplus.h" int main() { CTimer Timer; printf("elapsed=%lf\n",Timer.Elapsed()); sleep(1); printf("elapsed=%lf\n",Timer.Elapsed()); sleep(1); printf("elapsed=%lf\n",Timer.Elapsed()); usleep(1000); printf("elapsed=%lf\n",Timer.Elapsed()); usleep(100); printf("elapsed=%lf\n",Timer.Elapsed()); sleep(10); printf("elapsed=%lf\n",Timer.Elapsed()); }
运行效果
从demo29运行的效果上看,好像计时有偏差,一样是睡1秒,实际耗时倒是1.000126或1.000171,这是由于程序自己执行须要时间,虽然时间很短,那也是须要时间。
C语言技术网原创文章,转载请说明文章的来源、做者和原文的连接。
来源:C语言技术网(www.freecplus.net)
做者:码农有道
若是这篇文章对您有帮助,请点赞支持,或在您的博客中转发个人文章,谢谢!!!若是文章有错别字,或者内容有错误,或其余的建议和意见,请您留言指正,很是感谢!!!