c++时钟程序代码
想做一个有关日历时钟的程序功能部分已经写好了但是问题是怎么从系统中调用电脑里的时间程序用呢?希望给我代码或实例谢谢各位了呵呵再祝各位在新的一年里都有所进步呵呵加一句小弟我...
想做一个 有关日历时钟的 程序 功能部分已经写好了 但是问题是怎么从系统中调用 电脑里的 时间程序 用呢? 希望给我代码 或实例 谢谢各位了
呵呵 再祝各位在新的一年里都有所进步 呵呵
加一句 小弟我只是对c++ 略懂一些 别的 两眼一抹黑 嘿嘿 麻烦各位高手了! 展开
呵呵 再祝各位在新的一年里都有所进步 呵呵
加一句 小弟我只是对c++ 略懂一些 别的 两眼一抹黑 嘿嘿 麻烦各位高手了! 展开
2个回答
展开全部
用c++的话还是弄成类比较好的,下面是我的解决方法:
#include <ctime>
#include <cstring>
class Timer
{
private:
static const int size = 50;
std::time_t time;
std::tm localTime;
char Ascll[size];
char flag;
public:
Timer()
{
std::time(&time);
flag = 0;
}
~Timer() {}
const char* printTime()
{
if(!flag)
{
localTime = *std::localtime(&time);
std::strcpy(Ascll, std::asctime(&localTime));
}
flag++;
return Ascll;
}
double timeDiff(Timer* time2) const
{
return difftime(time, time2 -> time);
}
} ;
int main()
{
Timer t1;
for(int i = 0; i < 10000; i++ )
{
cout << i;
}
Timer t2;
std::cout << t1.printTime() << std::endl;
std::cout << t2.printTime() << std::endl;
std::cout << t2.timeDiff(&t1) << std::endl;
return 0;
}
#include <ctime>
#include <cstring>
class Timer
{
private:
static const int size = 50;
std::time_t time;
std::tm localTime;
char Ascll[size];
char flag;
public:
Timer()
{
std::time(&time);
flag = 0;
}
~Timer() {}
const char* printTime()
{
if(!flag)
{
localTime = *std::localtime(&time);
std::strcpy(Ascll, std::asctime(&localTime));
}
flag++;
return Ascll;
}
double timeDiff(Timer* time2) const
{
return difftime(time, time2 -> time);
}
} ;
int main()
{
Timer t1;
for(int i = 0; i < 10000; i++ )
{
cout << i;
}
Timer t2;
std::cout << t1.printTime() << std::endl;
std::cout << t2.printTime() << std::endl;
std::cout << t2.timeDiff(&t1) << std::endl;
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
C语言库函数里提供了一套关于时间的函数
time_t t = 0;
char day[20] = {0};
t = time(0);//获取系统时间,此时t存放的是系统时间的秒值(从1970年1月1日0时开始到当前时间)
strftime (day, sizeof(day), "%Y-%m-%d %H:%M:%S", gmtime (&t)); //转换为字符串格式,这里的例子是 年-月-日 时:分:秒
这里给出的是Linux下的例子,需要包含头文件#include <sys/time.h>。
如果是在windows下,你可以自己找找相应的头文件即可
下面是MSDN里关于时间函数的示例,仔细看几遍,相信你以后对时间操作的问题就不会抓瞎了
#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>
void main()
{
char tmpbuf[128], ampm[] = "AM";
time_t ltime;
struct _timeb tstruct;
struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };
/* Set time zone from TZ environment variable. If TZ is not set,
* the operating system is queried to obtain the default value
* for the variable.
*/
_tzset();
/* Display operating system-style date and time. */
_strtime( tmpbuf );
printf( "OS time:\t\t\t\t%s\n", tmpbuf );
_strdate( tmpbuf );
printf( "OS date:\t\t\t\t%s\n", tmpbuf );
/* Get UNIX-style time and display as number and string. */
time( <ime );
printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime );
printf( "UNIX time and date:\t\t\t%s", ctime( <ime ) );
/* Display UTC. */
gmt = gmtime( <ime );
printf( "Coordinated universal time:\t\t%s", asctime( gmt ) );
/* Convert to time structure and adjust for PM if necessary. */
today = localtime( <ime );
if( today->tm_hour > 12 )
{
strcpy( ampm, "PM" );
today->tm_hour -= 12;
}
if( today->tm_hour == 0 ) /* Adjust if midnight hour. */
today->tm_hour = 12;
/* Note how pointer addition is used to skip the first 11
* characters and printf is used to trim off terminating
* characters.
*/
printf( "12-hour time:\t\t\t\t%.8s %s\n",
asctime( today ) + 11, ampm );
/* Print additional time information. */
_ftime( &tstruct );
printf( "Plus milliseconds:\t\t\t%u\n", tstruct.millitm );
printf( "Zone difference in seconds from UTC:\t%u\n",
tstruct.timezone );
printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] );
printf( "Daylight savings:\t\t\t%s\n",
tstruct.dstflag ? "YES" : "NO" );
/* Make time for noon on Christmas, 1993. */
if( mktime( &xmas ) != (time_t)-1 )
printf( "Christmas\t\t\t\t%s\n", asctime( &xmas ) );
/* Use time structure to build a customized time string. */
today = localtime( <ime );
/* Use strftime to build a customized time string. */
strftime( tmpbuf, 128,
"Today is %A, day %d of %B in the year %Y.\n", today );
printf( tmpbuf );
}
程序运行结果
OS time: 21:51:03
OS date: 05/03/94
Time in seconds since UTC 1/1/70: 768027063
UNIX time and date: Tue May 03 21:51:03 1994
Coordinated universal time: Wed May 04 04:51:03 1994
12-hour time: 09:51:03 PM
Plus milliseconds: 279
Zone difference in seconds from UTC: 480
Time zone name:
Daylight savings: YES
Christmas Sat Dec 25 12:00:00 1993
Today is Tuesday, day 03 of May in the year 1994.
time_t t = 0;
char day[20] = {0};
t = time(0);//获取系统时间,此时t存放的是系统时间的秒值(从1970年1月1日0时开始到当前时间)
strftime (day, sizeof(day), "%Y-%m-%d %H:%M:%S", gmtime (&t)); //转换为字符串格式,这里的例子是 年-月-日 时:分:秒
这里给出的是Linux下的例子,需要包含头文件#include <sys/time.h>。
如果是在windows下,你可以自己找找相应的头文件即可
下面是MSDN里关于时间函数的示例,仔细看几遍,相信你以后对时间操作的问题就不会抓瞎了
#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>
void main()
{
char tmpbuf[128], ampm[] = "AM";
time_t ltime;
struct _timeb tstruct;
struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };
/* Set time zone from TZ environment variable. If TZ is not set,
* the operating system is queried to obtain the default value
* for the variable.
*/
_tzset();
/* Display operating system-style date and time. */
_strtime( tmpbuf );
printf( "OS time:\t\t\t\t%s\n", tmpbuf );
_strdate( tmpbuf );
printf( "OS date:\t\t\t\t%s\n", tmpbuf );
/* Get UNIX-style time and display as number and string. */
time( <ime );
printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime );
printf( "UNIX time and date:\t\t\t%s", ctime( <ime ) );
/* Display UTC. */
gmt = gmtime( <ime );
printf( "Coordinated universal time:\t\t%s", asctime( gmt ) );
/* Convert to time structure and adjust for PM if necessary. */
today = localtime( <ime );
if( today->tm_hour > 12 )
{
strcpy( ampm, "PM" );
today->tm_hour -= 12;
}
if( today->tm_hour == 0 ) /* Adjust if midnight hour. */
today->tm_hour = 12;
/* Note how pointer addition is used to skip the first 11
* characters and printf is used to trim off terminating
* characters.
*/
printf( "12-hour time:\t\t\t\t%.8s %s\n",
asctime( today ) + 11, ampm );
/* Print additional time information. */
_ftime( &tstruct );
printf( "Plus milliseconds:\t\t\t%u\n", tstruct.millitm );
printf( "Zone difference in seconds from UTC:\t%u\n",
tstruct.timezone );
printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] );
printf( "Daylight savings:\t\t\t%s\n",
tstruct.dstflag ? "YES" : "NO" );
/* Make time for noon on Christmas, 1993. */
if( mktime( &xmas ) != (time_t)-1 )
printf( "Christmas\t\t\t\t%s\n", asctime( &xmas ) );
/* Use time structure to build a customized time string. */
today = localtime( <ime );
/* Use strftime to build a customized time string. */
strftime( tmpbuf, 128,
"Today is %A, day %d of %B in the year %Y.\n", today );
printf( tmpbuf );
}
程序运行结果
OS time: 21:51:03
OS date: 05/03/94
Time in seconds since UTC 1/1/70: 768027063
UNIX time and date: Tue May 03 21:51:03 1994
Coordinated universal time: Wed May 04 04:51:03 1994
12-hour time: 09:51:03 PM
Plus milliseconds: 279
Zone difference in seconds from UTC: 480
Time zone name:
Daylight savings: YES
Christmas Sat Dec 25 12:00:00 1993
Today is Tuesday, day 03 of May in the year 1994.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询