What is the difference between clock_t, time_t and struct tm?
struct tm looks like this:
struct tm{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
But how does clock_t and time_t look like?
time_t
is an absolute time, represented as the integer number of seconds since the UNIX epoch (midnight GMT, 1 January 1970). It is useful as an unambiguous, easy-to-work-with representation of a point in time.
clock_t
is a relative measurement of time, represented by an integer number of clock ticks since some point in time (possibly the computer's bootup, but no guarantees, as it may roll over quite often). There are CLOCKS_PER_SEC
clock ticks per second; the value of this constant can vary between different operating systems, but it is usually around 100. It is sometimes used for timing purposes, but it is not very good at it due to its relatively low resolution. gettimeofday
's struct timeval
is much better for timing purposes.
struct tm
is a calendar date and time. It may not represent any real point in time (e.g, you can have a struct tm
that says it is February 31st, or Dodecember 0st). It does not include a time zone, so it is not absolute. It is typically used when converting to or from human-readable representations of the date and time.
time_t
represents the current time -- normally the number of seconds since some epoch (e.g., midnight, 1 Jan, 1970). It's intended to represent calendar/wall clock time, but still be easy to manipulate as a single, simple arithmetic type (e.g., difftime
can find the difference between two specified times).
clock_t
represents an amount of CPU time used since a process was started. It can be converted to seconds by dividing by CLOCKS_PER_SEC. Its real intent is to represent CPU time used though, not calendar/wall clock time.
struct tm
is a structure (with specified members) that represents a calendar/wall clock time broken down into components -- year, month, day, hour, minute, second, etc. It's intended (primarily) as an external interface, while a time_t
is intended primarily for internal use -- i.e., typical use is that when you get a date/time from the outside world, you put the components into a struct tm
and convert it to a time_t
for internal storage. Then, when you need to do something like displaying a time/date, you convert the time_t
to a struct tm
. The routines that do that manipulation include a fair amount of intelligence to do things like normalizing the dates, so a date like 30 February
would be converted to 2 March
(or in a leap year, 1 March
).
C just says that:
(C99, 7.23.1p3) "clock_t and time_t which are arithmetic types capable of representing times"
In the glibc
, the type time_t
is an alias to long
.
In the GNU C library, time_t is equivalent to long int
from http://www.gnu.org/software/libc/manual/html_node/Simple-Calendar-Time.html
来源:https://stackoverflow.com/questions/12240059/what-is-the-difference-between-clock-t-time-t-and-struct-tm