主题:日期、时间函数
cb0846
[专家分:0] 发布于 2003-01-02 19:36:00
求教TC2。0中的日期、时间函数及用法,有哪位大虾帮忙?
回复列表 (共12个回复)
沙发
lanjingquan [专家分:510] 发布于 2003-01-02 21:52:00
函数名: time
功 能: 取一天的时间
用 法: logn time(long *tloc);
程序例:
#include <time.h>
#include <stdio.h>
#include <dos.h>
int main(void)
{
time_t t;
t = time(NULL);
printf("The number of seconds since January 1, 1970 is %ld",t);
return 0;
}
板凳
lanjingquan [专家分:510] 发布于 2003-01-02 21:53:00
函数名: getdate
功 能: 取DOS日期
用 法: void getdate(struct *dateblk);
程序例:
#include <dos.h>
#include <stdio.h>
int main(void)
{
struct date d;
getdate(&d);
printf("The current year is: %d\n",
d.da_year);
printf("The current day is: %d\n",
d.da_day);
printf("The current month is: %d\n",
d.da_mon);
return 0;
}
3 楼
lanjingquan [专家分:510] 发布于 2003-01-02 21:54:00
函数名: gettime
功 能: 取得系统时间
用 法: void gettime(struct time *timep);
程序例:
#include <stdio.h>
#include <dos.h>
int main(void)
{
struct time t;
gettime(&t);
printf("The current time is: %2d:%02d:%02d.%02d\n",
t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund);
return 0;
}
4 楼
lanjingquan [专家分:510] 发布于 2003-01-02 21:54:00
函数名: gmtime
功 能: 把日期和时间转换为格林尼治标准时间(GMT)
用 法: struct tm *gmtime(long *clock);
程序例:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <dos.h>
/* Pacific Standard Time & Daylight Savings */
char *tzstr = "TZ=PST8PDT";
int main(void)
{
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;
}
5 楼
lanjingquan [专家分:510] 发布于 2003-01-02 21:55:00
VC里还可用MFC的CTIME,也很方便的
6 楼
cb0846 [专家分:0] 发布于 2003-01-06 08:22:00
我对lanjingquan大虾的回复很满意
7 楼
raul [专家分:0] 发布于 2003-02-23 11:19:00
请教一下哈,DOS.H是什么库函数?偶是初学者。
8 楼
李小龙 [专家分:0] 发布于 2003-04-23 17:35:00
9 楼
Qoooo [专家分:110] 发布于 2005-04-02 12:23:00
函数名: stime
功 能: 设置时间
用 法: int stime(long *tp);
程序例:
#include <stdio.h>
#include <time.h>
#include <dos.h>
int main(void)
{
time_t t;
struct tm *area;
t = time(NULL);
area = localtime(&t);
printf("Number of seconds since 1/1/1970 is: %ld\n", t);
printf("Local time is: %s", asctime(area));
t++;
area = localtime(&t);
printf("Add a second: %s", asctime(area));
t += 60;
area = localtime(&t);
printf("Add a minute: %s", asctime(area));
t += 3600;
area = localtime(&t);
printf("Add an hour: %s", asctime(area));
t += 86400L;
area = localtime(&t);
printf("Add a day: %s", asctime(area));
t += 2592000L;
area = localtime(&t);
printf("Add a month: %s", asctime(area));
t += 31536000L;
area = localtime(&t);
printf("Add a year: %s", asctime(area));
return 0;
}
10 楼
Qoooo [专家分:110] 发布于 2005-04-02 12:25:00
函数名: dostounix
功 能: 转换日期和时间为UNIX时间格式
用 法: long dostounix(struct date *dateptr, struct time *timeptr);
程序例:
#include <time.h>
#include <stddef.h>
#include <dos.h>
#include <stdio.h>
int main(void)
{
time_t t;
struct time d_time;
struct date d_date;
struct tm *local;
getdate(&d_date);
gettime(&d_time);
t = dostounix(&d_date, &d_time);
local = localtime(&t);
printf("Time and Date: %s\n", \
asctime(local));
return 0;
}
我来回复