C ++中的系统时间 [英] System time in C++

查看:102
本文介绍了C ++中的系统时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好:


我需要添加一个使用ANSI标准访问系统时间(至少至少
毫秒)的片段。 C ++。出于各种原因,我现在无法在我的程序中使用MFC和Win32 API。在ANSI C ++中是否有类似这样的东西?b $ b允许访问最多毫秒。我知道time.h

和CTime允许访问第二级,但我需要工作在$ m $ b msec级别。


任何帮助将不胜感激。


Partho


[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++。审核。第一次海报:做到这一点! ]

Hi all:

I need to add a snippet which access the system time (upto atleast
milliseconds) using ANSI std. C++. I cannot use MFC and Win32 APIs in
my program for now for various reasons. Is there something like this
in ANSI C++ which allows access up to milliseconds. I know that time.h
and CTime allow access up to the second level, but I need to work in
the msec level.

Any help will be appreciated.

Partho

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

推荐答案

Partho Choudhury写道:
Partho Choudhury wrote:
大家好:

我需要添加一个使用ANSI标准访问系统时间(最多至少几毫秒)的片段。 C ++。由于各种原因,我暂时无法在我的程序中使用MFC和Win32 API。在ANSI C ++中是否有类似这样的内容,允许访问最多毫秒。我知道time.h
和CTime允许访问第二级,但我需要在msec级别工作。
Hi all:

I need to add a snippet which access the system time (upto atleast
milliseconds) using ANSI std. C++. I cannot use MFC and Win32 APIs in
my program for now for various reasons. Is there something like this
in ANSI C++ which allows access up to milliseconds. I know that time.h
and CTime allow access up to the second level, but I need to work in
the msec level.







在time.h中应该有这样的东西:


/ *这定义了CLOCKS_PER_SEC,这是处理器时钟的数量

每秒滴答。 * /

#include< bits / time.h>


/ *这是相同常量的过时POSIX.1-1988名称。 * /

#if!defined __STRICT_ANSI__&& !定义__USE_XOPEN2K

#ifndef CLK_TCK

#define CLK_TCK CLOCKS_PER_SEC

#endif

#endif


with clock()你可以从开始就得到时钟滴答。举个例子

(来源:cplusplus.com):

/ *时钟示例:倒计时* /

#include< stdio.h> ;

#include< time.h>


无效等待(int秒)

{

clock_t endwait;

endwait = clock()+ seconds * CLK_TCK; //或CLOCKS_PER_SEC

while(clock()< endwait){}

}


int main()

{

int n;

printf(" Starting countdown ... \ n");

for (n = 10; n> 0; n--)

{

printf("%d \ n",n);

等待(1);

}

printf(" FIRE !!! \ n");

返回0; < br $>
}


问候marbac



Hi,

In time.h there should be something like this:

/* This defines CLOCKS_PER_SEC, which is the number of processor clock
ticks per second. */
# include <bits/time.h>

/* This is the obsolete POSIX.1-1988 name for the same constant. */
# if !defined __STRICT_ANSI__ && !defined __USE_XOPEN2K
# ifndef CLK_TCK
# define CLK_TCK CLOCKS_PER_SEC
# endif
# endif

with clock () you can get the clock ticks since start. As an example
(source:cplusplus.com):
/* clock example: countdown */
#include <stdio.h>
#include <time.h>

void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLK_TCK ; // or CLOCKS_PER_SEC
while (clock() < endwait) {}
}

int main ()
{
int n;
printf ("Starting countdown...\n");
for (n=10; n>0; n--)
{
printf ("%d\n",n);
wait (1);
}
printf ("FIRE!!!\n");
return 0;
}

Regards marbac


Partho Choudhury写道:
Partho Choudhury wrote:
I需要添加一个使用ANSI标准访问系统时间(最多至少几毫秒)的片段。 C ++。由于各种原因,我暂时无法在我的程序中使用MFC和Win32 API。在ANSI C ++中是否有类似这样的内容,允许访问最多毫秒。我知道time.h
和CTime允许访问第二级,但我需要在msec级别工作。
I need to add a snippet which access the system time (upto atleast
milliseconds) using ANSI std. C++. I cannot use MFC and Win32 APIs in
my program for now for various reasons. Is there something like this
in ANSI C++ which allows access up to milliseconds. I know that time.h
and CTime allow access up to the second level, but I need to work in
the msec level.




所以,你的问题基本上是这样的:我知道这种语言没有b $ b有一种做A的机制,但在语言中是否有办法做A?


BTW,没有标准符号''CTime''。也许你在混淆C ++

与MFC(由于各种原因你无法使用)。


在我之后重复:没有标准的C ++方式访问系统时间

,除非使用< ctime>中的函数(或< time.h>)。如果你需要

a毫秒分辨率(或更好),你_have_使用特定于OS的

机制(如果有的话)。


V



So, your question is essentially this: I know that the language doesn''t
have a mechanism to do A, but is there a way in the language to do A?

BTW, there is no standard symbol ''CTime''. Perhaps you''re confusing C++
with MFC (which you cannot use for various reasons).

Repeat after me: there is no standard C++ way to access system time
except by using the functions in <ctime> (or <time.h>). If you need
a millisecond resolution (or better), you _have_ to use OS-specific
mechanisms if they are available.

V


-----开始PGP签名消息-----

哈希:SHA1


Partho,

您可以尝试使用boost :: osix_time :: ptime接口。它非常简单。

http://www.boost.org/libs/date_time/...dex.html#usage

----- BEGIN PGP签名-----

版本:GnuPG v1.0.6(GNU / Linux)

评论:将GnuPG与Mozilla一起使用 - http://enigmail.mozdev.org

iD8DBQFA4BbEoo / Prlj9GScRAkdvAJ455 / WvFDB3qy3to8hvF9vnWcJZZACdFGMU

uzE86LIai4MH49d6 + rdyhgs =

= HWtn

----- END PGP SIGNATURE -----


[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++。审核。第一次海报:做到这一点! ]
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Partho,

You could try using the boost::osix_time::ptime interface. it is quite
simple.

http://www.boost.org/libs/date_time/...dex.html#usage
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFA4BbEoo/Prlj9GScRAkdvAJ455/WvFDB3qy3to8hvF9vnWcJZZACdFGMU
uzE86LIai4MH49d6+rdyhgs=
=HWtn
-----END PGP SIGNATURE-----

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


这篇关于C ++中的系统时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