c语言中的时间函数

woshifairy 2005-12-01 02:25:57
main(){
char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t timep;
struct tm *p;
time(&timep);
p=localtime($timep);
//p=gmtime(&timep);
printf("%d%d%d",(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);
printf("%s%d;%d;%d\n", wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
}
为什么我用localtime可以取道时间,而用gmtime就只能取道年呢?
...全文
181 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
woshifairy 2005-12-07
  • 打赏
  • 举报
回复
谢谢 各位不知道是怎么回是。这几天有时候能得到,有时候得不到。。奇怪。
netmelody 2005-12-01
  • 打赏
  • 举报
回复
#include <time.h>
main(){
char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t timep;
struct tm *p;
time(&timep);
p=gmtime(&timep);
printf(“%d%d%d”,(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);
printf(“%s%d;%d;%d\n”, wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
}
执行
2000/10/28 Sat 8:15:38
jsjjms 2005-12-01
  • 打赏
  • 举报
回复
struct tm *localtime(long *clock)
本函数把clock所指的时间(如函数time返回的时间)转换成当地标准时间,并以tm结构形式返回

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

int main(void)
{
time_t timer;
struct tm *tblock;

/* gets time of day */
timer = time(NULL);

/* converts date/time to a structure */
tblock = localtime(&timer);

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

return 0;
}
jsjjms 2005-12-01
  • 打赏
  • 举报
回复
struct tm *gmtime(long *clock)
本函数把clock所指的时间(如由函数time返回的时间)转换成格林威治时间,并以tm结构形式返回

#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;
}
屋顶上的老猫 2005-12-01
  • 打赏
  • 举报
回复
time_t timep;
struct tm *local,*gmt;
timep=time(NULL);
p=localtime($timep);
p=gmtime(&timep);
printf("LOCAL is %s\n",asctime(local));
printf("GMT is %s\n",asctime(gmt));
}
屋顶上的老猫 2005-12-01
  • 打赏
  • 举报
回复
不好意思,发错了!

改成 timep = time(NULL)
屋顶上的老猫 2005-12-01
  • 打赏
  • 举报
回复
struct tm *p,*timep;

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