当前位置:文档之家› vc++获取系统时间和程序运行时间

vc++获取系统时间和程序运行时间

vc++获取系统时间和程序运行时间
vc++获取系统时间和程序运行时间

内容:

Q:如何获取时间?精度如何?

A:

1 使用time_t time( time_t * timer ) 精确到秒

计算时间差使用double difftime( time_t timer1, time_t timer0 )

2 使用clock_t clock() 得到的是CPU时间精确到1/CLOCKS_PER_SEC秒

3 使用DWORD GetTickCount() 得到的是系统运行的时间精确到毫秒

4 如果使用MFC的CTime类,可以用CTime::GetCurrentTime() 精确到秒

5 要获取高精度时间,可以使用

BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)获取系统的计数器的频率

BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)获取计数器的值

然后用两次计数器的差除以Frequency就得到时间。

6 还有David的文章中提到的方法:

Multimedia Timer Functions

The following functions are used with multimedia timers.

timeBeginPeriod/timeEndPeriod/timeGetDevCaps/timeGetSystemTime

timeGetTime/timeKillEvent/TimeProc/timeSetEvent 精度很高

Q:GetTickCount()函数,说是毫秒记数,是真的吗,还是精确到55毫秒?

A:

GetTickCount()和GetCurrentTime()都只精确到55ms(1个tick就是55ms)。如果要精确到毫秒,应该使用timeGetTime函数或QueryPerformanceCounter函数。具体例子可以参考QA001022 "VC++中使用高精度定时器"、QA001813 "如何在Windows实现准确的定时"和QA004842 "timeGetTime函数延时不准"。

Q:vc++怎样获取系统时间,返回值是什么类型的变量呢?

GetSystemTime返回的是格林威志标准时间

GetLocalTime,和上面用法一样,返回的是你所在地区的时间,中国返回的是北京时间VOID GetSystemTime(

LPSYSTEMTIME lpSystemTime // address of system time structure

);

函数就可以获得了,其中LPSYSTEMTIME 是个结构体

含:年,月,日,周几,小时,分,秒,毫秒。

以下是Time的MSDN文档:

Compatibility in the Introduction.

Libraries

LIBC.LIBSingle thread static library, retail versionLIBCMT.LIBMultithread static library, retail versionMSVCRT.LIBImport library for MSVCRT.DLL, retail version

Return Value

time returns the time in elapsed seconds. There is no error return.

Parameter

timer

Storage location for time

Remarks

The time function returns the number of seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time, according to the system clock. The return value is stored in the location given by timer. This parameter may be NULL, in which case the return value is not stored.

Example

/* TIMES.C illustrates various time and date functions including:

* time _ftime ctime asctime

* localtime gmtime mktime _tzset

* _strtime _strdate strftime

*

* Also the global variable:

* _tzname

*/

#include

#include

#include

#include

#include

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", https://www.doczj.com/doc/6b8401209.html,litm );

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 );

}

Output

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.

1.使用CTime类

CString str;//获取系统时间CTime tm;tm=CTime::GetCurrentTime();str=tm.Format("现在时间是%Y年%m月%d日%X");MessageBox(str,NULL,MB_OK);

2: 得到系统时间日期(使用GetLocalTime)SYSTEMTIME st;CString strDate,strTime;GetLocalTime(&st);strDate.Format("%4d-%2d-%2d",st.wYear,st.wMonth,st.wDa y);strTime.Format("%2d:%2d:%2d",st.wHour,st.wMinute,st.wSecond);

3.使用GetTickCount//获取程序运行时间long t1=GetTickCount();//程序段开始前取得系统运行时间(ms)Sleep(500);long t2=GetTickCount();();//程序段结束后取得系统运行时间(ms)str.Format("time:%dms",t2-t1);//前后之差即程序运行时间AfxMessageBox(str);//获取系统运行时间long t=GetTickCount();CString str,str1;str1.Format("系统已运行%d时",t/3600000);str=str1;t%=3600000;str1.Format("%d分",t/60000);str+=str1;t%=60000;str1.Format("%d秒",t/1000);str+=str1;AfxMessageBox(str);

如何在VC6.0中得到一个程序的运行时间,也就是这个程序耗费的时钟周期数// C和C++的时间编程

#include #include using namespace std; int main() { time_t begin,end; begin=clock(); //这里加上你的代码end=clock(); cout<<"runtime: "<

unix时间相关,也是标准库的这些在1.timegm函数只是将struct tm结构转成time_t 结构,不使用时区信息;time_t timegm(struct tm *tm);

2.mktime使用时区信息time_t mktime(struct tm *tm);

timelocal 函数是GNU扩展的与posix函数mktime相当time_t timelocal (struct tm *tm);

3.gmtime函数只是将time_t结构转成struct tm结构,不使用时区信息;struct tm * gmtime(const time_t *clock);

4.localtime使用时区信息struct tm * localtime(const time_t *clock);

1.time获取时间,stime设置时间time_t t;t = time(&t);

2.stime其参数应该是GMT时间,根据本地时区设置为本地时间;int stime(time_t *tp)

