How can C + + obtain the current system time and format the output

Time:2021-10-20

This paper mainly uses time () and strftime () functions to achieve c + + to obtain the system time.

C + + system and time-related functions basically use the standard interfaces provided by C language

It is a common operation to obtain the system time in the program. In many cases, it can be obtained by using the time function provided by the system.

Time () is the standard interface of system C language. You can view the detailed usage through man time or man 2 time.


include <time.h>
include <stdio.h>
int main()
{
time_t tt = time(NULL);
tm* t=localtime(&tt);
printf("%d-%02d-%02d %02d:%02d:%02d\n",
t->tm_year + 1900,
t->tm_mon + 1,
t->tm_mday,
t->tm_hour,
t->tm_min,
t->tm_sec);
}

Save the file as timetest.cpp and compile it with G + + command under Linux:

g++timetest.cpp

A.out will be generated successfully, and the output can be seen by executing the command:

./a.out

This is the most commonly used method. Of course, there are other functions to use. Please refer to the following functions, which will not be detailed here.

date(1),gettimeofday(2), ctime(3), ftime(3), time(7)

Time string processing

After the time function above is used to obtain the time, the structure of TM is returned. Usually, we need to convert it into a string.

In this case, strftime can be used. This function is often used to format the time and date.


#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char 
argv[])
{
char outstr[200];
time_t t;
tmp;
t =time(NULL);
tmp =localtime(&t);
if (tmp == NULL) {
perror("localtime");
exit(EXIT_FAILURE);
}
if (strftime(outstr, sizeof(outstr), argv[1], tmp) == 0) {
fprintf(stderr, "strftime returned0");
exit(EXIT_FAILURE);
}
printf("Result string is "%s"\n", outstr);
exit(EXIT_SUCCESS);
} /* main */

The use method is the same as that of compiling, and the format of access time at runtime is as follows:


$ ./a.out '%m'
   Result string is "11"
    $./a.out '%5m'
   Result string is "00011"
    $./a.out '%_5m'
   Result string is "  11"

Strftime supports a variety of date and time formats. The details are as follows. You should pay attention to case:

%A short for day of week

%A full name of the day of the week

%B. abbreviation of month minute

%Full name of month B

%C standard date time series

%C last two digits of the year

%D the day ordinal of the month in decimal

%D month / day / year

%E in the two character field, the decimal day of the month

%F year month day

%G the last two digits of the year, using the week based year

%G year minute, using week based year

%H abbreviated month name

%H 24 hours

%I 12 hour system

%J decimal day of the year

%M decimal month

%Minutes in M ten hour system

%N new line character

%P local am or PM equivalent display

%R 12 hours

%R displays hours and minutes: HH: mm

%S decimal seconds

%T horizontal tab

%T displays hours, minutes and seconds: HH: mm: SS

%U the day of the week, Monday is the first day (value from 0 to 6, Monday is 0)

%The week of the year u, with Sunday as the first day (value from 0 to 53)

%V the week ordinal of each year, using the week based year

%W decimal day of the week (values from 0 to 6, Sunday is 0)

%W the week ordinal of each year, with Monday as the first day (value from 0 to 53)

%X standard date string

%X standard time series

%Y decimal year without Century (values from 0 to 99)

%Decadal year with century part of Y belt

%z. % Z time zone name. If the time zone name cannot be obtained, null characters will be returned.

%%Percent sign

Let’s look at the code c + + to get the current system time and format the output


#include <string>
#include <time.h>
using namespace std;
string getTime()
{
  time_t timep;
  time (&timep);
  char tmp[64];
  strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S",localtime(&timep) );
  return tmp;
}
int main(){
  string  time = getTime();
  cout << time << endl;
  return 0;
}

summary

This is the end of this article about how C + + obtains the current system time and formatted output. For more information about C + + obtaining system time, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!