diff options
Diffstat (limited to 'tinySAK/src/tsk_time.c')
-rwxr-xr-x | tinySAK/src/tsk_time.c | 184 |
1 files changed, 91 insertions, 93 deletions
diff --git a/tinySAK/src/tsk_time.c b/tinySAK/src/tsk_time.c index 0f93df8..5202c29 100755 --- a/tinySAK/src/tsk_time.c +++ b/tinySAK/src/tsk_time.c @@ -2,19 +2,19 @@ * Copyright (C) 2010-2011 Mamadou Diop. * * Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org> -* +* * This file is part of Open Source Doubango Framework. * * DOUBANGO is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. -* +* * DOUBANGO is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. -* +* * You should have received a copy of the GNU General Public License * along with DOUBANGO. * @@ -38,7 +38,7 @@ # include <sys/time.h> # endif #elif defined(__SYMBIAN32__) -# include <_timeval.h> +# include <_timeval.h> #else # include <sys/time.h> #endif @@ -63,55 +63,53 @@ #endif #if TSK_UNDER_WINDOWS_CE -#endif +#endif -struct timezone -{ - int tz_minuteswest; // minutes W of Greenwich - int tz_dsttime; // type of dst correction +struct timezone { + int tz_minuteswest; // minutes W of Greenwich + int tz_dsttime; // type of dst correction }; -static int gettimeofday(struct timeval *tv, struct timezone *tz) -{ - FILETIME ft; - uint64_t tmpres = 0; - static int tzflag = 0; +static int gettimeofday(struct timeval *tv, struct timezone *tz) +{ + FILETIME ft; + uint64_t tmpres = 0; + static int tzflag = 0; - if(tv) - { + if(tv) { #if TSK_UNDER_WINDOWS_CE - SYSTEMTIME st; - GetSystemTime(&st); - SystemTimeToFileTime(&st, &ft); + SYSTEMTIME st; + GetSystemTime(&st); + SystemTimeToFileTime(&st, &ft); #else - GetSystemTimeAsFileTime(&ft); + GetSystemTimeAsFileTime(&ft); #endif /* TSK_UNDER_WINDOWS_CE */ - tmpres |= ft.dwHighDateTime; - tmpres <<= 32; - tmpres |= ft.dwLowDateTime; + tmpres |= ft.dwHighDateTime; + tmpres <<= 32; + tmpres |= ft.dwLowDateTime; - /*converting file time to unix epoch*/ - tmpres /= 10; /*convert into microseconds*/ - tmpres -= DELTA_EPOCH_IN_MICROSECS; - tv->tv_sec = (long)(tmpres / 1000000UL); - tv->tv_usec = (long)(tmpres % 1000000UL); - } + /*converting file time to unix epoch*/ + tmpres /= 10; /*convert into microseconds*/ + tmpres -= DELTA_EPOCH_IN_MICROSECS; + tv->tv_sec = (long)(tmpres / 1000000UL); + tv->tv_usec = (long)(tmpres % 1000000UL); + } #if !TSK_UNDER_WINDOWS_CE - if (tz){ - if (!tzflag){ + if (tz) { + if (!tzflag) { #if !TSK_UNDER_WINDOWS_RT - _tzset(); + _tzset(); #endif /* TSK_UNDER_WINDOWS_RT */ - tzflag++; - } - tz->tz_minuteswest = _timezone / 60; - tz->tz_dsttime = _daylight; - } + tzflag++; + } + tz->tz_minuteswest = _timezone / 60; + tz->tz_dsttime = _daylight; + } #endif /* TSK_UNDER_WINDOWS_CE */ - return 0; + return 0; } #else @@ -121,7 +119,7 @@ static int gettimeofday(struct timeval *tv, struct timezone *tz) #endif /* !HAVE_GETTIMEOFDAY */ /**@ingroup tsk_time_group -* The tsk_gettimeofday() function shall obtain the current time, expressed as seconds and microseconds since EPOCH (00:00:00 UTC on 1 January 1970). +* The tsk_gettimeofday() function shall obtain the current time, expressed as seconds and microseconds since EPOCH (00:00:00 UTC on 1 January 1970). * The resolution of the system clock is unspecified. * @param tv The current time, expressed as seconds and microseconds since EPOCH(00:00:00 UTC on 1 January 1970). * @param tz The timezone. @@ -129,16 +127,16 @@ static int gettimeofday(struct timeval *tv, struct timezone *tz) */ int tsk_gettimeofday(struct timeval *tv, struct timezone *tz) { - return gettimeofday(tv, tz); + return gettimeofday(tv, tz); } /**@ingroup tsk_time_group */ uint64_t tsk_gettimeofday_ms() { - struct timeval tv; - tsk_gettimeofday(&tv, tsk_null); - return (((uint64_t)tv.tv_sec)*(uint64_t)1000) + (((uint64_t)tv.tv_usec)/(uint64_t)1000); + struct timeval tv; + tsk_gettimeofday(&tv, tsk_null); + return (((uint64_t)tv.tv_sec)*(uint64_t)1000) + (((uint64_t)tv.tv_usec)/(uint64_t)1000); } /**@ingroup tsk_time_group @@ -147,11 +145,11 @@ uint64_t tsk_gettimeofday_ms() */ uint64_t tsk_time_get_ms(const struct timeval* tv) { - if(!tv){ - TSK_DEBUG_ERROR("Invalid parameter"); - return 0; - } - return (((uint64_t)tv->tv_sec)*(uint64_t)1000) + (((uint64_t)tv->tv_usec)/(uint64_t)1000); + if(!tv) { + TSK_DEBUG_ERROR("Invalid parameter"); + return 0; + } + return (((uint64_t)tv->tv_sec)*(uint64_t)1000) + (((uint64_t)tv->tv_usec)/(uint64_t)1000); } /**@ingroup tsk_time_group @@ -160,9 +158,9 @@ uint64_t tsk_time_get_ms(const struct timeval* tv) */ uint64_t tsk_time_epoch() { - struct timeval tv; - gettimeofday(&tv, (struct timezone *)tsk_null); - return (((uint64_t)tv.tv_sec)*(uint64_t)1000) + (((uint64_t)tv.tv_usec)/(uint64_t)1000); + struct timeval tv; + gettimeofday(&tv, (struct timezone *)tsk_null); + return (((uint64_t)tv.tv_sec)*(uint64_t)1000) + (((uint64_t)tv.tv_usec)/(uint64_t)1000); } // /!\ NOT CURRENT TIME @@ -170,36 +168,36 @@ uint64_t tsk_time_epoch() uint64_t tsk_time_now() { #if TSK_UNDER_WINDOWS - static int __cpu_count = 0; - if(__cpu_count == 0){ - SYSTEM_INFO SystemInfo; + static int __cpu_count = 0; + if(__cpu_count == 0) { + SYSTEM_INFO SystemInfo; # if TSK_UNDER_WINDOWS_RT - GetNativeSystemInfo(&SystemInfo); + GetNativeSystemInfo(&SystemInfo); # else - GetSystemInfo(&SystemInfo); + GetSystemInfo(&SystemInfo); # endif - __cpu_count = SystemInfo.dwNumberOfProcessors; - } - if(__cpu_count == 1){ - static LARGE_INTEGER __liFrequency = {0}; - LARGE_INTEGER liPerformanceCount; - if(!__liFrequency.QuadPart){ - QueryPerformanceFrequency(&__liFrequency); - } - QueryPerformanceCounter(&liPerformanceCount); - return (uint64_t)(((double)liPerformanceCount.QuadPart/(double)__liFrequency.QuadPart)*1000.0); - } - else{ + __cpu_count = SystemInfo.dwNumberOfProcessors; + } + if(__cpu_count == 1) { + static LARGE_INTEGER __liFrequency = {0}; + LARGE_INTEGER liPerformanceCount; + if(!__liFrequency.QuadPart) { + QueryPerformanceFrequency(&__liFrequency); + } + QueryPerformanceCounter(&liPerformanceCount); + return (uint64_t)(((double)liPerformanceCount.QuadPart/(double)__liFrequency.QuadPart)*1000.0); + } + else { # if TSK_UNDER_WINDOWS_RT - return tsk_time_epoch(); + return tsk_time_epoch(); # else - return timeGetTime(); + return timeGetTime(); # endif - } + } #elif HAVE_CLOCK_GETTIME || _POSIX_TIMERS > 0 - struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); - return (((uint64_t)ts.tv_sec)*(uint64_t)1000) + (((uint64_t)ts.tv_nsec)/(uint64_t)1000000); + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (((uint64_t)ts.tv_sec)*(uint64_t)1000) + (((uint64_t)ts.tv_nsec)/(uint64_t)1000000); #elif defined(__APPLE__) static mach_timebase_info_data_t __apple_timebase_info = {0, 0}; if (__apple_timebase_info.denom == 0) { @@ -207,39 +205,39 @@ uint64_t tsk_time_now() } return (uint64_t)((mach_absolute_time() * __apple_timebase_info.numer) / (1e+6 * __apple_timebase_info.denom)); #else - struct timeval tv; - gettimeofday(&tv, tsk_null); - return (((uint64_t)tv.tv_sec)*(uint64_t)1000) + (((uint64_t)tv.tv_usec)/(uint64_t)1000); + struct timeval tv; + gettimeofday(&tv, tsk_null); + return (((uint64_t)tv.tv_sec)*(uint64_t)1000) + (((uint64_t)tv.tv_usec)/(uint64_t)1000); #endif } // http://en.wikipedia.org/wiki/Network_Time_Protocol uint64_t tsk_time_ntp() { - struct timeval tv; - gettimeofday(&tv, (struct timezone *)tsk_null); - return tsk_time_get_ntp_ms(&tv); + struct timeval tv; + gettimeofday(&tv, (struct timezone *)tsk_null); + return tsk_time_get_ntp_ms(&tv); } uint64_t tsk_time_get_ntp_ms(const struct timeval *tv) { - static const unsigned long __epoch = 2208988800UL; - static const double __ntp_scale_frac = 4294967295.0; + static const unsigned long __epoch = 2208988800UL; + static const double __ntp_scale_frac = 4294967295.0; - uint64_t tv_ntp; - uint64_t tv_usecs; + uint64_t tv_ntp; + uint64_t tv_usecs; - if(!tv){ - TSK_DEBUG_ERROR("Invalid parameter"); - return 0; - } + if(!tv) { + TSK_DEBUG_ERROR("Invalid parameter"); + return 0; + } - tv_ntp = tv->tv_sec + __epoch; + tv_ntp = tv->tv_sec + __epoch; #if 0 // ARM floating point calc issue (__aeabi_d2uiz) - tv_usecs = (tv->tv_usec * 1e-6) * __ntp_scale_frac; - return ((tv_ntp << 32) | (uint32_t)tv_usecs); + tv_usecs = (tv->tv_usec * 1e-6) * __ntp_scale_frac; + return ((tv_ntp << 32) | (uint32_t)tv_usecs); #else - tv_usecs = ((uint64_t)tv->tv_usec * (uint64_t)__ntp_scale_frac) / (uint64_t)1000000; - return ((((uint64_t)tv_ntp) << 32) | (uint32_t)tv_usecs); + tv_usecs = ((uint64_t)tv->tv_usec * (uint64_t)__ntp_scale_frac) / (uint64_t)1000000; + return ((((uint64_t)tv_ntp) << 32) | (uint32_t)tv_usecs); #endif }
\ No newline at end of file |