3.UTC=true 表示采用夏时制;

4.文件的修改时间等信息全部采用GMT时间存放,不同的系统在得到修改时间后通过localtime转换成本地时间;

5.设置时区推荐使用setup来设置;

6.设置时区也可以先更变/etc/sysconfig/clock中的设置再将ln -fs /usr/share/zoneinfo/xxxx/xxx /etc/localtime 才能重效

time_t只能表示68年的范围,即mktime只能返回1970-2038这一段范围的time_t看看你的系统是否有time_t64,它能表示更大的时间范围

Window里面的一些不一样的

CTime MFC类,好像就是把time.h封了个类,没扩展CTime t = GetCurrentTime(); SYSTEMTIME 结构包含毫秒信息typedef struct _SYSTEMTIME { WORD wYear; WORD wMonth; WORD wDayOfWeek; WORD wDay; WORD wHour; WORD wMinute; WORD wSecond; WORD wMilliseconds;} SYSTEMTIME, *PSYSTEMTIME;

SYSTEMTIME t1;GetSystemTime(&t1) CTime curTime(t1); WORD ms = t1.wMilliseconds; SYSTEMTIME sysTm;::GetLocalTime(&sysTm);

在time.h中的_strtime() //只能在windows中用char t[11];_strtime(t);puts(t);

------------------------------------------------------------------------------_timeb定义在SYS\TIMEB.H,有四个fieldsdstflagmillitmtimetimezone

void _ftime( struct _timeb *timeptr );

struct _timeb timebuffer; _ftime( &timebuffer );取当前时间:文档讲可以到ms,有人测试,好象只能到16ms!

-------------------------------------------------------------------------如何设定当前系统时间---windowsSYSTEMTIME m_myLocalTime,*lpSystemTime; m_myLocalTime.wYear=2003; m_myLocalTime.wMonth=1; m_myLocalTime.wDay=1; m_myLocalTime.wHour=0; m_myLocalTime.wMinute=0; m_myLocalTime.wSecond=0; m_myLocalTime.wMilliseconds=0; lpSystemTime=&m_myLocalTime; if( SetLocalTime(lpSystemTime) ) //此处换成SetSystemTime( )也不行MessageBox("OK !"); else MessageBox("Error !");

SYSTEMTIME

m_myLocalTime,*lpSystemTime;m_myLocalTime.wYear=2003;m_myLocalTime.wMonth=1;m_ myLocalTime.wDay=1;lpSystemTime=&m_myLocalTime;if( SetDate(lpSystemTime) ) //此处换成SetSystemTime( )也不行MessageBox("OK !"); else MessageBox("Error !");

-----------------------------------------------------------------------------用clock()函数,得到系统启动以后的毫秒级时间,然后除以CLOCKS_PER_SEC,就可以换成“秒”,标准c函数。clock_t clock ( void );

#include clock_t t = clock();long sec = t / CLOCKS_PER_SEC;他是记录时钟周期的,实现看来不会很精确,需要试验验证;

---------------------------------------------------------------------------据说tc2.0的time结构含有毫秒信息#include #include

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; } time 是一个结构体,,其中成员函数ti_hund 是豪秒。。。上程序可以在tc2.0运行

--------------------------------------------------------------------------------这个是windows里面常用来计算程序运行时间的函数;DWORD dwStart = GetTickCount();

//这里运行你的程序代码

DWORD dwEnd = GetTickCount();

则(dwEnd-dwStart)就是你的程序运行时间, 以毫秒为单位这个函数只精确到55ms,1个tick 就是55ms。--------------------------------------------------------------------------------timeGetTime()基本等于GetTickCount(),但是精度更高DWORD dwStart = timeGetTime();

//这里运行你的程序代码

DWORD dwEnd = timeGetTime();

则(dwEnd-dwStart)就是你的程序运行时间, 以毫秒为单位虽然返回的值单位应该是ms,但传说精度只有10ms。--------------------------------------------------------------------------------

Borland C++ Builder VCL的时间函数

1. Date返回TDateTime对象,包含当前的年月日信息,函数原型如下:System::TDateTime __fastcall Date(void);

2. Time返回TDateTime对象,包含当前的时间信息,函数原型如下:System::TDateTime __fastcall Time(void);

3. Now返回TDateTime对象,获取当前的日期和时间信息,函数原型如下:System::TDateTime __fastcall Now(void);

4. DatetimeToString将TDateTime对象转换为指定格式的字符串对象,函数原型如下:void __fastcall DateTimeToString(AnsiString &;Result, const AnsiString Format,System::TDateTime DateTime);

5. DateToStr将TDateTime对象(包含当前年月日信息)转换为字符串对象,函数原型如下:AnsiString __fastcall DateToStr(System::TDateTime Date);

6. TimeToStr将当前日期转换为字符串对象,函数原型如下:AnsiString __fastcall TimeToStr(System::TDateTime Time);

7. DateTimetoStr将TDateTime对象转换为字符串对象,函数原型如下:AnsiString __fastcall DateTimeToStr(System::TDateTime DateTime);

