changed qse_ntime_t to a structure and made related changes

This commit is contained in:
2012-11-11 16:07:34 +00:00
parent d6a3bfea8d
commit b94dd042c5
29 changed files with 460 additions and 304 deletions

View File

@ -258,7 +258,7 @@ int qse_fio_init (
}
qse_gettime (&now);
temp_no += (now & 0xFFFFFFFFlu);
temp_no += (now.sec & 0xFFFFFFFFlu);
temp_tries = 0;
temp_ptr -= 4;

View File

@ -368,10 +368,11 @@ static int set_entry_name (qse_fs_t* fs, const qse_mchar_t* name)
}
#if defined(_WIN32)
static QSE_INLINE qse_ntime_t filetime_to_ntime (const FILETIME* ft)
static QSE_INLINE void filetime_to_ntime (const FILETIME* ft, qse_ntime_t* nt)
{
/* reverse of http://support.microsoft.com/kb/167296/en-us */
ULARGE_INTEGER li;
li.LowPart = ft->dwLowDateTime;
li.HighPart = ft->dwHighDateTime;
@ -383,9 +384,12 @@ static QSE_INLINE qse_ntime_t filetime_to_ntime (const FILETIME* ft)
# error Unsupported 64bit integer type
#endif
/*li.QuadPart /= 10000000;*/
li.QuadPart /= 10000;
/*li.QuadPart /= 10000;
return li.QuadPart;*/
return li.QuadPart;
/* li.QuadPart is in the 100-nanosecond intervals */
nt->sec = li.QuadPart / (QSE_NSECS_PER_SEC / 100);
nt->nsec = (li.QuadPart % (QSE_NSECS_PER_SEC / 100)) * 100;
}
#endif
@ -511,9 +515,9 @@ qse_fs_ent_t* qse_fs_read (qse_fs_t* fs, int flags)
if (flags & QSE_FS_ENT_TIME)
{
fs->ent.time.create = filetime_to_ntime (&info->wfd.ftCreationTime);
fs->ent.time.access = filetime_to_ntime (&info->wfd.ftLastAccessTime);
fs->ent.time.modify = filetime_to_ntime (&info->wfd.ftLastWriteTime);
filetime_to_ntime (&info->wfd.ftCreationTime, &fs->ent.time.create);
filetime_to_ntime (&info->wfd.ftLastAccessTime, &fs->ent.time.access);
filetime_to_ntime (&info->wfd.ftLastWriteTime, &fs->ent.time.modify);
fs->ent.type |= QSE_FS_ENT_TIME;
}
@ -642,34 +646,36 @@ qse_fs_ent_t* qse_fs_read (qse_fs_t* fs, int flags)
if (flags & QSE_FS_ENT_TIME)
{
#if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIME)
fs->ent.time.create =
QSE_SECNSEC_TO_MSEC(st.st_birthtim.tv_sec,st.st_birthtim.tv_nsec);
#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
fs->ent.time.create.secs = st.st_birthtim.tv_sec;
fs->ent.time.create.nsecs = st.st_birthtim.tv_nsec;
#endif
fs->ent.time.access =
QSE_SECNSEC_TO_MSEC(st.st_atim.tv_sec,st.st_atim.tv_nsec);
fs->ent.time.modify =
QSE_SECNSEC_TO_MSEC(st.st_mtim.tv_sec,st.st_mtim.tv_nsec);
fs->ent.time.change =
QSE_SECNSEC_TO_MSEC(st.st_ctim.tv_sec,st.st_ctim.tv_nsec);
fs->ent.time.access.sec = st.st_atim.tv_sec;
fs->ent.time.access.nsec = st.st_atim.tv_nsec;
fs->ent.time.modify.sec = st.st_mtim.tv_sec;
fs->ent.time.modify.nsec = st.st_mtim.tv_nsec;
fs->ent.time.change.sec = st.st_ctim.tv_sec;
fs->ent.time.change.nsec = st.st_ctim.tv_nsec;
#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIME)
fs->ent.time.create = st.st_birthtime;
QSE_SECNSEC_TO_MSEC(st.st_birthtimespec.tv_sec,st.st_birthtimespec.tv_nsec);
#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC)
fs->ent.time.create.sec = st.st_birthtimespec.tv_sec;
fs->ent.time.create.nsec = st.st_birthtimespec.tv_nsec;
#endif
fs->ent.time.access =
QSE_SECNSEC_TO_MSEC(st.st_atimespec.tv_sec,st.st_atimespec.tv_nsec);
fs->ent.time.modify =
QSE_SECNSEC_TO_MSEC(st.st_mtimespec.tv_sec,st.st_mtimespec.tv_nsec);
fs->ent.time.change =
QSE_SECNSEC_TO_MSEC(st.st_ctimespec.tv_sec,st.st_ctimespec.tv_nsec);
fs->ent.time.access.sec = st.st_atimspec.tv_sec;
fs->ent.time.access.nsec = st.st_atimspec.tv_nsec;
fs->ent.time.modify.sec = st.st_mtimspec.tv_sec;
fs->ent.time.modify.nsec = st.st_mtimspec.tv_nsec;
fs->ent.time.change.sec = st.st_ctimspec.tv_sec;
fs->ent.time.change.nsec = st.st_ctimspec.tv_nsec;
#else
#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIME)
fs->ent.time.create = st.st_birthtime * QSE_MSECS_PER_SEC;
fs->ent.time.create.sec = st.st_birthtime;
#endif
fs->ent.time.access = st.st_atime * QSE_MSECS_PER_SEC;
fs->ent.time.modify = st.st_mtime * QSE_MSECS_PER_SEC;
fs->ent.time.change = st.st_ctime * QSE_MSECS_PER_SEC;
fs->ent.time.access.sec = st.st_atime;
fs->ent.time.modify.sec = st.st_mtime;
fs->ent.time.change.sec = st.st_ctime;
#endif
fs->ent.flags |= QSE_FS_ENT_TIME;
}

