freecplus framework - date, time and timer

Posted by ow-phil on Mon, 20 Apr 2020 18:04:25 +0200

@[toc]

1, Source code description

Freecplus is a C/C + + open source framework under Linux system. Please go to the C language technology network (www.freecplus.net) to download the source code.

This paper introduces the time operation function of freecplus framework.

The declaration file for functions and classes is freecplus / 65124; freecplus. H.

The definition file for functions and classes is freecplus / 65124; freecplus.cpp.

The sample program is located in the freecplus/demo directory.

The compile rule file is freecplus/demo/makefile.

2, Representation of computer time

The UNIX operating system uses January 1, 1970 as the epoch time of UNIX according to the age and application of the computer, and 0:00 on January 1, 1970 as the middle point to represent the time of the computer. It stores the number of seconds from January 1, 1970 as an integer. This efficient and concise time representation method is called "UNIX epoch". The left and right offsets can get earlier Or later.

In the actual development, there are many operation scenarios for date and time, such as the time of program start and exit, the time of program task execution, the time of data generation, the time of each link of data processing and so on.

In the Linux system, the time ﹣ T type is customized as follows:

typedef long time_t;    // The time value "time" is an alias of long.

3, Get operating system time

Take the time of the operating system, and convert the time represented by integer to the format represented by string.

Function declaration:

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

Parameter Description:

stime: used to store the obtained time string.

timetvl: the offset of time, in seconds. 0 is the default value, indicating the current time, 30 indicating the time point 30 seconds after the current time, and - 30 indicating the time point 30 seconds before the current time.

fmt: output time format. The meaning of each part of fmt: "yyyy" - year; "mm" - month; "dd" - date; "hh24" - hour; "mi" - minute; "ss" - second. The default is "yyyy MM dd hh24: mi: ss". Currently, the following formats are supported:

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

be careful:

1) The representation method of hours is hh24, not HH. The purpose of this is to keep consistent with the time representation method of database;

2) The commonly used time formats are listed above. If they can not meet your application development needs, please modify the source code timetostr function to add more format support;

3) When the function is called, if fmt matches all the above formats, the contents of stime will be empty.

Example (demo24.cpp)

/*
 *  Program name: demo24.cpp, this program demonstrates the use of LocalTime time function in freecplus framework.
 *  Author: C language technology network (www.freecplus.net) date: 20190525
*/
#include "../_freecplus.h"

int main()
{
  char strtime[20];
  memset(strtime,0,sizeof(strtime));

  LocalTime(strtime,"yyyy-mm-dd hh24:mi:ss",-30);  // Gets the time 30 seconds ago.
  printf("strtime1=%s\n",strtime);

  LocalTime(strtime,"yyyy-mm-dd hh24:mi:ss");      // Gets the current time.
  printf("strtime2=%s\n",strtime);

  LocalTime(strtime,"yyyy-mm-dd hh24:mi:ss",30);   // Gets the time after 30 seconds.
  printf("strtime3=%s\n",strtime);
}

4, Time conversion function

1. Convert the time represented by an integer to the time represented by a string

Function declaration:

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

Parameter Description:

ltime: the time represented by an integer.

stime: the time represented by the string.

fmt: the format of the output string time stime is the same as the fmt parameter of the LocalTime function. If the format of fmt is incorrect, stime will be empty.

2. Convert string time to integer time

Function declaration:

time_t strtotime(const char *stime);

Parameter Description:

stime: there is no limit to the time and format of string representation, but it must include yyyymmddh24miss, no less.

Return value: the time represented by an integer. If the format of stime is incorrect, return - 1.

Example (demo26.cpp)

/*
 *  Program name: demo26.cpp, which demonstrates the conversion between integer time and string time in freecplus framework.
 *  Author: C language technology network (www.freecplus.net) date: 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);    // Time to integer
  printf("ltime=%ld\n",ltime); // Output ltime=1577853322

  memset(strtime,0,sizeof(strtime));
  timetostr(ltime,strtime,"yyyy-mm-dd hh24:mi:ss");  // Time converted to string
  printf("strtime=%s\n",strtime);     // Output strtime=2020-01-01 12:35:22
}

5, Operation of time

Add the time represented by a string to the number of seconds offset to get a new time represented by a string.

Function declaration:

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

Parameter Description:

in_stime: the time in the format of the entered string.

Out time: the time in the output string format.

timetvl: the number of seconds to be offset. The positive number is offset backward, and the negative number is offset forward.

fmt: the format of the output string time out ﹣ stime is the same as the fmt parameter of the LocalTime function.

Note: the in stime and out stime parameters can be the addresses of the same variable. If the call fails, the contents of out stime will be cleared.

Return value: true success, false failure. If the return fails, it can be considered that the format of in ume is incorrect.

Example (demo28.cpp)

/*
 *  Program name: demo28.cpp. This program demonstrates the operation of time using AddTime in freecplus framework.
 *  Author: C language technology network (www.freecplus.net) date: 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); // One day less.
  printf("strtime=%s\n",strtime);     // Output strtime=2019-12-31 12:35:22

  AddTime(strtime,strtime,2*24*60*60); // Plus two days.
  printf("strtime=%s\n",strtime);     // Output strtime=2020-01-02 12:35:22
}

6, Timer

The CTimer class is a timer that is accurate to microseconds.

Class declaration:

// This is a timer accurate to microseconds.
class CTimer
{
private:
  struct timeval m_start;   // Time to start timing.
  struct timeval m_end;     // Time to complete.

  // Start timing.
  void Start();
public:
  CTimer();  // The Start method is called in the constructor.

  // Calculate the elapsed time in seconds, followed by microseconds.
  double Elapsed();
};

After the CTimer creates the object, it starts timing immediately. Each time it calls the Elapsed method to get the Elapsed time (in seconds, microseconds after the decimal point), and starts timing again.

Example (demo29.cpp)

/*
 *  Program name: demo29.cpp, which demonstrates the use of the CTimer class (timer) in the freecplus framework.
 *  Author: C language technology network (www.freecplus.net) date: 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());
}

Operation effect

From the point of view of the effect of demo29 operation, it seems that there is a timing error. It is also a second of sleep, but the actual time consumption is 1.000126 or 1.000171. This is because the execution of the program itself takes time. Although the time is very short, it also takes time.

7, Copyright notice

C language technology net original article, reprint please explain the article source, the author and the original link.
Source: C language technology network (www.freecplus.net)
Author: Manon Youdao

If this article is helpful to you, please like support or forward my article in your blog, thank you!!!
If there is a mistake in the article, or there is a mistake in the content, or other suggestions and opinions, please leave a message for correction, thank you very much!!!

Topics: C++ C network Unix Linux