求个输出北京时间的程序!

qq604622530 2009-04-27 08:09:25
要求:用C++编写一个程序。运行状态能显示当地时间的
...全文
112 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq604622530 2009-04-27
  • 打赏
  • 举报
回复
4楼的代码怎么我编译时出错?
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<dos.h>
#include<bios.h>
int main(void)
{ while(bioskey(1)==0)
{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);
}
}

最基本的gettime就可以,之后看你具体需要怎样
lingyin55 2009-04-27
  • 打赏
  • 举报
回复
简单一点的

#include<time.h> //C语言的头文件
#include<stdio.h> //C语言的I/O

void main()
{ time_t now; //实例化time_t结构
struct tm *timenow; //实例化tm结构指针
time(&now);
//time函数读取现在的时间(国际标准时间非北京时间),然后传值给now
timenow = localtime(&now);
//localtime函数把从time取得的时间now换算成你电脑中的时间(就是你设置的地区)
printf("Local time is %s\n",asctime(timenow));
//上句中asctime函数把时间转换成字符,通过printf()函数输出
}

注释:time_t是一个在time.h中定义好的结构体。而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;
};
lingyin55 2009-04-27
  • 打赏
  • 举报
回复

/*
程序功能:
显示系统当前时间
*/
#include <iostream>
#include "time.h"

using namespace std;

class Clock
{
public:
Clock(time_t pt=time(NULL))
{
t=pt;
local=localtime(&t);
nHour=local->tm_hour;
nMinute=local->tm_min;
nSecond=local->tm_sec;
}

~Clock()
{
cout<<"clock destruction OK!"<<endl;
}

private:
time_t t;
tm *local;
int nHour;
int nMinute;
int nSecond;

friend ostream& operator<<(ostream& out,Clock& clock)//重载操作符<<,输出时间
{
out<<clock.nHour<<":";
if(clock.nMinute<10)
out<<"0";
out<<clock.nMinute<<":";
if(clock.nSecond<10)
out<<"0";
out<<clock.nSecond;
out<<endl;
delete &clock;//释放空间
return out;
}
};

int main()
{
Clock *clock=new Clock();
cout<<"Local Time is:"<<endl;
cout<<*clock;

return 0;
}

mengde007 2009-04-27
  • 打赏
  • 举报
回复
Call this static function to retrieve a CFileTime object that represents the current system date and time.


static CFileTime GetCurrentTime( ) throw( );


Return Value
Returns the current system date and time in Coordinated Universal Time (UTC) format

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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