#include "common/cs_time.h"
#ifndef _WIN32
#include <stddef.h>
#if !(defined(__ARMCC_VERSION) || defined(__ICCARM__)) && \
!defined(__TI_COMPILER_VERSION__) && \
(!defined(CS_PLATFORM) || CS_PLATFORM != CS_P_NXP_LPC)
#include <sys/time.h>
#endif
#else
#include <windows.h>
#endif
double cs_time(void) WEAK;
double cs_time(void) {
double now;
#ifndef _WIN32
struct timeval tv;
if (gettimeofday(&tv, NULL ) != 0) return 0;
now = (double) tv.tv_sec + (((double) tv.tv_usec) / 1000000.0);
#else
SYSTEMTIME sysnow;
FILETIME ftime;
GetLocalTime(&sysnow);
SystemTimeToFileTime(&sysnow, &ftime);
now = (double) (((int64_t) ftime.dwLowDateTime +
((int64_t) ftime.dwHighDateTime << 32)) /
10000000.0) -
11644473600;
#endif
return now;
}
double cs_timegm(const struct tm *tm) {
static const int month_day[12] = {0, 31, 59, 90, 120, 151,
181, 212, 243, 273, 304, 334};
int month = tm->tm_mon % 12;
int year = tm->tm_year + tm->tm_mon / 12;
int year_for_leap;
int64_t rt;
if (month < 0) {
month += 12;
--year;
}
year_for_leap = (month > 1) ? year + 1 : year;
rt =
tm->tm_sec
+
60 *
(tm->tm_min
+
60 * (tm->tm_hour
+
24 * (month_day[month] + tm->tm_mday - 1
+ 365 * (year - 70)
+ (year_for_leap - 69) / 4
- (year_for_leap - 1) / 100
+ (year_for_leap + 299) / 400)));
return rt < 0 ? -1 : (double) rt;
}