ntp:fix parameter is negative numbers.
Signed-off-by: yangguangcai <yangguangcai@xiaomi.com>
diff --git a/netutils/ntpclient/ntpclient.c b/netutils/ntpclient/ntpclient.c
index 8ec85d2..ac48a3e 100644
--- a/netutils/ntpclient/ntpclient.c
+++ b/netutils/ntpclient/ntpclient.c
@@ -409,7 +409,7 @@
* Name: ntp_secpart
****************************************************************************/
-static uint32_t ntp_secpart(uint64_t time)
+static int32_t ntp_secpart(int64_t time)
{
/* NTP timestamps are represented as a 64-bit fixed-point number, in
* seconds relative to 0000 UT on 1 January 1900. The integer part is
@@ -434,11 +434,11 @@
* Name: ntp_nsecpart
****************************************************************************/
-static uint32_t ntp_nsecpart(uint64_t time)
+static int32_t ntp_nsecpart(int64_t time)
{
/* Get fraction part converted to nanoseconds. */
- return ((time & 0xffffffffu) * NSEC_PER_SEC) >> 32;
+ return (((int64_t)((uint64_t)time << 32) >> 32) * NSEC_PER_SEC) >> 32;
}
/****************************************************************************
@@ -528,6 +528,30 @@
}
/****************************************************************************
+ * Name: ntpc_apply_offset
+ ****************************************************************************/
+
+static void ntpc_apply_offset(FAR struct timespec *tp,
+ FAR struct timespec *src,
+ int64_t offset)
+{
+ tp->tv_sec = src->tv_sec + ntp_secpart(offset);
+ tp->tv_nsec = src->tv_nsec + ntp_nsecpart(offset);
+
+ while (tp->tv_nsec < 0)
+ {
+ tp->tv_nsec += NSEC_PER_SEC;
+ tp->tv_sec--;
+ }
+
+ while (tp->tv_nsec >= NSEC_PER_SEC)
+ {
+ tp->tv_nsec -= NSEC_PER_SEC;
+ tp->tv_sec++;
+ }
+}
+
+/****************************************************************************
* Name: ntpc_settime
*
* Description:
@@ -580,14 +604,7 @@
/* Apply offset */
- tp = curr_realtime;
- tp.tv_sec += ntp_secpart(offset);
- tp.tv_nsec += ntp_nsecpart(offset);
- while (tp.tv_nsec >= NSEC_PER_SEC)
- {
- tp.tv_nsec -= NSEC_PER_SEC;
- tp.tv_sec++;
- }
+ ntpc_apply_offset(&tp, &curr_realtime, offset);
/* Set the system time */