What is the difference between clock_t, time_t and struct tm?

前端 未结 3 621
花落未央
花落未央 2021-02-05 07:06

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;         


        
3条回答
  •  Happy的楠姐
    2021-02-05 07:48

    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.

提交回复
热议问题