package com.fsck.k9;


import java.util.Calendar;
import java.util.GregorianCalendar;


class QuietTimeChecker {
    private final Clock clock;
    private final String quietTimeStart;
    private final String quietTimeEnd;


    QuietTimeChecker(Clock clock, String quietTimeStart, String quietTimeEnd) {
        this.clock = clock;
        this.quietTimeStart = quietTimeStart;
        this.quietTimeEnd = quietTimeEnd;
    }

    public boolean isQuietTime() {
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        gregorianCalendar.setTimeInMillis(clock.getTime());

        Integer startHour = Integer.parseInt(quietTimeStart.split(":")[0]);
        Integer startMinute = Integer.parseInt(quietTimeStart.split(":")[1]);
        Integer endHour = Integer.parseInt(quietTimeEnd.split(":")[0]);
        Integer endMinute = Integer.parseInt(quietTimeEnd.split(":")[1]);

        Integer now = (gregorianCalendar.get(Calendar.HOUR_OF_DAY) * 60) + gregorianCalendar.get(Calendar.MINUTE);
        Integer quietStarts = startHour * 60 + startMinute;
        Integer quietEnds =  endHour * 60 + endMinute;

        // If start and end times are the same, we're never quiet
        if (quietStarts.equals(quietEnds)) {
            return false;
        }


        // 21:00 - 05:00 means we want to be quiet if it's after 9 or before 5
        if (quietStarts > quietEnds) {
            // if it's 22:00 or 03:00 but not 8:00
            if (now >= quietStarts || now <= quietEnds) {
                return true;
            }
        }

        // 01:00 - 05:00
        else {

            // if it' 2:00 or 4:00 but not 8:00 or 0:00
            if (now >= quietStarts && now <= quietEnds) {
                return true;
            }
        }

        return false;
    }
}

@@ -47,44 +47,8 @@
 ar;%0A
-import java.util.GregorianCalendar;%0A
 %0A%0Acl
@@ -111,38 +111,35 @@
   private final 
-Str
 in
-g
+t
  quietTimeStart;
@@ -157,22 +157,19 @@
 e final 
-Str
 in
-g
+t
  quietTi
@@ -315,16 +315,26 @@
 Start = 
+parseTime(
 quietTim
@@ -335,24 +335,25 @@
 ietTimeStart
+)
 ;%0A        th
@@ -370,16 +370,26 @@
 meEnd = 
+parseTime(
 quietTim
@@ -392,16 +392,17 @@
 tTimeEnd
+)
 ;%0A    %7D%0A
@@ -411,307 +411,80 @@
    p
-ublic boolean isQuietTime() %7B%0A        GregorianCalendar gregorianCalendar = new GregorianCalendar();%0A        gregorianCalend
+rivate static int p
 ar
-.
 se
-t
 Time
-InMillis(clock.getTime());%0A%0A        Integer startHour = Integer.parseInt(quietTimeStart.split(%22:%22)%5B0%5D);%0A        Integer startMinute = Integer.parseInt(quietTimeStart
+(String time) %7B%0A        String%5B%5D parts = time
 .spl
@@ -490,20 +490,16 @@
 lit(%22:%22)
-%5B1%5D)
 ;%0A      
@@ -504,20 +504,13 @@
     
-Integer endH
+int h
 our 
@@ -528,39 +528,21 @@
 arseInt(
-quietTimeEnd.split(%22:%22)
+parts
 %5B0%5D);%0A  
@@ -551,20 +551,13 @@
     
-Integer endM
+int m
 inut
@@ -581,189 +581,36 @@
 Int(
-quietTimeEnd.split(%22:%22)%5B1%5D);%0A%0A        Integer now = (gregorianCalendar.get(Calendar.HOUR_OF_DAY) * 60) + gregorianCalendar.get(Calendar.MINUTE);%0A        Integer quietStarts = startH
+parts%5B1%5D);%0A%0A        return h
 our 
@@ -620,14 +620,9 @@
 0 + 
-startM
+m
 inut
@@ -632,59 +632,45 @@
     
+%7D%0A%0A
     
-Integer quietEnds =  endHour * 60 + endMinute;%0A
+public boolean isQuietTime() %7B
 %0A   
@@ -753,32 +753,33 @@
 uiet
+Time
 Start
-s.equals(
+ == 
 quiet
+Time
 End
-s)
 ) %7B%0A
@@ -815,25 +815,24 @@
      %7D%0A%0A
-%0A
         
 // 21:00
@@ -827,78 +827,214 @@
     
-// 21:00 - 05:00 means we want to be quiet if it's after 9 or before 5
+Calendar calendar = Calendar.getInstance();%0A        calendar.setTimeInMillis(clock.getTime());%0A%0A        int minutesSinceMidnight = (calendar.get(Calendar.HOUR_OF_DAY) * 60) + calendar.get(Calendar.MINUTE);%0A
 %0A   
@@ -1051,22 +1051,25 @@
 uiet
+Time
 Start
-s
  %3E quiet
 Ends
@@ -1060,28 +1060,31 @@
 tart %3E quiet
+Time
 End
-s
 ) %7B%0A        
@@ -1090,67 +1090,33 @@
     
- //
  if 
-it's 22:00 or 03:00 but not 8:00%0A            if (now
+(minutesSinceMidnight
  %3E= 
@@ -1124,21 +1124,41 @@
 uiet
+Time
 Start
-s
  %7C%7C 
-now
+minutesSinceMidnight
  %3C= 
@@ -1154,36 +1154,39 @@
 idnight %3C= quiet
+Time
 End
-s
 ) %7B%0A            
@@ -1229,50 +1229,16 @@
    %7D
-%0A%0A        // 01:00 - 05:00%0A       
  else %7B%0A
 %0A   
@@ -1233,17 +1233,16 @@
  else %7B%0A
-%0A
         
@@ -1248,72 +1248,33 @@
     
- //
  if 
-it' 2:00 or 4:00 but not 8:00 or 0:00%0A            if (now
+(minutesSinceMidnight
  %3E= 
@@ -1282,21 +1282,41 @@
 uiet
+Time
 Start
-s
  && 
-now
+minutesSinceMidnight
  %3C= 
@@ -1324,12 +1324,15 @@
 uiet
+Time
 End
-s
 ) %7B%0A

Refactor QuietTimeChecker