Date & Time
There are four time-related types: clock_t, time_t, size_t and tm . Type clock_t, size_t time_t and the system time and date can be represented as a certain integer. Structure type tm the date and time stored in the form C structure, tm structure defined as follows:
struct tm {
int tm_sec; // 秒,正常范围从 0 到 59,但允许至 61
int tm_min; // 分,范围从 0 到 59
int tm_hour; // 小时,范围从 0 到 23
int tm_mday; // 一月中的第几天,范围从 1 到 31
int tm_mon; // 月,范围从 0 到 11
int tm_year; // 自 1900 年起的年数
int tm_wday; // 一周中的第几天,范围从 0 到 6,从星期日算起
int tm_yday; // 一年中的第几天,范围从 0 到 365,从 1 月 1 日算起
int tm_isdst; // 夏令时
}
Use date and time functions and structures need to reference <ctime> header file in a C ++ program. C ++ standard library operations functions Date and time:
function | description |
time_t time(time_t *t) | Returns the timestamp, if the parameter is not null * t, the return value is also stored in the memory space a pointer pointing to t, if there is no current system time, -1 |
char *ctime(const time_t *timer) | Returns a string representing local time, local time is based on the parameters of the Timer . Returns the string format is as follows: Www Mmm dd HH: mm: SS yyyy where, Www represents a day of the week, Mmm is a month of letters, dd represents the day of the month, HH: mm: SS indicates the time, yyyy indicate the year. |
struct tm *localtime(const time_t *timer) | A timer value used to fill tm structure. timer value is decomposed into tm structure, and is represented by local time zone. |
clock_t clock(void) | Since initiating the program returns, the time used by the processor clock. If it fails, it returns a value of -1. |
char *asctime(const struct tm *timeptr) | C returns a string containing a readable format date and time information Www Mmm dd HH: mm: SS yyyy , where, Www represents a day of the week, Mmm is a month of letters, dd represents the day of the month , HH: mm: SS indicates the time, yyyy represents the year. |
struct tm *gmtime(const time_t *timer) | This function returns a pointer pointing to time, time is tm structure, with Coordinated Universal Time (UTC), also known as Greenwich Mean Time (GMT) representation. |
time_t mktime(struct tm *timeptr) | Time_t returns a value that corresponds to the calendar time to the transmission parameters. If an error occurs, a value of -1 is returned. |
double difftime(time_t time1, time_t time2) | Returns the seconds of time to phase difference between two double precision floating-point value representation (time1 - time2) |
size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr) | The format formatting rules defined in the formatting structure timeptr represents time, and stores it in the str in. If C is smaller than the size string produced characters (characters including null terminator), it returns to the copy number of characters in str (not including null terminator character), otherwise, returns zero. |
The current date & time and time format tm
#include <iostream>
#include <ctime>
using namespace std;
int main( )
{
// 基于当前系统的当前日期/时间
time_t now = time(0);
// 把 now 转换为字符串形式
char* dt = ctime(&now);
cout << "本地日期和时间:" << dt << endl;
// 把 now 转换为 tm 结构
tm *gmtm = gmtime(&now);
dt = asctime(gmtm);
cout << "UTC 日期和时间:"<< dt << endl;
// 输出 tm 结构的各个组成部分
cout << "年: "<< 1900 + gmtm->tm_year << endl;
cout << "月: "<< 1 + gmtm->tm_mon<< endl;
cout << "日: "<< gmtm->tm_mday << endl;
cout << "时间: "<< gmtm->tm_hour << ":";
cout << gmtm->tm_min << ":";
cout << gmtm->tm_sec << endl;
}
tm time structure with dates and time-related operations in C / C ++, a particularly important. Arrow -> operator to access the structure members.
input Output
The C ++ I / O occurs in the stream, the stream is a sequence of bytes. If the flow stream of bytes from the memory device (e.g., keyboard, disk drive, network connection, etc.), which is called the input operation . If the byte stream flowing from the memory device (e.g., display screen, printer, disk drive, network connection, etc.), which is called the output operation .
I / O library header files
head File | description |
<iostream> | This document defines the CIN ( the standard input stream ), cout ( the standard output stream ), cerr, (unbuffered standard error stream) and the clog (standard error buffer stream) object |
<iomanip> | The document by so-called parametric flow manipulator (such setw and setPrecision ), to declare useful for implementation of standardized I / O services |
<fstream> | The file for the user control file processing service statement |
Standard input stream cin
Predefined object cin is iostream an instance of a class. cin objects affiliated to the standard input device, typically a keyboard. cin is the stream extraction operator >> used in combination, the stream extraction operator >> can be used repeatedly in a statement
C ++ compiler according to the data type of the input values to select the appropriate stream extraction operator to extract value, and stores it in a given variable.
Standard output stream cout
Predefined object cout is iostream an instance of a class. cout object "connected" to the standard output device, usually a display screen. cout is a stream insertion operator << using binding, flow insertion operator << can be used repeatedly in a statement.
C ++ compiler according to the data type of the variable to be output, select the appropriate stream insertion operator to display the value. << operator is overloaded output built-in type (integer, float, double, string and pointer) data items.
Output stream function (template):
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout<<setiosflags(ios::left|ios::showpoint); // 设左对齐,以一般实数方式显示
cout.precision(5); // 设置除小数点外有五位有效数字
cout<<123.456789<<endl;
cout.width(10); // 设置显示域宽10
cout.fill('*'); // 在显示区域空白处用*填充
cout<<resetiosflags(ios::left); // 清除状态左对齐
cout<<setiosflags(ios::right); // 设置右对齐
cout<<123.456789<<endl;
cout<<setiosflags(ios::left|ios::fixed); // 设左对齐,以固定小数位显示
cout.precision(3); // 设置实数显示三位小数
cout<<999.123456<<endl;
cout<<resetiosflags(ios::left|ios::fixed); //清除状态左对齐和定点格式
cout<<setiosflags(ios::left|ios::scientific); //设置左对齐,以科学技术法显示
cout.precision(3); //设置保留三位小数
cout<<123.45678<<endl;
return 0;
}
Which cout.setf with setiosflags the same, cout.precision like setprecision, cout.unsetf like resetiosflags.
setiosflags(ios::fixed) 固定的浮点显示
setiosflags(ios::scientific) 指数表示
setiosflags(ios::left) 左对齐
setiosflags(ios::right) 右对齐
setiosflags(ios::skipws 忽略前导空白
setiosflags(ios::uppercase) 16进制数大写输出
setiosflags(ios::lowercase) 16进制小写输出
setiosflags(ios::showpoint) 强制显示小数点
setiosflags(ios::showpos) 强制显示符号
cout.setf common signs:
Mark | Features |
---|---|
boolalpha | You can use the word "true" and "false" input / output Boolean value. |
oct | Display value in octal format. |
dec | Displayed value in decimal format. |
hex | Display values in hexadecimal format. |
left | Output justified on the left. |
right | Adjust the alignment for the right output. |
scientific | Display floating point numbers using scientific notation. |
fixed | 用正常的记数方法显示浮点数(与科学计数法相对应). |
showbase | 输出时显示所有数值的基数. |
showpoint | 显示小数点和额外的零,即使不需要. |
showpos | 在非负数值前面显示”+(正号)”. |
skipws | 当从一个流进行读取时,跳过空白字符(spaces, tabs, newlines). |
unitbuf | 在每次插入以后,清空缓冲区. |
internal | 将填充字符回到符号和数值之间. |
uppercase | 以大写的形式显示科学记数法中的”e”和十六进制格式的”x”. |
非缓冲标准错误流 cerr
预定义的对象 cerr 是 iostream 类的一个实例。cerr 对象附属到标准错误设备,通常也是显示屏,但是 cerr 对象是非缓冲的,且每个流插入到 cerr 都会立即输出。cerr 也是与流插入运算符 << 结合使用的
缓冲标准错误流 clog
预定义的对象 clog 是 iostream 类的一个实例。clog 对象附属到标准错误设备,通常也是显示屏,但是 clog 对象是缓冲的。这意味着每个流插入到 clog 都会先存储在缓冲在,直到缓冲填满或者缓冲区刷新时才会输出。clog 也是与流插入运算符 << 结合使用的
所以,使用 cerr 流来显示错误消息,而其他的日志消息则使用 clog 流来输出。