Linux programming environment time and date type timeval and related APIs
The concepts of "time" and "date" mainly include the following:
Coordinated Universal Time (UTC): coordinates the Universal Time, also known as the world standard time, that is, the well-known Greenwich Mean Time (GMT ). For example, the time difference between mainland China and UTC is + 8, that is, UTC + 8. America is UTC-5.
Calendar time: indicates the calendar time, which is expressed by the number of seconds elapsed from a standard time point to the current time. This standard time point varies with compilers, but for a compilation system, this standard time point remains unchanged, the calendar time corresponding to the time in the compilation system is measured by the standard time point. Therefore, it can be said that the calendar time is relative time, But no matter which time zone you are in, the calendar time is the same for the same standard time point at the same time point.
EPOCH: time point. The time point is an integer in the Standard C/C ++, which is expressed by the number of seconds (calendar time) between the current time and the standard time point.
Clock tick: A clock timing unit (instead of a clock tick). A clock tick is not a clock cycle of a CPU, but a basic unit of time for C/C ++.
We can use the time. h header file in the ANSI standard library. The method used to define the time and date in this header file, whether in the structure definition or naming, has a clear C language style. Next, I will explain how to use the date function in C/C ++.
Struct timeval
{
Time_t TV _sec; // second
Susenconds_t TV _usec; // microsecond 10 E-6 seconds
}
The benchmark calculation time is UTC (World Standard Time), January 1, January 1, 1970 (EPOCH)
The time (calendar time) represented by time_t is the number of seconds from a time point (for example, January 1, 1970 00:00:00) to this time point. In time. H, we can also see that time_t is a long integer:
# Ifndef _ time_t_defined
Typedef long time_t;/* Time Value */
# DEFINE _ time_t_defined/* avoid repeatedly defining time_t */
# Endif
You may have questions: Since time_t is actually a long integer, the number of seconds from a time point (usually January 1, 1970 00:00:00) to that day (that is, the calendar time) what should I do if it is out of the range of numbers that long integer can represent? For a value of the time_t data type, the time it represents cannot be later than January 18, 2038. To represent a longer period of time, some compiler vendors have introduced 64-bit or even longer integer numbers to save the calendar time. For example, Microsoft uses the _ time64_t data type in Visual C ++ to save the calendar time, and uses _ time64 () function to obtain the calendar time (instead of using the 32-bit time () function), you can use this data type to save the time before 00:00:00, January 1, January 1, 3001 (excluding this time point.
The following code demonstrates how to obtain the current system time and convert it to a string:
Timeval TV;
Gettimeofday (& TV, null );
STD: cout <ctime (& TV. TV _sec) <Endl; // converts the number of seconds to the string representation of the current time.
TM and related APIs
Time (obtain the current time)
Header file # include <time. h>
Defines the function time_t time (time_t * t );
Function Description: This function returns the number of seconds that have elapsed since 00:00:00 UTC on January 1, January 1, 1970 AD. If t is not a null pointer, this function also saves the returned value to the memory indicated by T pointer.
If the return value is successful, the number of seconds is returned. If the return value is failed (time_t)-1), the error cause is stored in errno.
The structure TM is defined
Struct TM
{
Int tm_sec; // indicates the current number of seconds. The normal range is 0-59, but the value can be 61 seconds.
Int tm_min; // indicates the current score. The value ranges from 0 to 59.
Int tm_hour; // The number of hours from midnight, ranging from 0 to 23
Int tm_mday; // the number of days of the current month, range: 01-31
Int tm_mon; // indicates the current month. The value ranges from 0 to 11 from January 1, January.
Int tm_year; // The number of years since January 1, 1900.
Int tm_wday; // the number of days in a week. The value ranges from 0 to 6 from Monday.
Int tm_yday; // The number of days since January 1, January 1 this year, ranging from 0 to 365.
Int tm_isdst; // time-saving flag for daylight saving
};
Example:
Time_t sec = time (0); // obtain the current time in seconds (UTC)
Struct TM tmbuf;
Localtime_r (& sec, & tmbuf); // converts the number of seconds to the local time zone
GCC compilation command
View gcc-V version
Find the Command help of G ++ and type info g ++. The GCC help information is displayed. Most of the parameters of G ++ are the same as those of GCC.
Option description
-ANSI only supports the ANSI standard C syntax. This option will disable some features of gnu c,
For example, ASM or typeof keywords.
-C-o only compiles and generates the target file.
-Dmacro defines the macro with the string "1.
-Dmacro = defn: Define the macro with the string "defn.
-E only runs the C pre-compiler.
-G generates debugging information. The GNU Debugger can use this information.
-Idirectory: specify an additional header file to search for the path directory.
-Ldirectory: specify an additional function library to search for the path directory.
-Search for the specified library when connecting to llibrary.
-Msung optimizes code for 486.
-O file: generate the specified output file. Used to generate executable files.
-O0 is not optimized.
-O or-O1 optimized code generation.
-O2 is further optimized.
-O3 is further optimized than-O2, including the inline function.
-Shared shared object generation. It is usually used to create a shared library.
-Static prohibit the use of shared connections.
-Umacro undefines macro macros.
-W does not generate any warning information.
-Wall generates all warning information.
G ++-O target file name source file name
LDD programm view the Shared Library (. So) file on which the programm program depends
Ar CRV mm. a x. o y. O combines X. O and Y. O into a static library mm. A file.
Ranlib mm. A generate the content table of the static library (not required)
Emacs basic commands
CTRL + x + F open the file
CTRL + x + D open the Directory and view its subdirectories
CTRL + x + S save the file in the Current Buffer
CTRL + s find the text in the Current Buffer
Rename-buffer modify the name of the Current Buffer
CTRL + Shift +-rollback
CTRL + ALT +/format the selected code
CTRL + x loosen and press o to move the focus in different buffers
Alt + x selected Command Buffer
CTRL + g cancel current command
Alt + W copy the selected code
CTRL + y paste code
CTRL + W cut code
CTRL + x loose and press backspace to set the breakpoint
CTRL + x + k close the Current Buffer
GDB debugging command
View GDB help information
Note that the-G command is used during G ++ compilation.
Name of the program file compiled by GDB
List display source code
The break command can be followed by a function name to set a breakpoint.
Delete breakpoints delete all breakpoints
Run the program
Step <count> one-step tracking (automatically enters the function)
Next <count> single-step tracking (not automatically entering the function)
Continue running
Print variable name to view the value of a variable. The value of a variable is changed during debugging.
Info breakpoints
Quit exit
Info thread lists multiple thread numbers. Thread to check the current thread number to select which thread to enter for debugging.
Conversion of Unicode and multibytes strings
# Include <wchar. h>
Size_t wcsrtombs (char * DEST, const wchar_t ** SRC, size_t Len, mbstate_t * PS );
# Include <stdlib. h>
Size_t mbstowcs (wchar_t * pwcs, const char * s, size_t N );
Environment Variable
View environment variables
Env
Env can also be used to set the value of environment variables. For details, see env -- help.
Set can also be used to set environment variables.
If the environment variable is set in a terminal, it is only valid for this terminal
Modify global settings
/Etc/profile file