View File

@ -533,14 +533,14 @@ done:
#endif
}
int qse_mux_poll (qse_mux_t* mux, qse_ntime_t timeout)
int qse_mux_poll (qse_mux_t* mux, const qse_ntime_t* tmout)
{
#if defined(USE_SELECT)
struct timeval tv;
int n;
tv.tv_sec = timeout / QSE_MSECS_PER_SEC;
tv.tv_usec = (timeout % QSE_MSECS_PER_SEC) * QSE_USECS_PER_MSEC;
tv.tv_sec = tmout->sec;
tv.tv_usec = QSE_NSEC_TO_USEC (tmout->nsec);
mux->tmprset = mux->rset;
mux->tmpwset = mux->wset;
@ -579,10 +579,10 @@ int qse_mux_poll (qse_mux_t* mux, qse_ntime_t timeout)
return n;
#elif defined(USE_EPOLL)
int nfds, i, mask;
int nfds, i;
qse_mux_evt_t* evt, xevt;
nfds = epoll_wait (mux->fd, mux->ee.ptr, mux->ee.len, timeout);
nfds = epoll_wait (mux->fd, mux->ee.ptr, mux->ee.len, QSE_SECNSEC_TO_MSEC(tmout->sec,tmout->nsec));
if (nfds <= -1)
{
mux->errnum = syserr_to_errnum(errno);
@ -605,10 +605,8 @@ int qse_mux_poll (qse_mux_t* mux, qse_ntime_t timeout)
if (mux->ee.ptr[i].events & EPOLLHUP)
{
if (evt->mask & QSE_MUX_IN)
xevt.mask |= QSE_MUX_IN;
if (evt->mask & QSE_MUX_OUT)
xevt.mask |= QSE_MUX_OUT;
if (evt->mask & QSE_MUX_IN) xevt.mask |= QSE_MUX_IN;
if (evt->mask & QSE_MUX_OUT) xevt.mask |= QSE_MUX_OUT;
}
mux->evtfun (mux, &xevt);

View File

@ -65,6 +65,8 @@ union sockaddr_t
};
#endif
#define TMOUT_ENABLED(tmout) (tmout.sec >= 0 && tmout.nsec >= 0)
static int nwad_to_sockaddr (const qse_nwad_t* nwad, int* family, void* addr)
{
int addrsize = -1;
@ -252,7 +254,7 @@ static qse_nwio_errnum_t tio_errnum_to_nwio_errnum (qse_tio_t* tio)
}
}
static int wait_for_data (qse_nwio_t* nwio, int tmout, int what)
static int wait_for_data (qse_nwio_t* nwio, const qse_ntime_t* tmout, int what)
{
int xret;
@ -265,8 +267,8 @@ static int wait_for_data (qse_nwio_t* nwio, int tmout, int what)
FD_SET (nwio->handle, &fds[what]);
tv.tv_sec = tmout / QSE_MSECS_PER_SEC;
tv.tv_usec = (tmout % QSE_MSECS_PER_SEC) * QSE_USECS_PER_MSEC;
tv.tv_sec = tmout->sec;
tv.tv_usec = QSE_NSEC_TO_USEC (tmout->nsec);
xret = select (nwio->handle + 1, &fds[0], &fds[1], QSE_NULL, &tv);
if (xret == SOCKET_ERROR)
@ -281,10 +283,12 @@ static int wait_for_data (qse_nwio_t* nwio, int tmout, int what)
}
#elif defined(__OS2__)
int count[2] = { 0, 0 };
long tmout_msecs;
count[what]++;
xret = os2_select (&nwio->handle, count[0], count[1], 0, tmout);
tmout_msecs = QSE_SECNSEC_TO_MSEC (tmout->sec, tmout->nsec);
xret = os2_select (&nwio->handle, count[0], count[1], 0, tmout_msecs);
if (xret <= -1)
{
nwio->errnum = syserr_to_errnum (sock_errno());
@ -309,8 +313,8 @@ static int wait_for_data (qse_nwio_t* nwio, int tmout, int what)
FD_SET (nwio->handle, &fds[what]);
tv.tv_sec = tmout / QSE_MSECS_PER_SEC;
tv.tv_usec = (tmout % QSE_MSECS_PER_SEC) * QSE_USECS_PER_MSEC;
tv.tv_sec = tmout->sec;
tv.tv_usec = QSE_NSEC_TO_USEC (tmout->nsec);
xret = select (nwio->handle + 1, &fds[0], &fds[1], QSE_NULL, &tv);
if (xret <= -1)
@ -375,10 +379,10 @@ int qse_nwio_init (
if (tmout) nwio->tmout = *tmout;
else
{
nwio->tmout.r = -1;
nwio->tmout.w = -1;
nwio->tmout.c = -1;
nwio->tmout.a = -1;
nwio->tmout.r.sec = -1;
nwio->tmout.w.sec = -1;
nwio->tmout.c.sec = -1;
nwio->tmout.a.sec = -1;
}
#if defined(AF_INET)
@ -437,8 +441,8 @@ int qse_nwio_init (
goto oops;
}
if (nwio->tmout.a >= 0 &&
wait_for_data (nwio, nwio->tmout.a, 0) <= -1) goto oops;
if (TMOUT_ENABLED(nwio->tmout.a) &&
wait_for_data (nwio, &nwio->tmout.a, 0) <= -1) goto oops;
handle = accept (nwio->handle, (struct sockaddr*)&addr, &addrlen);
if (handle == INVALID_SOCKET)
@ -459,7 +463,7 @@ int qse_nwio_init (
{
int xret;
if (nwio->tmout.c >= 0 && (flags & QSE_NWIO_TCP))
if (TMOUT_ENABLED(nwio->tmout.c) && (flags & QSE_NWIO_TCP))
{
unsigned long cmd = 1;
@ -472,7 +476,7 @@ int qse_nwio_init (
xret = connect (nwio->handle, (struct sockaddr*)&addr, addrlen);
if (nwio->tmout.c >= 0 && (flags & QSE_NWIO_TCP))
if (TMOUT_ENABLED(nwio->tmout.c) && (flags & QSE_NWIO_TCP))
{
unsigned long cmd = 0;
@ -483,7 +487,7 @@ int qse_nwio_init (
goto oops;
}
if (wait_for_data (nwio, nwio->tmout.c, 1) <= -1) goto oops;
if (wait_for_data (nwio, &nwio->tmout.c, 1) <= -1) goto oops;
else
{
int xlen;
@ -550,8 +554,8 @@ int qse_nwio_init (
goto oops;
}
if (nwio->tmout.a >= 0 &&
wait_for_data (nwio, nwio->tmout.a, 0) <= -1) goto oops;
if (TMOUT_ENABLED(nwio->tmout.a) &&
wait_for_data (nwio, &nwio->tmout.a, 0) <= -1) goto oops;
handle = accept (nwio->handle, (struct sockaddr*)&addr, &addrlen);
if (handle <= -1)
@ -572,7 +576,7 @@ int qse_nwio_init (
{
int xret;
if (nwio->tmout.c >= 0 && (flags & QSE_NWIO_TCP))
if (TMOUT_ENABLED(nwio->tmout.c) && (flags & QSE_NWIO_TCP))
{
int noblk = 1;
@ -585,7 +589,7 @@ int qse_nwio_init (
xret = connect (nwio->handle, (struct sockaddr*)&addr, addrlen);
if (nwio->tmout.c >= 0 && (flags & QSE_NWIO_TCP))
if (TMOUT_ENABLED(nwio->tmout.c) && (flags & QSE_NWIO_TCP))
{
int noblk = 0;
@ -596,7 +600,7 @@ int qse_nwio_init (
goto oops;
}
if (wait_for_data (nwio, nwio->tmout.c, 1) <= -1) goto oops;
if (wait_for_data (nwio, &nwio->tmout.c, 1) <= -1) goto oops;
else
{
int xlen, xerr;
@ -679,8 +683,8 @@ int qse_nwio_init (
goto oops;
}
if (nwio->tmout.a >= 0 &&
wait_for_data (nwio, nwio->tmout.a, 0) <= -1) goto oops;
if (TMOUT_ENABLED(nwio->tmout.a) &&
wait_for_data (nwio, &nwio->tmout.a, 0) <= -1) goto oops;
handle = accept (nwio->handle, (struct sockaddr*)&addr, &addrlen);
if (handle <= -1)
@ -701,7 +705,7 @@ int qse_nwio_init (
{
int orgfl, xret;
if (nwio->tmout.c >= 0 && (flags & QSE_NWIO_TCP))
if (TMOUT_ENABLED(nwio->tmout.c) && (flags & QSE_NWIO_TCP))
{
orgfl = fcntl (nwio->handle, F_GETFL, 0);
if (orgfl <= -1 ||
@ -714,7 +718,7 @@ int qse_nwio_init (
xret = connect (nwio->handle, (struct sockaddr*)&addr, addrlen);
if (nwio->tmout.c >= 0 && (flags & QSE_NWIO_TCP))
if (TMOUT_ENABLED(nwio->tmout.c) && (flags & QSE_NWIO_TCP))
{
if ((xret <= -1 && errno != EINPROGRESS) ||
fcntl (nwio->handle, F_SETFL, orgfl) <= -1)
@ -723,7 +727,7 @@ int qse_nwio_init (
goto oops;
}
if (wait_for_data (nwio, nwio->tmout.c, 1) <= -1) goto oops;
if (wait_for_data (nwio, &nwio->tmout.c, 1) <= -1) goto oops;
else
{
#if defined(HAVE_SOCKLEN_T)
@ -908,8 +912,8 @@ static qse_ssize_t nwio_read (qse_nwio_t* nwio, void* buf, qse_size_t size)
addrlen = QSE_SIZEOF(addr);
if (nwio->tmout.a >= 0 &&
wait_for_data (nwio, nwio->tmout.a, 0) <= -1) return -1;
if (TMOUT_ENABLED(nwio->tmout.a) &&
wait_for_data (nwio, &nwio->tmout.a, 0) <= -1) return -1;
count = recvfrom (
nwio->handle, buf, size, 0,
@ -932,8 +936,8 @@ static qse_ssize_t nwio_read (qse_nwio_t* nwio, void* buf, qse_size_t size)
}
else
{
if (nwio->tmout.r >= 0 &&
wait_for_data (nwio, nwio->tmout.r, 0) <= -1) return -1;
if (TMOUT_ENABLED(nwio->tmout.r) &&
wait_for_data (nwio, &nwio->tmout.r, 0) <= -1) return -1;
count = recv (nwio->handle, buf, size, 0);
if (count == SOCKET_ERROR) nwio->errnum = syserr_to_errnum (WSAGetLastError());
@ -952,8 +956,8 @@ static qse_ssize_t nwio_read (qse_nwio_t* nwio, void* buf, qse_size_t size)
addrlen = QSE_SIZEOF(addr);
if (nwio->tmout.a >= 0 &&
wait_for_data (nwio, nwio->tmout.a, 0) <= -1) return -1;
if (TMOUT_ENABLED(nwio->tmout.a) &&
wait_for_data (nwio, &nwio->tmout.a, 0) <= -1) return -1;
n = recvfrom (
nwio->handle, buf, size, 0,
@ -976,8 +980,8 @@ static qse_ssize_t nwio_read (qse_nwio_t* nwio, void* buf, qse_size_t size)
}
else
{
if (nwio->tmout.r >= 0 &&
wait_for_data (nwio, nwio->tmout.r, 0) <= -1) return -1;
if (TMOUT_ENABLED(nwio->tmout.r) &&
wait_for_data (nwio, &nwio->tmout.r, 0) <= -1) return -1;
n = recv (nwio->handle, buf, size, 0);
if (n <= -1) nwio->errnum = syserr_to_errnum (sock_errno());
@ -1012,8 +1016,8 @@ reread:
* like the 'nc' utility does.
* so i treat this recvfrom() as if it is accept().
*/
if (nwio->tmout.a >= 0 &&
wait_for_data (nwio, nwio->tmout.a, 0) <= -1) return -1;
if (TMOUT_ENABLED(nwio->tmout.a) &&
wait_for_data (nwio, &nwio->tmout.a, 0) <= -1) return -1;
n = recvfrom (
nwio->handle, buf, size, 0,
@ -1045,8 +1049,8 @@ reread:
}
else
{
if (nwio->tmout.r >= 0 &&
wait_for_data (nwio, nwio->tmout.r, 0) <= -1) return -1;
if (TMOUT_ENABLED(nwio->tmout.r) &&
wait_for_data (nwio, &nwio->tmout.r, 0) <= -1) return -1;
n = recv (nwio->handle, buf, size, 0);
if (n <= -1)
@ -1102,8 +1106,8 @@ static qse_ssize_t nwio_write (qse_nwio_t* nwio, const void* data, qse_size_t si
if (size > (QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(int)))
size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(int);
if (nwio->tmout.w >= 0 &&
wait_for_data (nwio, nwio->tmout.w, 1) <= -1) return -1;
if (TMOUT_ENABLED(nwio->tmout.w) &&
wait_for_data (nwio, &nwio->tmout.w, 1) <= -1) return -1;
count = send (nwio->handle, data, size, 0);
if (count == SOCKET_ERROR) nwio->errnum = syserr_to_errnum (WSAGetLastError());
@ -1114,8 +1118,8 @@ static qse_ssize_t nwio_write (qse_nwio_t* nwio, const void* data, qse_size_t si
if (size > (QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(int)))
size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(int);
if (nwio->tmout.w >= 0 &&
wait_for_data (nwio, nwio->tmout.w, 1) <= -1) return -1;
if (TMOUT_ENABLED(nwio->tmout.w) &&
wait_for_data (nwio, &nwio->tmout.w, 1) <= -1) return -1;
n = send (nwio->handle, data, size, 0);
if (n <= -1) nwio->errnum = syserr_to_errnum (sock_errno());
@ -1132,8 +1136,8 @@ static qse_ssize_t nwio_write (qse_nwio_t* nwio, const void* data, qse_size_t si
size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(size_t);
rewrite:
if (nwio->tmout.w >= 0 &&
wait_for_data (nwio, nwio->tmout.w, 1) <= -1) return -1;
if (TMOUT_ENABLED(nwio->tmout.w) &&
wait_for_data (nwio, &nwio->tmout.w, 1) <= -1) return -1;
n = send (nwio->handle, data, size, 0);
if (n <= -1)

View File

@ -48,9 +48,8 @@
#define WIN_EPOCH_DAY (1)
#define EPOCH_DIFF_YEARS (QSE_EPOCH_YEAR-WIN_EPOCH_YEAR)
#define EPOCH_DIFF_DAYS ((qse_ntime_t)EPOCH_DIFF_YEARS*365+EPOCH_DIFF_YEARS/4-3)
#define EPOCH_DIFF_SECS ((qse_ntime_t)EPOCH_DIFF_DAYS*24*60*60)
#define EPOCH_DIFF_MSECS ((qse_ntime_t)EPOCH_DIFF_SECS*QSE_MSECS_PER_SEC)
#define EPOCH_DIFF_DAYS ((qse_long_t)EPOCH_DIFF_YEARS*365+EPOCH_DIFF_YEARS/4-3)
#define EPOCH_DIFF_SECS ((qse_long_t)EPOCH_DIFF_DAYS*24*60*60)
#endif
static const int mdays[2][QSE_MONS_PER_YEAR] =
@ -89,6 +88,7 @@ int qse_gettime (qse_ntime_t* t)
#if defined(_WIN32)
SYSTEMTIME st;
FILETIME ft;
ULARGE_INTEGER li;
/*
* MSDN: The FILETIME structure is a 64-bit value representing the
@ -97,8 +97,14 @@ int qse_gettime (qse_ntime_t* t)
GetSystemTime (&st);
if (SystemTimeToFileTime (&st, &ft) == FALSE) return -1;
*t = ((qse_ntime_t)(*((qse_int64_t*)&ft)) / (10 * 1000));
*t -= EPOCH_DIFF_MSECS;
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
/* li.QuadPart is in the 100-nanosecond intervals */
t->sec = (li.QuadPart / (QSE_NSECS_PER_SEC / 100)) - EPOCH_DIFF_SECS;
t->nsec = (li.QuadPart % (QSE_NSECS_PER_SEC / 100)) * 100;
return 0;
#elif defined(__OS2__)
@ -121,10 +127,11 @@ int qse_gettime (qse_ntime_t* t)
bt.hour = dt.hours;
bt.min = dt.minutes;
bt.sec = dt.seconds;
bt.msec = dt.hundredths * 10;
/*bt.msec = dt.hundredths * 10;*/
bt.isdst = -1; /* determine dst for me */
if (qse_timelocal (&bt, t) <= -1) return -1;
t->nsec = QSE_MSEC_TO_NSEC(dt.hundredths * 10);
return 0;
#elif defined(__DOS__)
@ -142,10 +149,11 @@ int qse_gettime (qse_ntime_t* t)
bt.hour = dt.hour;
bt.min = dt.minute;
bt.sec = dt.second;
bt.msec = dt.hsecond * 10;
/*bt.msec = dt.hsecond * 10; */
bt.isdst = -1; /* determine dst for me */
if (qse_timelocal (&bt, t) <= -1) return -1;
t->nsec = QSE_MSEC_TO_NSEC(dt.hsecond * 10);
return 0;
#else
@ -155,19 +163,21 @@ int qse_gettime (qse_ntime_t* t)
n = QSE_GETTIMEOFDAY (&tv, QSE_NULL);
if (n == -1) return -1;
*t = (qse_ntime_t)tv.tv_sec * QSE_MSECS_PER_SEC +
(qse_ntime_t)tv.tv_usec / QSE_USECS_PER_MSEC;
t->sec = tv.tv_sec;
t->nsec = QSE_USEC_TO_NSEC(tv.tv_usec);
return 0;
#endif
}
int qse_settime (qse_ntime_t t)
int qse_settime (const qse_ntime_t* t)
{
#if defined(_WIN32)
FILETIME ft;
SYSTEMTIME st;
*((qse_int64_t*)&ft) = ((t + EPOCH_DIFF_MSECS) * (10 * 1000));
/**((qse_int64_t*)&ft) = ((t + EPOCH_DIFF_MSECS) * (10 * 1000));*/
*((qse_int64_t*)&ft) =
(QSE_SEC_TO_NSEC(t->sec + EPOCH_DIFF_SECS) / 100) + (t->nsec / 100);
if (FileTimeToSystemTime (&ft, &st) == FALSE) return -1;
if (SetSystemTime(&st) == FALSE) return -1;
return 0;
@ -187,7 +197,7 @@ int qse_settime (qse_ntime_t t)
dt.hours = bt.hour;
dt.minutes = bt.min;
dt.seconds = bt.sec;
dt.hundredths = bt.msec / 10;
dt.hundredths = QSE_NSEC_TO_MSEC(t->nsec) / 10;
rc = DosSetDateTime (&dt);
return (rc != NO_ERROR)? -1: 0;
@ -206,7 +216,7 @@ int qse_settime (qse_ntime_t t)
dt.hour = bt.hour;
dt.minute = bt.min;
dt.second = bt.sec;
dt.hsecond = bt.msec / 10;
dt.hsecond = QSE_NSEC_TO_MSEC(t->nsec) / 10;
if (_dos_settime (&dt) != 0) return -1;
if (_dos_setdate (&dd) != 0) return -1;
@ -217,8 +227,8 @@ int qse_settime (qse_ntime_t t)
struct timeval tv;
int n;
tv.tv_sec = t / QSE_MSECS_PER_SEC;
tv.tv_usec = (t % QSE_MSECS_PER_SEC) * QSE_USECS_PER_MSEC;
tv.tv_sec = t->sec;
tv.tv_usec = QSE_NSEC_TO_USEC(t->nsec);
/*
#if defined CLOCK_REALTIME && HAVE_CLOCK_SETTIME
@ -237,19 +247,14 @@ int qse_settime (qse_ntime_t t)
#endif
}
static void breakdown_time (qse_ntime_t nt, qse_btime_t* bt, qse_ntoff_t offset)
static void breakdown_time (const qse_ntime_t* nt, qse_btime_t* bt, qse_long_t offset)
{
int midx;
qse_ntime_t days; /* total days */
qse_ntime_t secs; /* the remaining seconds */
qse_ntime_t year = QSE_EPOCH_YEAR;
qse_long_t days; /* total days */
qse_long_t secs; /* the remaining seconds */
qse_long_t year = QSE_EPOCH_YEAR;
nt += offset;
bt->msec = nt % QSE_MSECS_PER_SEC;
if (bt->msec < 0) bt->msec = QSE_MSECS_PER_SEC + bt->msec;
secs = nt / QSE_MSECS_PER_SEC;
secs = nt->sec + offset; /* offset in seconds */
days = secs / QSE_SECS_PER_DAY;
secs %= QSE_SECS_PER_DAY;
@ -305,17 +310,16 @@ static void breakdown_time (qse_ntime_t nt, qse_btime_t* bt, qse_ntoff_t offset)
/*bt->offset = offset;*/
}
int qse_gmtime (qse_ntime_t nt, qse_btime_t* bt)
int qse_gmtime (const qse_ntime_t* nt, qse_btime_t* bt)
{
breakdown_time (nt, bt, 0);
return 0;
}
int qse_localtime (qse_ntime_t nt, qse_btime_t* bt)
int qse_localtime (const qse_ntime_t* nt, qse_btime_t* bt)
{
struct tm* tm;
time_t t = (time_t)(nt / QSE_MSECS_PER_SEC);
qse_ntime_t rem = nt % QSE_MSECS_PER_SEC;
time_t t = nt->sec;
/* TODO: remove dependency on localtime/localtime_r */
#if defined(_WIN32)
@ -342,7 +346,6 @@ int qse_localtime (qse_ntime_t nt, qse_btime_t* bt)
QSE_MEMSET (bt, 0, QSE_SIZEOF(*bt));
bt->msec = (rem >= 0)? rem: (QSE_MSECS_PER_SEC + rem);
bt->sec = tm->tm_sec;
bt->min = tm->tm_min;
bt->hour = tm->tm_hour;
@ -409,7 +412,7 @@ int qse_timegm (const qse_btime_t* bt, qse_ntime_t* nt)
#endif
#endif
qse_ntime_t n = 0;
qse_long_t n = 0;
int y = bt->year + QSE_BTIME_YEAR_BASE;
int midx = QSE_IS_LEAPYEAR(y)? 1: 0;
@ -434,10 +437,13 @@ int qse_timegm (const qse_btime_t* bt, qse_ntime_t* nt)
n = (n + QSE_HOURS_PER_DAY - bt->hour - 1) * QSE_MINS_PER_HOUR;
n = (n + QSE_MINS_PER_HOUR - bt->min - 1) * QSE_SECS_PER_MIN;
n = (n + QSE_SECS_PER_MIN - bt->sec) * QSE_MSECS_PER_SEC;
n = (n + QSE_SECS_PER_MIN - bt->sec); /* * QSE_MSECS_PER_SEC;
if (bt->msec > 0) n += QSE_MSECS_PER_SEC - bt->msec;
*nt = -n;
*nt = -n; */
nt->sec = -n;
nt->nsec = 0;
}
else
{
@ -454,9 +460,12 @@ int qse_timegm (const qse_btime_t* bt, qse_ntime_t* nt)
n = (n + bt->mday - 1) * QSE_HOURS_PER_DAY;
n = (n + bt->hour) * QSE_MINS_PER_HOUR;
n = (n + bt->min) * QSE_SECS_PER_MIN;
n = (n + bt->sec) * QSE_MSECS_PER_SEC;
n = (n + bt->sec); /* QSE_MSECS_PER_SEC;
*nt = n + bt->msec;
*nt = n + bt->msec;*/
nt->sec = n;
nt->nsec = 0;
}
return 0;
@ -467,6 +476,7 @@ int qse_timelocal (const qse_btime_t* bt, qse_ntime_t* nt)
/* TODO: qse_timelocal - remove dependency on timelocal */
struct tm tm;
QSE_MEMSET (&tm, 0, QSE_SIZEOF(tm));
tm.tm_sec = bt->sec;
tm.tm_min = bt->min;
tm.tm_hour = bt->hour;
@ -477,11 +487,12 @@ int qse_timelocal (const qse_btime_t* bt, qse_ntime_t* nt)
tm.tm_yday = bt->yday;
tm.tm_isdst = bt->isdst;
#ifdef HAVE_TIMELOCAL
*nt = ((qse_ntime_t)timelocal(&tm)*QSE_MSECS_PER_SEC) + bt->msec;
return 0;
#if defined(HAVE_TIMELOCAL)
nt->sec = timelocal (&tm);
#else
*nt = ((qse_ntime_t)mktime(&tm)*QSE_MSECS_PER_SEC) + bt->msec;
return 0;
nt->sec = mktime (&tm);
#endif
nt->nsec = 0;
return 0;
}