27025e
diff --git a/lib/isc/include/isc/result.h b/lib/isc/include/isc/result.h
27025e
index 0389efa..149cde5 100644
27025e
--- a/lib/isc/include/isc/result.h
27025e
+++ b/lib/isc/include/isc/result.h
27025e
@@ -89,7 +89,8 @@
27025e
 #define ISC_R_DISCFULL			67	/*%< disc full */
27025e
 #define ISC_R_DEFAULT			68	/*%< default */
27025e
 #define ISC_R_IPV4PREFIX		69	/*%< IPv4 prefix */
27025e
-#define ISC_R_NRESULTS 			70
27025e
+#define ISC_R_TIMESHIFTED               70      /*%< system time changed */
27025e
+#define ISC_R_NRESULTS 			71
27025e
 
27025e
 ISC_LANG_BEGINDECLS
27025e
 
27025e
diff --git a/lib/isc/include/isc/util.h b/lib/isc/include/isc/util.h
27025e
index 973c348..cceeb5e 100644
27025e
--- a/lib/isc/include/isc/util.h
27025e
+++ b/lib/isc/include/isc/util.h
27025e
@@ -289,6 +289,10 @@ extern void mock_assert(const int result, const char* const expression,
27025e
  * Time
27025e
  */
27025e
 #define TIME_NOW(tp) 	RUNTIME_CHECK(isc_time_now((tp)) == ISC_R_SUCCESS)
27025e
+#ifdef CLOCK_BOOTTIME
27025e
+#define TIME_MONOTONIC(tp) 	RUNTIME_CHECK(isc_time_boottime((tp)) == ISC_R_SUCCESS)
27025e
+#endif
27025e
+
27025e
 
27025e
 /*%
27025e
  * Alignment
27025e
diff --git a/lib/isc/result.c b/lib/isc/result.c
27025e
index a9db132..f33fc6b 100644
27025e
--- a/lib/isc/result.c
27025e
+++ b/lib/isc/result.c
27025e
@@ -105,6 +105,7 @@ static const char *description[ISC_R_NRESULTS] = {
27025e
 	"disc full",				/*%< 67 */
27025e
 	"default",				/*%< 68 */
27025e
 	"IPv4 prefix",				/*%< 69 */
27025e
+	"time changed",                         /*%< 70 */
27025e
 };
27025e
 