8. StrToDate将字符串对象转换为年月日对象,函数原型如下:System::TDateTime __fastcall StrToDate(const AnsiString S);

9. StrToTime将字符串对象转换时间对象,函数原型如下:System::TDateTime __fastcall StrToTime(const AnsiString S);

10.StrToDateTime将字符串对象转换为年月日时间对象,函数原型如下:System::TDateTime __fastcall StrToDateTime(const AnsiString S);

11.DateTimeToSystemTime将TDateTime对象转换为操作系统时间,函数原型如下:void __fastcall DateTimeToSystemTime(System::TDateTime DateTime, _SYSTEMTIME &;SystemTime);

12.SystemTimeToDateTime将操作系统时间转换为TDateTime对象,函数原型如下:System::TDateTime __fastcall SystemTimeToDateTime(const _SYSTEMTIME &;SystemTime);

---------------------------------------------------------------------------------------下面是转的一个用汇编的精确计时方法---------------------------------------------------------------------------------------如何获得程序或者一段代码运行的时间?你可能说有专门的程序测试工具,确实,不过你也可以在程序中嵌入汇编代码来实现。

在Pentium的指令系统中有一条指令可以获得CPU内部64位计数器的值,我们可以通过代码两次获取该计数器的值而获得程序或代码运行的时钟周期数,进而通过你的cpu的频率算出一个时钟周期的时间,从而算出程序运行的确切时间。

我们通过指令TDSIC来获得cpu内部计数器的值,指令TDSIC返回值放在EDX:EAX中,其中EDX中存放64位寄存器中高32位的值,EAX存放第32位的值.

下面看看实现的代码:

//用汇编实现获取一段代码运行的时间#include

using namespace std;

void GetClockNumber (long high, long low); void GetRunTime();

int main() {

long HighStart,LowStart,HighEnd,LowEnd; long numhigh,numlow; //获取代码运行开始时cpu 内部计数器的值__asm { RDTSC mov HighStart, edx mov LowStart, eax } for(int i= 0; i<100000; i++ ) { for(int i= 0; i<100000; i++ ) { }

} //获取代码结束时cpu内部计数器的值,并减去初值__asm { RDTSC mov HighEnd, edx Mov LowEnd, eax ;获取两次计数器值得差sub eax, LowStart cmp eax, 0 ; 如果低32的差为负则求返,因为第二次取得永远比第一次的大jg L1 neg eax jmp L2

L1: mov numlow, eax L2: sbb edx, HighStart mov numhigh, edx } //把两个计数器值之差放在一个64位的整形变量中//先把高32位左移32位放在64的整形变量中,然后再加上低32位__int64 timer =(numhigh<<32) + numlow; //输出代码段运行的时钟周期数//以频率1.1Gcpu为例,如果换计算机把其中的1.1改乘其它即可,因为相信大家的cpu都应该在1G以上^_^ cout<< (double) (timer /1.1/1000000000) << endl; return 0; } 这样通过一条简单的汇编指令就可以获得程序或一段代码的大概时间,不过并不能得到运行的确切时间,因为即使去掉中间的循环,程序也会有个运行时间,

因为在第一次取得计数器的值后,有两条汇编指令mov HighStart, edx mov LowStart, eax 这两条指令当然也有运行时间,当然你可以减去这两条指令的运行时间(在1.1G的机子上是3e-8s),这样会更精确一点。

如果你要确切知道程序的运行时间,专业的测试软件肯定会更好一点,不过好像一般没有必要获取除非专门的要求的程序。

不过能DIY一个也是不错的,不管有没有,最起码你可以学到在VC++中如何嵌入汇编代码以及如何使用32位的寄存器,其实和16位的寄存器一样使用,将来64的也应该一样,只不过位数不同罢了

Oracle 获取系统日期,日期转换函数

Oracle 获取系统日期,日期转换函数 learning oracle 获取系统日期和日期转换函数应用获取系统日期:SYSDA TE() 格式化日期:TO_CHAR(SYSDATE(),'YY/MM/DD HH24:MI:SS) 或TO_DATE(SYSDA TE(),'YY/MM/DD HH24:MI:SS) 格式化数字:TO_NUMBER 注:TO_CHAR 把日期或数字转换为字符串 TO_CHAR(number, '格式') TO_CHAR(salary, '$99,999.99') TO_CHAR(date, '格式') TO_DATE 把字符串转换为数据库中的日期类型 TO_DATE(char, '格式') TO_NUMBER 将字符串转换为数字 TO_NUMBER(char, '格式') 返回系统日期,输出25-12月-09 select sysdate from dual; mi是分钟,输出2009-12-25 14:23:31 select to_char(sysdate,'yyyy-MM-dd HH24:mi:ss') from dual; mm会显示月份,输出2009-12-25 14:12:31 select to_char(sysdate,'yyyy-MM-dd HH24:mm:ss') from dual; 输出09-12-25 14:23:31 select to_char(sysdate,'yy-mm-dd hh24:mi:ss') from dual 输出2009-12-25 14:23:31 select to_date('2009-12-25 14:23:31','yyyy-mm-dd,hh24:mi:ss') from dual 而如果把上式写作: select to_date('2009-12-25 14:23:31','yyyy-mm-dd,hh:mi:ss') from dual 则会报错,因为小时hh是12进制,14为非法输入,不能匹配。 输出$10,000,00 : select to_char(1000000,'$99,999,99') from dual; 输出RMB10,000,00 : select to_char(1000000,'L99,999,99') from dual; 输出1000000.12 : select trunc(to_number('1000000.123'),2) from dual; select to_number('1000000.123') from dual; 转换的格式: 表示year 的:y 表示年的最后一位、 yy 表示年的最后2位、 yyy 表示年的最后3位、 yyyy 用4位数表示年 表示month的:mm 用2位数字表示月、 mon 用简写形式,比如11月或者nov 、 month 用全称,比如11月或者november 表示day的:dd 表示当月第几天、 ddd 表示当年第几天、

