系統提供的計算時間的精確度
(高顯忠, sjgau4311@gmail.com, 2010-11-15 09:07)
1樓
time()
只能夠傳回 秒的資料,
ftime()
號稱可以傳回 ms 的資料,
無論是哪一個,都需要寫程式來 測試一下,
no= 10;
while (no > 0) {
//
t1= time(NULL);
//
sum= 0;
for (i=1;i<=no;i++) {
sum+= i;
}
t2= time(NULL);
printf("no= %12d, sum= %12d, dt= %5d\n", no, sum, (t2 - t1),);
no*= 3;// 為什麼不是 *2 ???
}
// 以上的程式,沒有經過紹基的 實際測試
(高顯忠, sjgau4311@gmail.com, 2010-11-15 09:49)
2樓
// 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
OS time: 09:46:17
OS date: 11/15/10
Time in seconds since UTC 1/1/70: 1289785577
// 從 1970/01/01 AM 00:00:00 到現在,所經過的 秒數
UNIX time and date: Mon Nov 15 09:46:17 2010
Coordinated universal time: Mon Nov 15 01:46:17 2010
12-hour time: 09:46:17 AM
Plus milliseconds: 828
Zone difference in seconds from UTC: 4294966816
Time zone name: 台北標準時間
Daylight savings: NO
Christmas Sat Dec 25 12:00:00 1993
Today is Monday, day 15 of November in the year 2010.
Press any key to continue
*/
/* 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 <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 );
}
(高顯忠, sjgau4311@gmail.com, 2010-11-15 09:59)
3樓
以下這個程式,大致證明 time() 的正確性
#if 0
t1= 1289786215
y1= 40.870859, y2= 2010.870859
m1= 10.450306
d1= 13.509183
Press any key to continue
#endif
// ----------------------------------------------
#include <time.h>
#include <stdio.h>
void main()
{
int t1= time(NULL);
printf("t1= %d\n", t1);
double y1= ((double) t1)/(1.0*24*60*60)/365.25;
printf("y1= %.6lf, y2= %.6lf\n", y1, (y1 + 1970));
double m1= (y1 - ((int) y1))*12.0;
printf("m1= %.6lf\n", m1);
double d1= (m1 - ((int) m1))*30.0;
printf("d1= %.6lf\n", d1);
}// end of main()