이전에 post 중에
에 보면 서로 경과 시간을 출력하는 것이 있다.
( gettimeofday 사용)
이번에는 pda 의 응용프로그래밍 때문에 pda 에서도 유사한 코드를 이용, 출력하도록 해 보았다.
을 참조 하였다.
#include <time.h>static __inline int isLeapYear(int year){ return (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0); }static __inline int getCountOfLeapYearFor(int year){ return (year / 4) - (year / 100) + (year / 400); }static __inline int getCountOfDaysFor(int year){ return (365 * year) + getCountOfLeapYearFor(year); }INT64 MC_knlCurrentTime(){static const int daysInMonths[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };SYSTEMTIME st[1];INT64 totalDays;int month0Based, i;GetSystemTime(st);totalDays = st->wDay - 1; // start with 1month0Based = st->wMonth - 1; // start with 1for ( i=0; i<month0Based; ++i )totalDays += daysInMonths[i];if ( isLeapYear(st->wYear) && month0Based > 1 ) // passed leap month?++totalDays;totalDays += getCountOfDaysFor(st->wYear - 1);return ((totalDays * 24 + st->wHour) * 60 + st->wMinute) * 60 + st->wSecond - 62135596800i64/*=1970-01-01*/;}
참고로 코드 수정 부분은 INT64 형으로 바꾼것,
(마지막 함수의 return 형태와 totalDays 변수. )
그리고 ms 단위로 하지 않고 sec 단위로 하기 위하여 마지막 수식을 살짝 바꿨다.
그리고 실제 사용은 다음과 같이 하였다.
INT64 time = MC_knlCurrentTime();data[size++] = (unsigned char)(time >> 24);data[size++] = (unsigned char)(time >> 16);data[size++] = (unsigned char)(time >> 8);data[size++] = (unsigned char)time;
(작성환경 : VS2008, 실행환경 : PDA. AoM-1250, MIPS processor, WinCE 5.0)
참고로 원 글을 참조하자면, WinCE 5.0 에서는 위 식에서 ms 단위까지의 계산을 지원하지 않는다고 한다. 6.0 부터는 지원한다고 하지만...
(정작 내 폰은 이미 WinCE 6.5 이다. 내 폰(100만원) 보다 안 좋은 180만원짜리 PDA 같으니라고..;; )
그리고 계산상 4byte 이면 32bit 를 표현할 수 있고, 아직 한참 남아돌만큼 쓸 수 있으니 걱정은 하지 않도록 하자.
더불어 이 시간 계산이 맞는지 궁금하면 다음과 같이 linux 에서 컴파일 하여 작동해 보면 확인할 수 있다.
#include <sys/time.h>#include <iostream.h>int main(){struct timeval cur;gettimeofday(&cur, 0);cout << "now : " << cur.tv_sec<<endl;return 0;}
참~~ 쉽죠잉?
(환경 : 컴파일러는 다음 참조하자. 뭐 별꺼 있겠냐만. ;;;
yoons@yoons-desktop:~$ g++ -vUsing built-in specs.Target: i486-linux-gnuConfigured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-targets=all --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnuThread model: posixgcc version 4.2.4 (Ubuntu 4.2.4-1ubuntu4)yoons@yoons-desktop:~$
'Study.. > Programming' 카테고리의 다른 글
printf 자릿수 채워 출력하는 방법.. (0) | 2010.04.22 |
---|---|
이러니 될 일이 없었던거다..;;;; (0) | 2010.01.14 |
WinCE Emulator 설치시 오류... (0) | 2009.10.27 |
문제가 있을 때는... (0) | 2009.08.06 |
IAR 에서 printf 사용하기. (0) | 2009.06.30 |