Chinaunix首页 | 论坛 | 博客
  • 博客访问: 257139
  • 博文数量: 65
  • 博客积分: 2599
  • 博客等级: 少校
  • 技术积分: 710
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-04 10:49
文章分类

全部博文(65)

文章存档

2015年(4)

2013年(2)

2012年(4)

2011年(51)

2010年(4)

分类: LINUX

2011-12-19 22:50:37

1. Data Type
1.1 struct tm
struct tm
{
  int tm_sec;            /* Seconds.    [0-60] (1 leap second) */
  int tm_min;            /* Minutes.    [0-59] */
  int tm_hour;            /* Hours.    [0-23] */
  int tm_mday;            /* Day.        [1-31] */
  int tm_mon;            /* Month.    [0-11] */
  int tm_year;            /* Year    - 1900.  */
  int tm_wday;            /* Day of week.    [0-6] */
  int tm_yday;            /* Days in year.[0-365]    */
  int tm_isdst;            /* DST.        [-1/0/1]*/

#ifdef    __USE_BSD
  long int tm_gmtoff;        /* Seconds east of UTC.  */
  __const char *tm_zone;    /* Timezone abbreviation.  */
#else
  long int __tm_gmtoff;        /* Seconds east of UTC.  */
  __const char *__tm_zone;    /* Timezone abbreviation.  */
#endif
};
1.2 time_t

typedef __time_t time_t;


__STD_TYPE __TIME_T_TYPE __time_t;    /* Seconds since the Epoch.  */


#define __TIME_T_TYPE        __SLONGWORD_TYPE


#define __SLONGWORD_TYPE    long int

So we can see time_t is just a type of
It represent the seconds past since Epoch( 1970-01-01 00:00:00 +0000 (UTC) )

1.3 struct timeval

struct timeval {
    __kernel_time_t        tv_sec;        /* seconds */
    __kernel_suseconds_t    tv_usec;    /* microseconds */
};
typedef long        __kernel_time_t;
typedef long        __kernel_suseconds_t;

So they are all long int type.

1.4 struct timespec

struct timespec {
    __kernel_time_t    tv_sec;            /* seconds */
    long        tv_nsec;        /* nanoseconds */
};
You see these are two long type.


#ifdef __USE_GNU
/* Macros for converting between `struct timeval' and `struct timespec'.  */
# define TIMEVAL_TO_TIMESPEC(tv, ts) {                                   \
        (ts)->tv_sec = (tv)->tv_sec;                                    \
        (ts)->tv_nsec = (tv)->tv_usec * 1000;                           \
}
# define TIMESPEC_TO_TIMEVAL(tv, ts) {                                   \
        (tv)->tv_sec = (ts)->tv_sec;                                    \
        (tv)->tv_usec = (ts)->tv_nsec / 1000;                           \
}
#endif

1.5 struct timezone

struct timezone {
    int    tz_minuteswest;    /* minutes west of Greenwich */
    int    tz_dsttime;    /* type of dst correction */
};
As man proposes this struct is obsolete, should not be used
any more. So when an argument of a function is this type, firstly
just give a NULL; Secondly, if error occurs, man the function to
get detail infomation.

3. API
3.1 gettimeofday
#include
int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv, const struct timezone *tz);

tz should be NULL.

3.2 time
#include
time_t time(time_t *t);

Only return seconds.

3.3 utime and utimes
#include
#include
int utime(const char *filename, const struct utimbuf *times);

#include
int utimes(const char *filename, const struct timeval times[2]);

These change file access time and modification time.

3.4 clock
#include
clock_t clock(void);

clock_t is int in linux 32-bit system.
This function return the approximation of processor time
used by the program. But this is not so accurate and useful
as we expect.
use CLOCKS_PER_SEC to make it to a second, should use a float type
to contain it.

3.5 difftime
#include
double difftime(time_t time1, time_t time0);
return the number of seconds elapsed.
the reason why return double type is to try best to recorder more time, it's
always a integer.

3.6 Time Format
#include
size_t strftime(char *s, size_t max, const char *format,
                       const struct tm *tm);

#define _XOPEN_SOURCE /* glibc2 needs this */
#include
char *strptime(const char *s, const char *format, struct tm *tm);

3.7 timelocal and timegm
#include
time_t timelocal(struct tm *tm);
time_t timegm(struct tm *tm);


I think in most cases, these functions and data type are enough.
阅读(2324) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~