HTML中获得当前日期时间星期代码

这代码本人早已使用,现共享一下。

C获取系统时间及时间格式

1、新建一个windows form 窗体应用程序。 2、在该窗体加载时,输入如下代码 [csharp] view plaincopy Thread P_thread = new Thread( () => //lambda表达式(参数列表)=>{执行语句} lambda表达式是比匿名方法更加简洁的一种匿名函数语法 { while (true) {//public Object Invoke (Delegate method)在(拥有此控件的基础窗口句柄的)线程上执行指定的委托。 //关于为何使用invoke方法,参见C#中跨线程调用控件的线程安全性方法一文 this.Invoke( (MethodInvoker)delegate()//methodinvoke 表示一个委托,该委托可执行托管代码中声明为void 且不接受任何参数的任何方法。 //在对控件的Invoke 方法进行调用时或需要一个简单委托又不想自己定义时可以使用该委托。 { this.Refresh(); Graphics P_Graphics = CreateGraphics(); // Control.CreateGraphics方法,为控件创建Graphics。 //public Graphics CreateGraphics () 返回值为控件的Graphics。Graphics 类提供将对象绘制到显示设备的方法 //public void DrawString( // string s, // Font font, // Brush brush, // PointF point //)在指定位置point并且用指定的Brush 和Font 对象绘制指定的文本字符串s。 P_Graphics.DrawString("系统时间:" + DateTime.Now.ToString("yyyy年MM月dd日HH时mm分ss秒"), new Font("宋体", 15), Brushes.Blue, new Point(10, 10)); });//this.invoke Thread.Sleep(1000); }//while

JAVA中获取当前时间

Java中获得当前时间的方法 2008年06月16日星期一下午 10:06 有两种方法: 方法一:用java.util.Date类来实现,并结合java.text.DateFormat类来实现时间的格式化,看下面代码: import java.util.*; import java.text.*; //以下默认时间日期显示方式都是汉语语言方式 //一般语言就默认汉语就可以了,时间日期的格式默认为MEDIUM风格,比如:2008-6-16 20:54:53 //以下显示的日期时间都是再Date类的基础上的来的,还可以利用Calendar类来实现见类TestDate2.java public class TestDate { public static void main(String[] args) { Date now = new Date(); Calendar cal = Calendar.getInstance(); DateFormat d1 = DateFormat.getDateInstance(); //默认语言(汉语)下的默认风格(MEDIUM风格,比如:2008-6-16 20:54:53) String str1 = d1.format(now); DateFormat d2 = DateFormat.getDateTimeInstance(); String str2 = d2.format(now); DateFormat d3 = DateFormat.getTimeInstance(); String str3 = d3.format(now); DateFormat d4 = DateFormat.getInstance(); //使用SHORT风格显示日期和时间 String str4 = d4.format(now); DateFormat d5 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); //显示日期,周,时间(精确到秒) String str5 = d5.format(now); DateFormat d6 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); //显示日期。时间(精确到秒) String str6 = d6.format(now); DateFormat d7 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); //显示日期,时间(精确到分) String str7 = d7.format(now); DateFormat d8 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM);

Matlab中计算程序运行时间的三种方法

Matlab中计算程序运行时间的三种方法 经常我们需要计算我们程序到底运行多长时间,这样可以比较程序的执行效率。当然这个对于只有几秒钟的小程序没有什么意义,但是对于大程序就有很重要的意义了。 下面我们就说说Matlab中计算程序运行时间的三种常用方法吧! 注意:三种方法由于使用原理不一样,得到结果可能有一定的差距! 1、tic和toc组合(使用最多的) 计算tic和toc之间那段程序之间的运行时间,它的经典格式为 1. tic 2. 。。。。。。。。。。 3. toc 复制代码 换句话说程序,程序遇到tic时Matlab自动开始计时,运行到toc时自动计算此时与最近一次tic之间的时间。这个有点拗口,下面我们举个例子说明 1. % by dynamic of Matlab技术论坛 2. % see also https://www.doczj.com/doc/6b8401209.html, 3. % contact me matlabsky@https://www.doczj.com/doc/6b8401209.html, 4. % 2009-08-18 12:08:47 5. clc 6. tic;%tic1 7. t1=clock; 8. for i=1:3 9. tic ;%tic2 10. t2=clock; 11. pause(3*rand) 12. %计算到上一次遇到tic的时间,换句话说就是每次循环的时间 13. disp(['toc计算第',num2str(i),'次循环运行时间:',num2str(toc)]); 14. %计算每次循环的时间 15. disp(['etime计算第',num2str(i),'次循环运行时间:',num2str(etime(clock,t2))]); 16. %计算程序总共的运行时间 17. disp(['etime计算程序从开始到现在运行的时间:',num2str(etime(clock,t1))]); 18. disp('======================================') 19. end 20. %计算此时到tic2的时间,由于最后一次遇到tic是在for循环的i=3时,所以计算 的是最后一次循环的时间 21. disp(['toc计算最后一次循环运行时间',num2str(toc)]) 22. disp(['etime程序总运行时间:',num2str(etime(clock,t1))]); 复制代码 运行结果如下,大家可以自己分析下 1. toc计算第1次循环运行时间: 2.5628 2. etime计算第1次循环运行时间:2.562

wincc系统日期时间获取

wincc系统日期时间获取 系统时间的获得,有两种办法,一是建立TAG,二是使用函数获取系统时间 方法一: 在[Tag Management]添加新的驱动"System Info.chn",然后在新添加的[SYSTEM INFO]新建连接,这样就可以创建实际TAG了; 下面新建几个TAG: 名字:date, 数据类型:text tag 8-bit character set,地址设定为Function:date,format:"MM-DD-YYYY" 名字:time, 数据类型:text tag 8-bit character set,地址设定为Function:Time,format:"HH:MM:SS" 根据上面的方法依次建立year,mon,day,week等TAG,请分别设置对应的format内容 TAG建立完成了,就可以读到系统时间了。新建一个图形文件,分别放置几个静态文本框[static text],把TEXT属性连接到上面新建的TAG,就可以显示系统日期时间,利用WEEK还能显示今天是星期几了. 方法二: 使用 C脚本获得系统时间 #include "apdefap.h" char* _main(char* lpszPictureName, char* lpszObjectName, char* lpszPropertyName) { time_t timer;

struct tm *ptm; char *p; time(&timer); ptm=localtime(&timer); p=SysMalloc(9); sprintf(p,"%04d:%02d:%02d",ptm->tm_year+1900,ptm->tm_mon+1,p tm->tm_mday); return(p); } 其中 //系统时间已经获得 //年: ptm -> tm_year+1900 //月: ptm-> tm_mon+1 //日: ptm -> tm_mday //星期: ptm -> tm_wday *注意这个,tm_wday返回的是整数,必须经过转换才能用来表示星期几

