C 语言 时间函数 接着来

  1. 计算时间差函数 double difftime(time_t time2,time_t time1);
// 函数 功能: 计算两个时间的差值,一秒为单位
#include<stdio.h>
#include<time.h>
#include<dos.h> 
#include<conio.h>
int main()
{
    time_t first, second;
    clrscr();
    first = time(NULL);// 传递参数 NULL 获得 当前系统时间
    delay(3000);// 调用延迟程序 延迟 3秒
    second = time(NULL);// 这儿其实可以通过 clock 获得当前处理器时间
    printf("The interval is : %f seconds\n",difftime(second,first));
    getch();// 作用是然控制台停止下来 该函数又在 conio.h 种声明
    return 0;    
}
  1. gmtime 将日历转换成GMT (格林尼治时间)
// 函数原型 struct tm * gmtime(const time_t *timer)
//例子说明:
// 1. 首先使用 time() 函数获得一个系统时间
// 2. 在使用gmtime() 将该函数转换成GMT 时间, 该函数会返回一个指向struct tm 分段日期结构类型的指针
//最后应用asctime函数将分段日期转换成规定格式的字符串表示


#include<stdio.h>
#include<time.h>
#include<stdlib.h>

int main(void)
{
    time_t t;
    struct tm *gmt;
    t = time(NULL);
    gmt = gmtime(&t);
    printf("GMT is %s",asctime(gmt));
    return 0;
}
  1. 与上一个函数这号相反: localtime() 将 日历时间转换结构体类型时间
// 函数原型 struct tm *localtime(const tiem_t *timer)
// 1. 利用time() 获得系统时间,它是一个日历时间(日历时间,是用“从一个标准时间点到此时的时间经过的秒数”来表示的时间)
// 2. 利用函数localtime 将获取的系统时间(日历时间)转换为分段时间tm
// 3. 利用函数asctime 将该分段时间转换为规定的字符串格式的,并显示
// 4. 利用函数ctime直接将日历时间转换为规定的字符串时间,并显示

#include<stdio.h>
#include<time.h>
#include<dos.h>
int main(void)
{
    time_t timer;
    struct tm *tblock;
    timer = time(NULL);
    tblock = localtime(&timer);// localtime 传入的参数为 time_t 指针
    printf("Local time is: %s\n",asctime(tblock));
    printf("Today's date and time:%s\n",ctime(&timer));
    getch();
    return 0;
}

注:

dos.h 请点击查看

conio.h 包含函数如下:

cgets(char*);;
cputs(constchar*);
cscanf(constchar*,...);
inpw(unsignedshort);
getch(void);
getche(void);
kbhit(void);
outp(unsignedshort,int);
outpw(unsignedshort,unsignedshort);
putch(int);
ungetch(int);

void_Cdeclclreol(void);
void_Cdeclclrscr(void);
void_Cdecldelline(void);
int_Cdeclgettext(intleft,inttop,intright,intbottom,void*destin);
void_Cdeclgettextinfo(structtext_info*r);
void_Cdeclgotoxy(intx,inty);
void_Cdeclhighvideo(void);
void_Cdeclinsline(void);
void_Cdecllowvideo(void);
int_Cdeclmovetext(intleft,inttop,intright,intbottom,intdestleft,intdesttop);
void_Cdeclnormvideo(void);
int_Cdeclputtext(intleft,inttop,intright,intbottom,void*source);
void_Cdecltextattr(intnewattr);
void_Cdecltextbackground(intnewcolor);
void_Cdecltextcolor(intnewcolor);
void_Cdecltextmode(intnewmode);
int_Cdeclwherex(void);
int_Cdeclwherey(void);
void_Cdeclwindow(intleft,inttop,intright,intbottom);
char*_Cdeclcgets(char*str);
int_Cdeclcprintf(constchar*format,...);
int_Cdeclcputs(constchar*str);
int_Cdeclcscanf(constchar*format,...);
int_Cdeclgetch(void);
int_Cdeclgetche(void);
char*_Cdeclgetpass(constchar*prompt);
int_Cdeclkbhit(void);
int_Cdeclputch(intc);
int_Cdeclungetch(intch);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值