27025e
 static const char *identifier[ISC_R_NRESULTS] = {
27025e
@@ -178,6 +179,7 @@ static const char *identifier[ISC_R_NRESULTS] = {
27025e
 	"ISC_R_DISCFULL",
27025e
 	"ISC_R_DEFAULT",
27025e
 	"ISC_R_IPV4PREFIX",
27025e
+	"ISC_R_TIMESHIFTED",
27025e
 };
27025e
 
27025e
 #define ISC_RESULT_RESULTSET			2
27025e
diff --git a/lib/isc/unix/app.c b/lib/isc/unix/app.c
27025e
index a6e9882..286fe95 100644
27025e
--- a/lib/isc/unix/app.c
27025e
+++ b/lib/isc/unix/app.c
27025e
@@ -442,15 +442,47 @@ isc__app_ctxonrun(isc_appctx_t *ctx0, isc_mem_t *mctx, isc_task_t *task,
27025e
 static isc_result_t
27025e
 evloop(isc__appctx_t *ctx) {
27025e
 	isc_result_t result;
27025e
+        isc_time_t now;
27025e
+#ifdef CLOCK_BOOTTIME
27025e
+        isc_time_t monotonic;
27025e
+        isc_uint64_t diff  = 0;
27025e
+#else
27025e
+        isc_time_t prev;
27025e
+        TIME_NOW(&prev;;
27025e
+#endif
27025e
 
27025e
 	while (!ctx->want_shutdown) {
27025e
 		int n;
27025e
-		isc_time_t when, now;
27025e
+		isc_time_t when;
27025e
 		struct timeval tv, *tvp;
27025e
 		isc_socketwait_t *swait;
27025e
 		bool readytasks;
27025e
 		bool call_timer_dispatch = false;
27025e
 
27025e
+                uint64_t us; 
27025e
+
27025e
+#ifdef CLOCK_BOOTTIME
27025e
+                // TBD macros for following three lines
27025e
+                TIME_NOW(&now;;
27025e
+                TIME_MONOTONIC(&monotonic);
27025e
+                INSIST(now.seconds > monotonic.seconds)
27025e
+                us = isc_time_microdiff (&now, &monotonic);
27025e
+                if (us < diff){
27025e
+                  us = diff - us;
27025e
+                  if (us > 1000000){ // ignoring shifts less than one second
27025e
+                    return ISC_R_TIMESHIFTED;
27025e
+                  };
27025e
+                  diff = isc_time_microdiff (&now, &monotonic);
27025e
+                } else {
27025e
+                  diff = isc_time_microdiff (&now, &monotonic);
27025e
+                  // not implemented
27025e
+                }
27025e
+#else
27025e
+                TIME_NOW(&now;;
27025e
+                if (isc_time_compare (&now, &prev) < 0)
27025e
+                  return ISC_R_TIMESHIFTED;
27025e
+                TIME_NOW(&prev;;
27025e
+#endif
27025e
 		/*
27025e
 		 * Check the reload (or suspend) case first for exiting the
27025e
 		 * loop as fast as possible in case:
27025e
@@ -475,7 +507,6 @@ evloop(isc__appctx_t *ctx) {
27025e
 			if (result != ISC_R_SUCCESS)
27025e
 				tvp = NULL;
27025e
 			else {
27025e
-				uint64_t us;
27025e
 
27025e
 				TIME_NOW(&now;;
27025e
 				us = isc_time_microdiff(&when, &now;;
27025e
diff --git a/lib/isc/unix/include/isc/time.h b/lib/isc/unix/include/isc/time.h
27025e
index b864c29..5dd43c9 100644
27025e
--- a/lib/isc/unix/include/isc/time.h
27025e
+++ b/lib/isc/unix/include/isc/time.h
27025e
@@ -132,6 +132,26 @@ isc_time_isepoch(const isc_time_t *t);
27025e
  *\li	't' is a valid pointer.
27025e
  */
27025e
 
27025e
+#ifdef CLOCK_BOOTTIME
27025e
+isc_result_t
27025e
+isc_time_boottime(isc_time_t *t);
27025e
+/*%<
27025e
+ * Set 't' to monotonic time from previous boot
27025e
+ * it's not affected by system time change. It also
27025e
+ * includes the time system was suspended
27025e
+ *
27025e
+ * Requires:
27025e
+ *\li	't' is a valid pointer.
27025e
+ *
27025e
+ * Returns:
27025e
+ *
27025e
+ *\li	Success
27025e
+ *\li	Unexpected error
27025e
+ *		Getting the time from the system failed.
27025e
+ */
27025e
+#endif /* CLOCK_BOOTTIME */
27025e
+ 
27025e
+
27025e
 isc_result_t
27025e
 isc_time_now(isc_time_t *t);
27025e
 /*%<
27025e
diff --git a/lib/isc/unix/time.c b/lib/isc/unix/time.c
27025e
index 8edc9df..fe0bb91 100644
27025e
--- a/lib/isc/unix/time.c
27025e
+++ b/lib/isc/unix/time.c
27025e
@@ -498,3 +498,25 @@ isc_time_formatISO8601ms(const isc_time_t *t, char *buf, unsigned int len) {
27025e
 			 t->nanoseconds / NS_PER_MS);
27025e
 	}
27025e
 }
27025e
+
27025e
+
27025e
+#ifdef CLOCK_BOOTTIME
27025e
+isc_result_t
27025e
+isc_time_boottime(isc_time_t *t) {
27025e
+  struct timespec ts;
27025e
+  
27025e
+  char strbuf[ISC_STRERRORSIZE];
27025e
+
27025e
+  if (clock_gettime (CLOCK_BOOTTIME, &ts) != 0){
27025e
+    isc__strerror(errno, strbuf, sizeof(strbuf));
27025e
+    UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf);
27025e
+    return (ISC_R_UNEXPECTED);    
27025e
+  }
27025e
+
27025e
+  t->seconds = ts.tv_sec;
27025e
+  t->nanoseconds = ts.tv_nsec;
27025e
+
27025e
+  return (ISC_R_SUCCESS);
27025e
+  
27025e
+};
27025e
+#endif