Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19912624
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类: LINUX

2008-12-03 09:30:10

时间和日期

 

Unix的时间起点为GMT on January 1, 1970MS-DOS开始于1980

时间类型:time_tlinux中为long integer

 

语法:

#include

time_t time(time_t *tloc);

time的返回值存储在tloc,

 

实例:

# cat envtime.c

#include

#include

#include

#include

 

int main()

{

    int i;

    time_t the_time;

 

    for(i = 1; i <= 10; i++) {

        the_time = time((time_t *)0);

        printf("The time is %ld\n", the_time);

        sleep(2);

    }

    exit(0);

}

 

运行结果:

# ./envtime

The time is 1228204398

The time is 1228204400

The time is 1228204402

The time is 1228204404

The time is 1228204406

The time is 1228204408

The time is 1228204410

The time is 1228204412

The time is 1228204414

The time is 1228204416

 

ISO/ANSI C使用difftime的结构。

#include

double difftime(time_t time1, time_t time2);

gmtime提供更精确的时间:

#include

struct tm *gmtime(const time_t timeval);

 

tm Member Description

int tm_sec Seconds, 0-61

int tm_min Minutes, 0-59

int tm_hour Hours, 0-23

int tm_mday Day in the month, 1-31

int tm_mon Month in the year, 0-11 (January = 0)

int tm_year Years since 1900

int tm_wday Day in the week, 0-6 (Sunday = 0)

int tm_yday Day in the year, 0-365

int tm_isdst Daylight savings in effect

 

实例:

# cat gmtime.c

#include

#include

#include

 

int main()

{

    struct tm *tm_ptr;

    time_t the_time;

 

    (void) time(&the_time);

    tm_ptr = gmtime(&the_time);

 

    printf("Raw time is %ld\n", the_time);

    printf("gmtime gives:\n");

    printf("date: %02d/%02d/%02d\n",

        tm_ptr->tm_year, tm_ptr->tm_mon+1, tm_ptr->tm_mday);

    printf("time: %02d:%02d:%02d\n",

        tm_ptr->tm_hour, tm_ptr->tm_min, tm_ptr->tm_sec);

    exit(0);

}

 

运行结果:

# ./gmtime

Raw time is 1228205755

gmtime gives:

date: 108/12/02

time: 08:15:55

         输出的是:GMT (now known as Coordinated Universal Time, or UTC)时间,本地时间需要使用:

#include

struct tm *localtime(const time_t *timeval);

         tm转换为time_t,可以使用mktime,如果转换不成功,返回-1

         #include

time_t mktime(struct tm *timeptr);

需要更友好的输出,

#include

char *asctime(const struct tm *timeptr);

char *ctime(const time_t *timeval);

前者的输出类似:

Sun Jun 9 12:34:56 2007\n\0

后者的输出类似:

asctime(localtime(timeval))

实例:

# cat ctime.c

#include

#include

#include

 

int main()

{

    time_t timeval;

 

    (void)time(&timeval);

    printf("The date is: %s", ctime(&timeval));

    exit(0);

}

 

# ./ctime

The date is: Tue Dec  2 16:26:27 2008

 

 

         Strftime函数可以提供更多的控制:

         #include

size_t strftime(char *s, size_t maxsize, const char *format, struct tm *timeptr);

 

Conversion Specifier Description

%a Abbreviated weekday name

%A Full weekday name

%b Abbreviated month name

%B Full month name

%c Date and time

%d Day of the month, 01-31

%H Hour, 00-23

%I Hour in 12-hour clock, 01-12

%j Day of the year, 001-366

%m Month of the year, 01-12

%M Minutes, 00-59

%p a.m. or p.m.

%S Seconds, 00-61

%u Day in the week, 1-7 (1 = Monday)

%U Week in the year, 01-53 (Sunday is the first day of the week.)

%V Week in the year, 01-53 (Monday is the first day of the week.)

%w Day in the week, 0-6 (0 = Sunday)

%x Date in local format

%X Time in local format

%y Year number less 1900

%Y Year

%Z Time zone name

%% A % character

 

比如通常date的显示“Wed Dec  3 09:20:41 CST 2008就类似: %a %b %d %H:%M:%S %Y”。

Strptime可以帮助阅读日期,格式化和format类似,扫描字符串时和sscanf类似。Strptime的转换比strftime复杂,

#include

char *strptime(const char *buf, const char *format, struct tm *timeptr);

实例:

# cat strftime.c

#define _XOPEN_SOURCE /* glibc2 needs this for strptime */

#include

#include

#include

#include

 

int main()

{

    struct tm *tm_ptr, timestruct;

    time_t the_time;

    char buf[256];

    char *result;

 

    (void) time(&the_time);

    tm_ptr = localtime(&the_time);

    strftime(buf, 256, "%A %d %B, %I:%S %p", tm_ptr);

 

    printf("strftime gives: %s\n", buf);

 

    strcpy(buf,"Thu 26 July 2007, 17:53 will do fine");

 

    printf("calling strptime with: %s\n", buf);

    tm_ptr = ×truct;

 

    result = strptime(buf,"%a %d %b %Y, %R", tm_ptr);

    printf("strptime consumed up to: %s\n", result);

 

    printf("strptime gives:\n");

    printf("date: %02d/%02d/%02d\n",

        tm_ptr->tm_year % 100, tm_ptr->tm_mon+1, tm_ptr->tm_mday);

    printf("time: %02d:%02d\n",

        tm_ptr->tm_hour, tm_ptr->tm_min);

    exit(0);

}

运行结果:

 

# ./strftime

strftime gives: Wednesday 03 December, 09:58 AM

calling strptime with: Thu 26 July 2007, 17:53 will do fine

strptime consumed up to:  will do fine

strptime gives:

date: 07/07/26

time: 17:53

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