freecplus framework - the date, time and timer

A source code description

freecplus is a C / C ++ open source framework under a Linux system, go to the C language source code Technology Network (www.freecplus.net) download.

This article describes the operation of a function freecplus time frame.

Declaration file functions and classes are freecplus / _freecplus.h.

Definition file functions and classes are freecplus / _freecplus.cpp.

Sample program located freecplus / demo directory.

Compiled rules file is freecplus / demo / makefile.

Second, the computer time representation

UNIX operating system is based on computer-generated application's and January 1, 1970 as the UNIX epoch time at 0:00 on January 1 1970 as a computer representation of time is the middle point, will begin after January 1, 1970 storing by an integer number of seconds, such a simple and efficient method is called time represents "Unix time epoch", left and right offsets may be obtained earlier or later time.

In the actual development, operational scenarios for the date and time are many, such as program start and exit time, time to perform your tasks, and data generation time, all aspects of data processing time and so on.

In the Linux system, the custom of time_t, as follows:

typedef long time_t;    // 时间值time_t为长整型long的别名。

Third, access to the operating system time

Operating system takes time, and the time represented by the integer format is converted to a string.

Function declaration:

void LocalTime(char *out_stime,const char *in_fmt=0,const int in_interval=0);

Parameter Description:

stime: Time for storing the acquired character string.

timetvl: offset time, unit: seconds, 0 is the default value, representing the current time, the point 30 represents the time after the current time of 30 seconds, the current time point -30 for 30 seconds before.

fmt: output time format, FMT meaning of each section: "yyyy" - Year; "mm" - Month; "dd" - the date; "hh24" - h; "mi" - min; "ss" - second, the lack of the province is "yyyy-mm-dd hh24: mi: ss", currently supports the following formats:

  "yyyy-mm-dd hh24:mi:ss"
  "yyyymmddhh24miss"
  "yyyy-mm-dd"
  "yyyymmdd"
  "hh24:mi:ss"
  "hh24miss"
  "hh24:mi"
  "hh24mi"
  "hh24"
  "mi"

note:

1) hour is represented hh24, not hh, The goal is to maintain a consistent approach time representation of the database;

2) the above list of commonly used time format, if you can not meet your application development needs, modify the source code timetostr function to add more format support;

3) when the function is called, if the above format fmt match, stime content will be empty.

Example (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);
}

Fourth, time conversion function

1, the integer time to a time represented by a string

Function declaration:

void timetostr(const time_t ltime,char *stime,const char *fmt=0);

Parameter Description:

ltime: Time integer representation.

stime: time string representation.

fmt: Time stime output string format, with the same function LocalTime fmt parameter, if the format is not correct, fmt, stime will be empty.

2, the time to convert the string representation of the integer time

Function declaration:

time_t strtotime(const char *stime);

Parameter Description:

stime: a string representation of the time, the format is not limited, but must include YYYYMMDDHH24MISS, one less.

Return Value: integer time, and if the format is not correct stime, return -1.

Example (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
}

Fifth, the time of operation

Plus the time represented by the string after the number of seconds to obtain a time offset represented by a new string.

Function declaration:

bool AddTime(const char *in_stime,char *out_stime,const int timetvl,const char *fmt=0);

Parameter Description:

in_stime: Time input string format.

out_stime: Time string format output.

timetvl: number of seconds required offset next positive offset, a negative offset forward.

fmt: Time out_stime output string format, with the same function LocalTime fmt parameter.

Note: in_stime and out_stime same argument can be the address of a variable, if the call fails, the contents of out_stime will be cleared.

Return value: true- success, false- failure, if failure is returned, can be considered in_stime is not formatted correctly.

Example (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
}

Sixth, the timer

CTimer class is a timer accurate to the microsecond.

Class declaration:

// 这是一个精确到微秒的计时器。
class CTimer
{
private:
  struct timeval m_start;   // 开始计时的时间。
  struct timeval m_end;     // 计时完成的时间。

  // 开始计时。
  void Start();
public:
  CTimer();  // 构造函数中会调用Start方法。

  // 计算已逝去的时间,单位:秒,小数点后面是微秒。
  double Elapsed();
};

CTimer start timing immediately after creating an object, method to get each call Elapsed time elapsed (unit: seconds, microseconds after the decimal point), and re-starts.

Example (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());
}

running result

Here Insert Picture Description

In effect demo29 run point of view, there seems to timing error, the same sleep one second, but it is actually time-consuming or 1.000171 1.000126, this is because the program itself performs the required time, although time is very short, and that takes time.

Seven, copyright notice

C Language Technology Network original article, reproduced please indicate the source link to the article, the author and original.
Source: C Language Technology Network (www.freecplus.net)
Author: Ethics code Agriculture

If the article typos, or content errors, or other suggestions and comments, please correct me message, thank you very much! ! !

Published 159 original articles · won praise 458 · views 120 000 +

Guess you like

Origin blog.csdn.net/wucz122140729/article/details/105187292