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