How do C++ get the current system time and formatted output

  • 2020-07-21 09:31:45
  • OfStack

In this paper, time() and strftime() functions are used to achieve c++ to obtain system time.

C++ system and time dependent functions are basically standard interfaces provided by the C language

Getting system time in a program is a common operation, and in many cases can be done using the system-provided time function.

time() is the standard interface for the system C language. The detailed usage can be viewed 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 ES22en. cpp and compile with g++ command under linux:

g++timetest.cpp

a. out will be generated if it succeeds, and the output can be seen after executing the command:

[

./a.out

]

This is the most commonly used method, but there are other functions that can be used. 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

Using the time function above to get the time returns the structure of tm, which we usually need to convert to a string.

This is where strftime comes in handy. This function is often used to format 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 */

Use the same as the above compilation, runtime in and out time format:


$ ./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.

Shorthand for the day of the week

Full name of the day of the week

Short for %b per month

Full name of the month

c standard date time string

The last two digits of the year

The day of the month in decimal system

%D month/day/year

The day of the month in decimal in the two-character field

Year - month - day

The last two digits of the g year, using years based on weeks

Year points, using years based on weeks

The name of the month

A 24-hour system of hours

%I 12 hours

The day of the year in decimal notation

%m 10 decimal representation of the month

%M 10 minutes

% n new line operator

%p Local AM or PM equivalent display

%r 12 hours

%R displays hours and minutes: hh:mm

%S in decimal seconds

%t horizontal TAB

%T display time: hh:mm:ss

Day of the week, Day 1 is day 1 (value 0 to 6, day 1 is 0)

Weeks of the year, with Sunday as the first day (values from 0 to 53)

The week of the year, used based on the week of the year

%w the day of the week in decimal (value 0 to 6, 0 on Sunday)

Week of the year, with Monday as the first day (values from 0 to 53)

x standard date string

X standard time string

%y 10 decimal years without century (values 0 to 99)

10 system years of the century part

%z, %Z time zone name, returns null if the time zone name cannot be obtained.

% % percent

Here is a 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;
}

conclusion


Related articles: