         return r > 0 ? r : 0
     }
 
-    func isInPeriod(time: Int64) -> Bool {
+    func isFinished(_ time: Int64) -> Bool {
         var e: Int64!
-        if cycle == 0 && endDate == 0 {
-            e = startDate!.int64Value + duration!.int64Value
-        } else if endDate == 0 {
+        if cycle == 0 && end == 0 {
+            e = start + duration
+        } else if end == 0 {
             e = Int64.max
         } else {
-            e = endDate!.int64Value
+            e = end
         }
-        
-        return time >= startDate!.int64Value && time < e
+
+        return time >= e
+    }
+
+    func isInPeriod(_ time: Int64) -> Bool {
+        return time >= start!.int64Value && !isFinished(time)
     }
 }

            }

            if currentException != nil {
                var exRemaining = currentException!.remaining(time: time)
                for exception in exceptions! {
                    if isInSession(exception, time + exRemaining) {
                        exRemaining += exception.remaining(time: time + exRemaining)
                    }
                }
                if findCurrentSession(time: time + exRemaining) != nil {
                    return exRemaining
                }
            }

        return (session != nil)
    }

    func nextChange(_ time: Int64) -> Int64 {
        let currentSession = findCurrentSession(time: time)
        if currentSession != nil {
            let nearestException = findNearest(exceptions, nil, time)
            let remaining = currentSession?.remaining(time)

            return min(remaining!, nearestException)
        } else {
