Common C ++ Programming - Get the current system time

Common C ++ Programming - Get the current system time
articles starting https://www.cppentry.com

In this paper, using the time () and strftime () function

C ++ and time-dependent system functions are basically using C language provides a standard interface
system time is a common operation in the program, the system used in many cases to provide time acquisition function.

time () is a standard C language interface to the system, or by man time man 2 time can view the detailed usage.

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, use the command g ++ compiler under linux:

G ++ timetest.cpp

Success will generate a.out, execute the command to see the output:

./a.out

This is the most common method, of course, may be used in addition to other functions, refer to the following functions, not described in detail here.

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

String processing time
after the acquisition time using the above time function returns a structure of tm, we usually need to use to convert strings embodiment.

Strftime may come in handy case, the function used to time and date format.

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char argv[])
{
char outstr[200];
time_t t;
struct tm
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 */

Using the above method to compile, run out of 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, detailed below, need to pay attention Oh capitalization:

% A abbreviated weekday

% A week's full name

% B abbreviated month of

% B Full name of month

Time series Date% c Standard

% C last two digits of the year

% D day of the month in decimal notation

% D month / day / year

The first few days of each month in the two-character domain% e, decimal notation

% F year - month - day

% G last two digits of the year, based on weeks

Year% G, based on weeks

% H abbreviated month name

% H 24-hour clock

% I 12 hour clock

% J day of the decimal representation of a year

January% m decimal notation

% M when ten minutes system represented by

% N newline

% P local AM or PM display equivalent

% R 12 hours

% R displays hours and minutes: hh: mm

% S decimal seconds

% T horizontal tab

% T display minutes and seconds: hh: mm: ss

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

% U week number of the year, the Sunday as the first day (value from 0-53)

% V week number of year, based on weeks

% W weeks several decimal notation (a value from 0 to 6, Sunday is 0)

% W week number of year, Monday as the first day (value from 0-53)

% X standard date string

Standard time series% X

Decimal% y year without century (values ​​from 0 to 99)

% Y lug ten years made part of the century

% Z,% Z time zone name, the name of a time zone can not be obtained if the space character is returned.

%% percent sign

Common C ++ Programming - Get the current system time https://www.cppentry.com/bencandy.php?fid=49&id=265713
programming information https://www.cppentry.com

Guess you like

Origin www.cnblogs.com/cppentry/p/12364644.html