java获取当前时间

有两种方法: 方法一:用java.util.Date类来实现,并结合java.text.DateFormat类来实现时间的格式化,看下面代码: import java.util.*; import java.text.*; //以下默认时间日期显示方式都是汉语语言方式 //一般语言就默认汉语就可以了,时间日期的格式默认为MEDIUM风格,比如:2008-6-16 20:54:53 //以下显示的日期时间都是再Date类的基础上的来的,还可以利用Calendar类来实现见类TestDate2.java public class TestDate { public static void main(String[] args) { Date now = new Date(); Calendar cal = Calendar.getInstance(); DateFormat d1 = DateFormat.getDateInstance(); //默认语言(汉语)下的默认风格(MEDIUM风格,比如:2008-6-16 20:54:53) String str1 = d1.format(now); DateFormat d2 = DateFormat.getDateTimeInstance(); String str2 = d2.format(now); DateFormat d3 = DateFormat.getTimeInstance(); String str3 = d3.format(now); DateFormat d4 = DateFormat.getInstance(); //使用SHORT风格显示日期和时间 String str4 = d4.format(now); DateFormat d5 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); //显示日期,周,时间(精确到秒) String str5 = d5.format(now); DateFormat d6 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); //显示日期。时间(精确到秒) String str6 = d6.format(now); DateFormat d7 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); //显示日期,时间(精确到分) String str7 = d7.format(now);

labView中如何获取windows当前系统时间

labView中如何获取windows当前系统时间 《labview8.2入门到精通》的PPT,第4章里面有个练习是“写一个VI获取当前系统时间,并将其转换为字符串和浮点数”。完成效果如图。我要怎么来获取当前系统的时间呢? 程序如下,还是比较简单的,用到的函数都在编程---定时和编程---数值---转换里 问题: 我从一个仪器当中读取到了GPS时间,并想在LabVIEW中以较高的分辨率设定Windows系统时间。我注意到LabVIEW的时间函数选版中有获取时间/日期的VI,但没有设定时间的VI。如何才能在LabVIEW中实现时间设定?

