关于C语言time函数的用法

头文件time.h

@函数名称:     localtime

函数原型:     struct tm *localtime(const time_t *timer)

函数功能:     返回一个以tm结构表达的机器时间信息

函数返回:     以tm结构表达的时间,结构tm定义如下:

复制代码

 1 struct  tm {

 2     int tm_sec;

 3     int tm_min;

 4     int tm_hour;

 5     int tm_mday;

 6     int tm_mon;

 7     int tm_year;

 8     int tm_wday;

 9     int tm_yday;

10     int tm_isdst;

11 };

复制代码

参数说明:     timer-使用time()函数获得的机器时间 

 

按 Ctrl+C 复制代码

 

#include <time.h>

#include <stdio.h>

#include <dos.h>

int main() {

    time_t timer;

    struct tm *tblock;

    timer=time(NULL);

    tblock=localtime(&timer);

    printf("Local time is: %s",asctime(tblock));

    return 0;

}

按 Ctrl+C 复制代码

 

 

@函数名称:     asctime

函数原型:     char* asctime(struct tm * ptr)

函数功能:     得到机器时间(日期时间转换为ASCII码)

函数返回:     返回的时间字符串格式为:星期,月,日,小时:分:秒,年 

参数说明:     结构指针ptr应通过函数localtime()和gmtime()得到

所属文件:     <time.h>

 

按 Ctrl+C 复制代码

 

#include <stdio.h>

#include <string.h>

#include <time.h>

int main() {

    struct tm t;

    char str[80];

    t.tm_sec=1;

    t.tm_min=3;

    t.tm_hour=7;

    t.tm_mday=22;

    t.tm_mon=11;

    t.tm_year=56;

    t.tm_wday=4;

    t.tm_yday=0;

    t.tm_isdst=0;

    strcpy(str,asctime(&t));

    printf("%s",str);

    return 0;

}

按 Ctrl+C 复制代码

 

 

@函数名称:     ctime

函数原型:     char *ctime(long time)

函数功能:     得到日历时间

函数返回:     返回字符串格式:星期,月,日,小时:分:秒,年

参数说明:     time-该参数应由函数time获得

所属文件:     <time.h>

复制代码

1 #include <stdio.h>

2 #include <time.h>

3 int main() {

4     time_t t;

5     time(&t);

6     printf("Today's date and time: %s",ctime(&t));

7     return 0;

8 }

复制代码

@函数名称:     difftime

 

函数原型:     double difftime(time_t time2, time_t time1)

函数功能:     得到两次机器时间差,单位为秒

函数返回:     时间差,单位为秒

参数说明:     time1-机器时间一,time2-机器时间二.该参数应使用time函数获得

所属文件:     <time.h>

复制代码

 1 #include <time.h>

 2 #include <stdio.h>

 3 #include <dos.h>

 4 #include <conio.h>

  int main() {

      time_t first, second;

      clrscr();

      first=time(NULL);

      delay(2000);

     second=time(NULL);

     printf("The difference is: %f seconds",difftime(second,first));

     getch();

     return 0;

 }

复制代码

@函数名称:     gmtime

 

函数原型:     struct tm *gmtime(time_t  *time)

函数功能:     得到以结构tm表示的时间信息

函数返回:     以结构tm表示的时间信息指针

参数说明:     time-用函数time()得到的时间信息

所属文件:     <time.h>

复制代码

 1 #include <stdio.h>

 2 #include <stdlib.h>

 3 #include <time.h>

 4 #include <dos.h>

 5 char *tzstr="TZ=PST8PDT";

 6 int main() {

 7     time_t t;

 8     struct tm *gmt, *area;

 9     putenv(tzstr);

10     tzset();

11     t=time(NULL);

12     area=localtime(&t);

13     printf("Local time is:%s", asctime(area));

14     gmt=gmtime(&t);

15     printf("GMT is:%s", asctime(gmt));

16     return 0;

17 }

复制代码

@函数名称:     time

 

函数原型:     time_t time(time_t *timer)

函数功能:     得到机器的日历时间或者设置日历时间

函数返回:     机器日历时间

参数说明:     timer=NULL时得到机器日历时间,timer=时间数值时,用于设置日历时间,time_t是一个long类型

所属文件:     <time.h>

复制代码

1 #include <time.h>

2 #include <stdio.h>

3 #include <dos.h>

4 int main() {

5     time_t t;

6     t=time();

7     printf("The number of seconds since January 1,1970 is %ld",t);

8     return 0;

9 }

复制代码

@函数名称:     tzset

 

函数原型:     void tzset(void)

函数功能:     UNIX兼容函数,用于得到时区,在DOS环境下无用途

函数返回:

参数说明:

所属文件:     <time.h>

复制代码

 1 #include <time.h>

 2 #include <stdlib.h>

 3 #include <stdio.h>

 4 int main() {

 5      time_t td;

 6      putenv("TZ=PST8PDT");

 7      tzset();

 8      time(&td);

 9      printf("Current time=%s",asctime(localtime(&td)));

10      return 0;

11 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值