首页 > 编程知识 正文

c语言time函数的使用方法,C time函数

时间:2023-05-04 19:59:29 阅读:228745 作者:2809

头文件time.h
@函数名称: localtime
函数原型: struct tm *localtime(const time_t *timer)
函数功能: 返回一个以tm结构表达的机器时间信息
函数返回: 以tm结构表达的时间

struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst;}; #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;}

@函数名称: asctime
函数原型: char* asctime(struct tm * ptr)
函数功能: 得到机器时间(日期时间转换为ASCII码)
函数返回: 返回的时间字符串格式为:星期,月,日,小时:分:秒,年
参数说明: 结构指针ptr应通过函数localtime()和gmtime()得到

#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;}

@函数名称: ctime
函数原型: char *ctime(long time)
函数功能: 得到日历时间
函数返回: 返回字符串格式:星期,月,日,小时:分:秒,年
参数说明: time-该参数应由函数time获得

#include <stdio.h>#include <time.h>int main() { time_t t; time(&t); printf("Today's date and time: %s",ctime(&t)); return 0;}

@函数名称: gmtime

函数原型: struct tm *gmtime(time_t *time)
函数功能: 得到以结构tm表示的时间信息
函数返回: 以结构tm表示的时间信息指针
参数说明: time-用函数time()得到的时间信息

#include <stdio.h>#include <stdlib.h>#include <time.h>#include <dos.h>char *tzstr="TZ=PST8PDT";int main() { time_t t; struct tm *gmt, *area; putenv(tzstr); tzset(); t=time(NULL); area=localtime(&t); printf("Local time is:%s", asctime(area)); gmt=gmtime(&t); printf("GMT is:%s", asctime(gmt)); return 0;}

@函数名称: time

函数原型: time_t time(time_t *timer)
函数功能: 得到机器的日历时间或者设置日历时间
函数返回: 机器日历时间
参数说明: timer=NULL时得到机器日历时间,timer=时间数值时,用于设置日历时间,time_t是一个long类型

#include <time.h>#include <stdio.h>#include <dos.h>int main() { time_t t; t=time(); printf("The number of seconds since January 1,1970 is %ld",t); return 0;}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。