解答: LabVIEW中没有现成的VI用于系统时间设定,但可以通过Windows SDK来实现。参考以下的步骤,通过调用库函数节点的方式,调用kernel32.dll中SetSystemTime函数,可以设定系统时间: 1. 在程序框图中放置一个调用库函数节点。双击此节点打开调用 库函数对话框。 2. 点击浏览按钮并选择kernel32.dll (一般放置于 "C:\WINDOWS\system32\kernel32.dll")。 3. 在函数名下拉列表中选择"SetSystemTime" 。 4. 在调用规范下拉列表中选择"stdcall (WINAPI)" 。 5. 在返回类型中选择“数值”以及“有符号8位整数”。 6. 点击添加参数并在类型中选择“匹配至类型”,接着在数据格式 中选择“句柄指针”。 7. 点击确定按钮,完成对调用库函数节点的配置。 8. 这个函数以一个包含八个双字节(16位整数)的结构体作为参 数。在LabVIEW中,需要创建一个包含八个双字节的簇来传 递这个参数。八个双字节分别为年、月、星期、日、小时、分 钟、秒以及毫秒。将此簇连接至调用库函数节点的参数1。 9. 输入相应的值,并运行程序以设定系统时间。

vc++获取系统时间和程序运行时间

内容: Q:如何获取时间?精度如何? A: 1 使用time_t time( time_t * timer ) 精确到秒 计算时间差使用double difftime( time_t timer1, time_t timer0 ) 2 使用clock_t clock() 得到的是CPU时间精确到1/CLOCKS_PER_SEC秒 3 使用DWORD GetTickCount() 得到的是系统运行的时间精确到毫秒 4 如果使用MFC的CTime类,可以用CTime::GetCurrentTime() 精确到秒 5 要获取高精度时间,可以使用 BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)获取系统的计数器的频率 BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)获取计数器的值 然后用两次计数器的差除以Frequency就得到时间。 6 还有David的文章中提到的方法: Multimedia Timer Functions The following functions are used with multimedia timers. timeBeginPeriod/timeEndPeriod/timeGetDevCaps/timeGetSystemTime timeGetTime/timeKillEvent/TimeProc/timeSetEvent 精度很高 Q:GetTickCount()函数,说是毫秒记数,是真的吗,还是精确到55毫秒? A: GetTickCount()和GetCurrentTime()都只精确到55ms(1个tick就是55ms)。如果要精确到毫秒,应该使用timeGetTime函数或QueryPerformanceCounter函数。具体例子可以参考QA001022 "VC++中使用高精度定时器"、QA001813 "如何在Windows实现准确的定时"和QA004842 "timeGetTime函数延时不准"。 Q:vc++怎样获取系统时间,返回值是什么类型的变量呢? GetSystemTime返回的是格林威志标准时间 GetLocalTime,和上面用法一样,返回的是你所在地区的时间,中国返回的是北京时间VOID GetSystemTime( LPSYSTEMTIME lpSystemTime // address of system time structure ); 函数就可以获得了,其中LPSYSTEMTIME 是个结构体 含:年,月,日,周几,小时,分,秒,毫秒。 以下是Time的MSDN文档: Compatibility in the Introduction. Libraries LIBC.LIBSingle thread static library, retail versionLIBCMT.LIBMultithread static library, retail versionMSVCRT.LIBImport library for MSVCRT.DLL, retail version Return Value time returns the time in elapsed seconds. There is no error return. Parameter timer Storage location for time Remarks

Js获取当前日期时间及其它操作

Js获取当前日期时间及其它操作 (2009-06-12 09:54:28) 转载▼ 标签: 分类:JavaScript js 时间 日期 it var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970-????) myDate.getMonth(); //获取当前月份(0-11,0代表1月) myDate.getDate(); //获取当前日(1-31) myDate.getDay(); //获取当前星期X(0-6,0代表星期天) myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数) myDate.getHours(); //获取当前小时数(0-23) myDate.getMinutes(); //获取当前分钟数(0-59) myDate.getSeconds(); //获取当前秒数(0-59) myDate.getMilliseconds(); //获取当前毫秒数(0-999) myDate.toLocaleDateString(); //获取当前日期 var mytime=myDate.toLocaleTimeString(); //获取当前时间myDate.toLocaleString( ); //获取日期与时间 日期时间脚本库方法列表 Date.prototype.isLeapYear 判断闰年 Date.prototype.Format 日期格式化

Date.prototype.DateAdd 日期计算 Date.prototype.DateDiff 比较日期差 Date.prototype.toString 日期转字符串 Date.prototype.toArray 日期分割为数组 Date.prototype.DatePart 取日期的部分信息 Date.prototype.MaxDayOfDate 取日期所在月的最大天数 Date.prototype.WeekNumOfYear 判断日期所在年的第几周 StringToDate 字符串转日期型 IsValidDate 验证日期有效性 CheckDateTime 完整日期时间检查 daysBetween 日期天数差 js代码: //--------------------------------------------------- // 判断闰年 //--------------------------------------------------- Date.prototype.isLeapYear = function() { return (0==this.getYear()%4&&((this.getYear()%100!=0)||(this.getYear()%400==0))); } //--------------------------------------------------- // 日期格式化 // 格式YYYY/yyyy/YY/yy 表示年份 // MM/M 月份 // W/w 星期 // dd/DD/d/D 日期 // hh/HH/h/H 时间 // mm/m 分钟

