Chinaunix首页 | 论坛 | 博客
  • 博客访问: 344054
  • 博文数量: 60
  • 博客积分: 1570
  • 博客等级: 上尉
  • 技术积分: 620
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-02 23:37
文章分类

全部博文(60)

文章存档

2012年(2)

2010年(2)

2009年(56)

分类: LINUX

2009-11-26 00:03:26

1. time 函数。

#include <time.h>

time_t time(time_t *calptr);
                             Returns: value of time if OK, 1 on error

说明:

time 返回的时间值是指:自国际标准时间公元 1970年1月1日00:00:00 以来经过的秒数。

如果参数不为空,则这个时间值同时也会存放在由 calptr 指向的单元内。

2. 与 time 函数相比,gettimeofday 提供了更高的分辨率(可达微秒级)。


 

#include <sys/time.h>
int gettimeofday(struct timeval *restrict tp, void *restrict tzp);

                                                  Returns: 0 always

    该函数作为 XSI扩展定义在 Single UNIX Specification 中,tzp 的唯一合法值是 NULL,其他值则产生不确定的结果。某些平台支持用 tzp 说明时区,但这完全依赖实现而定,并不由 Single UNIX Specification 定义。
gettimeofday 函数将当前时间存放在 tp 指向的 timeval 结构中。

  struct timeval {
           time_t tv_sec;    /* 秒*/
           long   tv_usec;   /* 微秒。注意,不是毫秒!*/
   };
 

3. 各时间函数之间的关系。

 

4. localtime 和 gmtime 函数:

#include <time.h>
struct tm *gmtime(const time_t *calptr);
struct tm *localtime(const time_t *calptr);

                           Both return: pointer to broken-down time

    localtime 和 gmtime 函之间的区别是:localtime 将日历时间转换为本地时间(考虑到本地时间和夏时制标志),而 gmtime 则将日历时间转换为国际标准时间的年、月、日、时、分、秒周日。二者都把这些时间存放在 tm 结构中:

   struct tm {        /* a broken-down time */
     int  tm_sec;     /* seconds after the minute: [0 - 60] */
     int  tm_min;     /* minutes after the hour: [0 - 59] */
     int  tm_hour;    /* hours after midnight: [0 - 23] */
     int  tm_mday;    /* day of the month: [1 - 31] */
     int  tm_mon;     /* months since January: [0 - 11] */
     int  tm_year;    /* years since 1900 */
     int  tm_wday;    /* days since Sunday: [0 - 6] */
     int  tm_yday;    /* days since January 1: [0 - 365] */
     int  tm_isdst;   /* daylight saving time flag: <0, 0, >0 */
   };
    秒可以超过 59 的理由是可以表示润秒。注意,除了月日字段,其他字段的值都以 0 开始。如果夏时制生效,则夏时制标志为正;如果为非夏时制时间,则该标志值为 0; 如果此信息不可用,则其值为负。

5. mktime 函数以本地时间的年、月、日等作为参数,将其转换为 time_t 值。

#include <time.h>
time_t mktime(struct tm *tmptr);
                           Returns: calendar time if OK, 1 on error

 

6. asctime 和 ctime 函数产生一个 26 字节的字符串,这与 date(1) 命令的系统默认输出形式类似,例如:

Tue Feb 10 18:27:38 2004\n\0

#include <time.h>
char *asctime(const struct tm *tmptr);
char *ctime(const time_t *calptr);
                     Both return: pointer to null-terminated string

asctime 的参数是指向年、月、日等字符串的指针,而 ctime的参数是则是指向日历时间的指针。

注意:这四个函数的返回值指向一个静态分配区域,其后如果调用任何相关日期和时间函数,其内容就会被重写。

7. strftime 函数, 有些类似于 printf 函数。

#include <time.h>

size_t strftime(char *restrict buf, size_t maxsize,
                const char *restrict format,
                const struct tm *restrict tmptr);
 Returns: number of characters stored in array if room, 0 otherwise

    最后一个参数是要格式化的时间值,由一个指向 tm 结构的指针指定。格式化结果存放在一个长度为 maxsize 个字符的 buf 数组中,如果 buf 长度足以存放格式化结果以及一个 null 终止符,则该函数返回在 buf 中存放的字符数(不包括 null 终止符),否则返回0。

format 参数控制时间值的格式。如果 printf 函数一样,转换说明的形式是百分号之后跟一个特定字符。format 中的其他字符则按原样输出。两个连续的百分号在输出中产生一个百分号。与 printf 函数不同的是,每个转换说明产生一个不同的定长输出字符串,在format 字符串中没有字段宽度修饰符。

strftime 的转换说明

 格式  说明  实例
 %a  缩写的周日名  Tue
 %A  全周日名  Tuesday
 %b  缩写的月名

 Feb

 %B  全月名  February
 %c  日期和时间  Tue Feb 10 18:27:38 2004
 %C  年/100: [00~99]  20
 %d  月日: [01~31]  10
 %D  日期[MM/DD/YY]  02/10/04
 %e  月日(一位数前加空格):[1~31]  10
 %F  ISO 8601 日期格式[YYYY-MM-DD]  2004-02-10
 %g  ISO 8601基于周的年的最后2位数[00~99]  04
 %G  ISO 8601基于周的年  2004
 %h  与 %b 相同  Feb
 %H  小时(24 小时制):[00~23]  18
 %I  小时(12 小时制):[01~12]  06
 %j  年日:[001~366]  041
 %m  月:[01~12]  02
 %M  分:[00~59]  27
 %n  换行符  
 %p  AM/PM  PM
 %r  本地时间:(12时制)  06:27:38 PM
 %R  与“%H:%M”相同  18:27
 %S  秒:[00~60]  38
 %t  水平制表符  
 %T  与“%H:%M:%S”相同  18:27:38
 %u  ISO 8601周日[Monday=1, 1~7]  2 
 %U  星期日周数:[00~53]  06
 %V  ISO 8601周数:[01~53]  07
 %w  周日:[0=Sunday,06]  2
 %W  星期一周数:[00~53]  06
 %x  日期  02/10/04
 %X  时间  18:27:38
 %y  年的最后两位数:[00~99]  04
 %Y  年  2004
 %z  ISO 8601格式的UTC偏移量  -0500
 %Z  时区名  EST
 %%  转换为 1 个 %  %
     
     

    上表中的大多数格式说明的意义很明显。需要略作解释的是 %U、%V 和 %W。%U 是相应日期在该年中所属周数,包含该年中第一个星期日的周是第一周。 %W 也是相应日期在该年中所属的周数,不同的是包含第一个星期一的周为第一周。 %V 说明符则与上述两者有较大的区别。若某周包含了 1月1日,而且至少包含了其后的另外 3 天,那么该周是一年中的第一周,否则该周被认为是上一年的最后一周。在这两种情况下,周一都被视作每周的第一天。


 

阅读(1577) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~