linux下获取系统时间的方法

linux下获取系统时间的方法 时间:2009-11-11 13:45:04 来源:Linux联盟作者:可以用localtime 函数分别获取年月日时分秒的数值。 Linux下获得系统时间的C语言的实现方法: 1. 可以用localtime 函数分别获取年月日时分秒的数值。 #include //C语言的头文件 #include //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 %sn",asctime(timenow)); //上句中asctime函数把时间转换成字符,通过printf()函数输出 } 注释:time_t是一个在time.h中定义好的结构体。而tm结构体的原形如下: struct tm { int tm_sec;//seconds 0-61 int tm_min;//minutes 1-59 int tm_hour;//hours 0-23 int tm_mday;//day of the month 1-31 int tm_mon;//months since jan 0-11 int tm_year;//years from 1900 int tm_wday;//days since Sunday, 0-6 int tm_yday;//days since Jan 1, 0-365 int tm_isdst;//Daylight Saving time indicator }; 2. 对某些需要较高精准度的需求,Linux提供了gettimeofday()。

Qt系统运行时间差计算

Qt系统运行时间差计算 在网上查了很多资料,发觉网上很多用Qt写的系统运行时间差的例子写的都不是很全,今天自己研究了一下,可以成功得显示日时分秒,觉得不错,就与大家分享了 #include #include #include int main(int argc,char*argv[]) { QCoreApplication a(argc,argv); QDateTime now=QDateTime::currentDateTime(); QDateTime Moon_Festival; QDateTime xmas(QDate(now.date().year(),7,26),QTime(0,0)); //QDate(now.date().year()取当前的年分,可以自己设置,比如2012 Moon_Festival=xmas; //这里最大计数为天 //这里涉及到两个函数daysTo()和SecsTo(),他们分别返回差值的天数和秒数,如果有朋友需要精确到毫秒的,QDateTime还提供了一个msecsTo() qDebug()<start(1000);//设置更新时间间隔 timeFlag=1; } }

Python获取当前时间

python获取当前时间 我有的时候写程序要用到当前时间,我就想用python去取当前的时间,虽然不是很难,但是老是忘记,用一次丢一次, 为了能够更好的记住,我今天特意写下python当前时间这篇文章,如果你觉的对你有用的话,可以收藏下。 取得时间相关的信息的话,要用到python time模块,python time模块里面有很多非常好用的功能,你可以去官方 文档了解下,要取的当前时间的话,要取得当前时间的时间戳,时间戳好像是1970年到现在时间相隔的时间。 你可以试下下面的方式来取得当前时间的时间戳: import time print time.time() 输出的结果是: 1279578704.672 该结果为时间戳(单位:秒) 如果希望得到毫秒将该值*1000,即:time.time()*1000 但是这样是一连串的数字不是我们想要的结果,我们可以利用time模块的格式化时间的方法来处理:time.localtime(time.time()) 用time.localtime()方法,作用是格式化时间戳为本地的时间。 输出的结果是: time.struct_time(tm_year=2010,tm_mon=7,tm_mday=19,tm_hour=22,tm_min=33,tm_sec=39, tm_wday=0,tm_yday=200,tm_isdst=0) 现在看起来更有希望格式成我们想要的时间了。 time.strftime('%Y-%m-%d',time.localtime(time.time())) 最后用time.strftime()方法,把刚才的一大串信息格式化成我们想要的东西,现在的结果是: 2010-07-19 time.strftime里面有很多参数,可以让你能够更随意的输出自己想要的东西: 下面是time.strftime的参数: strftime(format[,tuple])->string 将指定的struct_time(默认为当前时间),根据指定的格式化字符串输出 python中时间日期格式化符号: %y两位数的年份表示(00-99) %Y四位数的年份表示(000-9999) %m月份(01-12) %d月内中的一天(0-31) %H24小时制小时数(0-23)

C#获取当前系统时间

C#获取当前系统时间 2010-01-02 16:24 --DateTime 数字型 System.DateTimecurrentTime=new System.DateTime(); 取当前年月日时分秒 currentTime=System.DateTime.Now; 取当前年 int 年=currentTime.Year; 取当前月 int 月=currentTime.Month; 取当前日 int 日=currentTime.Day; 取当前时 int 时=currentTime.Hour; 取当前分 int 分=currentTime.Minute; 取当前秒 int 秒=currentTime.Second; 取当前毫秒 int毫秒=https://www.doczj.com/doc/6b8401209.html,lisecond; (变量可用中文) 取中文日期显示——年月日时分 string strY=currentTime.ToString("f"); //不显示秒 取中文日期显示_年月 string strYM=currentTime.ToString("y"); 取中文日期显示_月日 string strMD=currentTime.ToString("m"); 取当前年月日,格式为:2003-9-23 string strYMD=currentTime.ToString("d"); 取当前时分,格式为:14:24 string strT=currentTime.ToString("t"); DateTime.Now.ToString();//获取当前系统时间完整的日期和时间DateTime.Now.ToLongDateString();//只显示日期 xxxx年xx月xx日,一个是长日期 DateTime.Now.ToShortDateString();//只显示日期 xxxx-xx-xx 一个是短日期 //今天 DateTime.Now.Date.ToShortDateString(); //昨天的 DateTime.Now.AddDays(-1).ToShortDateString(); //明天的 DateTime.Now.AddDays(1).ToShortDateString(); //本周(注意这里的每一周是从周日始至周六止) DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString(); DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString(); //上周,上周就是本周再减去7天 DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();

VC 获取当前时间

VC++中其实还是通过调用它自带的CTime类来完成的获取当前系统时间的,我们做一个小程序来了解这个过程吧!对话框里只有两个显示框和两个按钮,点下按钮显示当前时间。就这么一个小程序。 (1)建立应用程序外壳 创建一个新的AppWizard项目,命名为shiyan,选择Dialog based;其他都选用默认属性,单击Finish完成生成应用程序的步骤。进入对话框界面以后,按下图所示布置显示框和功能按钮。 (2)设置参数 其中上面的显示文本框设为CString型,命名为m_show,ID号为IDC_show。下面的显示文本框设为CString型,命名为m_show1,ID号为IDC_show1。 (3)编译程序 start键程序: void CDate1Dlg::Onstart() { //count=0; SetTimer(1,1000,NULL); // TODO: Add your control notification handler code here

} stop键程序: void CDate1Dlg::Onstop() { KillTimer(1); // TODO: Add your control notification handler code here } (4)增加计时器控件 View -> ClassWizard -> MessageMaps -> CUse progressDlg,加入WM_TIMER函数,编辑程序: void CDate1Dlg::OnTimer(UINT nIDEvent) { if(nIDEvent==1) { // count++; UpdateData(1); CTime mtime=CTime::GetCurrentTime();//获取当前时间 char i; CString w; i=char (mtime.GetDayOfWeek()); //获取当前时间的天数是这个星期的第几天,这里要注意了,系统上默认的一个星期的第一天是星期日,最后一天是周六,大家千万不要搞错了。我也是试了才知道的。 switch(i)//将数字状换成字符就不会出现星期7这种情况了 { case 2:

c++ 简单获取系统时间

C++获取时间方法有多种,其中比较实用的是localtime函数 我们来看下下面这个范例 struct tm t; //tm结构指针 time_t nowT; //声明time_t类型变量 time(&nowT); //获取系统日期和时间 t = localtime( &nowT); //获取当地日期和时间 printf("%4d年%02d月%02d日%02d:%02d:%02d\n", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec); //格式化输出本地时间 该方法是调用了localtime函数进行时间获取,使用时得包含time.h头文件 #include 但是在vs上不少人会遇到编译器的报错提示,要求把localtime换成localtime_s 解释是为了安全性 如果出现这种情况我们只要改一行代码即可 t = localtime( &nowT); 改为localtime_s( &t,&nowT); 如果想要一次性输出日期与时间,那我们就要用到asctime函数 struct tm t; //tm结构指针 time_t nowT; //声明time_t类型变量 time(&nowT); //获取系统日期和时间 t = localtime( &nowT); //获取当地日期和时间 char TIMET[32]; TIMET = asctime(,&t); printf("now is: %s\n",TIMET ); 如诺出现vs的编译器报错,那只需要改成这样既可 struct tm t; //tm结构指针 time_t nowT; //声明time_t类型变量 time(&nowT); //获取系统日期和时间 localtime_s(&t, &nowT); //获取当地日期和时间 char TIMET[32]; asctime_s(TIMET, &t); printf("now is: %s\n",TIMET);

C语言获取当前系统时间的几种方式

C语言中如何获取时间?精度如何? 1 使用time_t time( time_t * timer ) 精确到秒 2 使用clock_t clock() 得到的是CPU时间精确到1/CLOCKS_PER_SEC秒 3 计算时间差使用double difftime( time_t timer1, time_t timer0 ) 4 使用DWORD GetTickCount() 精确到毫秒 5 如果使用MFC的CTime类,可以用CTime::GetCurrentTime() 精确到秒 6 要获取高精度时间,可以使用 BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency) 获取系统的计数器的频率 BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount) 获取计数器的值 然后用两次计数器的差除以Frequency就得到时间。 7 Multimedia Timer Functions The following functions are used with multimedia timers. timeBeginPeriod/timeEndPeriod/timeGetDevCaps/timeGetSystemTime //********************************************************************* //用标准C实现获取当前系统时间的函数 一.time()函数 time(&rawtime)函数获取当前时间距1970年1月1日的秒数,以秒计数单位,存于rawtime 中。#include "time.h" void main () { time_t rawtime; struct tm * timeinfo; time ( &rawtime );

相关主题
文本预览
相关文档 最新文档