We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Starting bot in sleep cycle
### Actual

When starting the bot while it should be in a sleep cycle, it waits for start of next sleep cycle.
### Expected

Bot should 'continue' sleeping and wake up on normal time.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `pokemongo_bot/cell_workers/sleep_schedule.py`
Content:
```
1 from datetime import datetime, timedelta
2 from time import sleep
3 from random import uniform
4 from pokemongo_bot.base_task import BaseTask
5 
6 
7 class SleepSchedule(BaseTask):
8     """Pauses the execution of the bot every day for some time
9 
10     Simulates the user going to sleep every day for some time, the sleep time
11     and the duration is changed every day by a random offset defined in the
12     config file
13     Example Config:
14     {
15         "type": "SleepSchedule",
16         "config": {
17           "time": "12:00",
18           "duration":"5:30",
19           "time_random_offset": "00:30",
20           "duration_random_offset": "00:30"
21         }
22     }
23     time: (HH:MM) local time that the bot should sleep
24     duration: (HH:MM) the duration of sleep
25     time_random_offset: (HH:MM) random offset of time that the sleep will start
26                         for this example the possible start time is 11:30-12:30
27     duration_random_offset: (HH:MM) random offset of duration of sleep
28                         for this example the possible duration is 5:00-6:00
29     """
30     SUPPORTED_TASK_API_VERSION = 1
31 
32     LOG_INTERVAL_SECONDS = 600
33     SCHEDULING_MARGIN = timedelta(minutes=10)    # Skip if next sleep is RESCHEDULING_MARGIN from now
34 
35     def initialize(self):
36         # self.bot.event_manager.register_event('sleeper_scheduled', parameters=('datetime',))
37         self._process_config()
38         self._schedule_next_sleep()
39 
40     def work(self):
41         if datetime.now() >= self._next_sleep:
42             self._sleep()
43             self._schedule_next_sleep()
44             self.bot.login()
45 
46     def _process_config(self):
47         self.time = datetime.strptime(self.config.get('time', '01:00'), '%H:%M')
48 
49         # Using datetime for easier stripping of timedeltas
50         duration = datetime.strptime(self.config.get('duration', '07:00'), '%H:%M')
51         self.duration = int(timedelta(hours=duration.hour, minutes=duration.minute).total_seconds())
52 
53         time_random_offset = datetime.strptime(self.config.get('time_random_offset', '01:00'), '%H:%M')
54         self.time_random_offset = int(
55             timedelta(
56                 hours=time_random_offset.hour, minutes=time_random_offset.minute).total_seconds())
57 
58         duration_random_offset = datetime.strptime(self.config.get('duration_random_offset', '00:30'), '%H:%M')
59         self.duration_random_offset = int(
60             timedelta(
61                 hours=duration_random_offset.hour, minutes=duration_random_offset.minute).total_seconds())
62 
63     def _schedule_next_sleep(self):
64         self._next_sleep = self._get_next_sleep_schedule()
65         self._next_duration = self._get_next_duration()
66         self.emit_event(
67             'next_sleep',
68             formatted="Next sleep at {time}",
69             data={
70                 'time': str(self._next_sleep)
71             }
72         )
73 
74     def _get_next_sleep_schedule(self):
75         now = datetime.now() + self.SCHEDULING_MARGIN
76         next_time = now.replace(hour=self.time.hour, minute=self.time.minute)
77 
78         next_time += timedelta(seconds=self._get_random_offset(self.time_random_offset))
79 
80         # If sleep time is passed add one day
81         if next_time <= now:
82             next_time += timedelta(days=1)
83 
84         return next_time
85 
86     def _get_next_duration(self):
87         duration = self.duration + self._get_random_offset(self.duration_random_offset)
88         return duration
89 
90     def _get_random_offset(self, max_offset):
91         offset = uniform(-max_offset, max_offset)
92         return int(offset)
93 
94     def _sleep(self):
95         sleep_to_go = self._next_duration
96         self.emit_event(
97             'bot_sleep',
98             formatted="Sleeping for {time_in_seconds}",
99             data={
100                 'time_in_seconds': sleep_to_go
101             }
102         )
103         while sleep_to_go > 0:
104             if sleep_to_go < self.LOG_INTERVAL_SECONDS:
105                 sleep(sleep_to_go)
106                 sleep_to_go = 0
107             else:
108                 sleep(self.LOG_INTERVAL_SECONDS)
109                 sleep_to_go -= self.LOG_INTERVAL_SECONDS
110
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/pokemongo_bot/cell_workers/sleep_schedule.py b/pokemongo_bot/cell_workers/sleep_schedule.py
--- a/pokemongo_bot/cell_workers/sleep_schedule.py
+++ b/pokemongo_bot/cell_workers/sleep_schedule.py
@@ -36,9 +36,10 @@
         # self.bot.event_manager.register_event('sleeper_scheduled', parameters=('datetime',))
         self._process_config()
         self._schedule_next_sleep()
+        self._calculate_current_sleep()
 
     def work(self):
-        if datetime.now() >= self._next_sleep:
+        if self._should_sleep_now():
             self._sleep()
             self._schedule_next_sleep()
             self.bot.login()
@@ -71,6 +72,20 @@
             }
         )
 
+    def _calculate_current_sleep(self):
+        self._current_sleep = self._next_sleep - timedelta(days=1)
+        current_duration = self._get_next_duration()
+        self._current_end = self._current_sleep + timedelta(seconds = current_duration)
+
+    def _should_sleep_now(self):
+        if datetime.now() >= self._next_sleep:
+            return True
+        if datetime.now() >= self._current_sleep and datetime.now() < self._current_end:
+            self._next_duration = (self._current_end - datetime.now()).total_seconds()
+            return True
+
+        return False
+
     def _get_next_sleep_schedule(self):
         now = datetime.now() + self.SCHEDULING_MARGIN
         next_time = now.replace(hour=self.time.hour, minute=self.time.minute)

{"golden_diff": "diff --git a/pokemongo_bot/cell_workers/sleep_schedule.py b/pokemongo_bot/cell_workers/sleep_schedule.py\n--- a/pokemongo_bot/cell_workers/sleep_schedule.py\n+++ b/pokemongo_bot/cell_workers/sleep_schedule.py\n@@ -36,9 +36,10 @@\n         # self.bot.event_manager.register_event('sleeper_scheduled', parameters=('datetime',))\n         self._process_config()\n         self._schedule_next_sleep()\n+        self._calculate_current_sleep()\n \n     def work(self):\n-        if datetime.now() >= self._next_sleep:\n+        if self._should_sleep_now():\n             self._sleep()\n             self._schedule_next_sleep()\n             self.bot.login()\n@@ -71,6 +72,20 @@\n             }\n         )\n \n+    def _calculate_current_sleep(self):\n+        self._current_sleep = self._next_sleep - timedelta(days=1)\n+        current_duration = self._get_next_duration()\n+        self._current_end = self._current_sleep + timedelta(seconds = current_duration)\n+\n+    def _should_sleep_now(self):\n+        if datetime.now() >= self._next_sleep:\n+            return True\n+        if datetime.now() >= self._current_sleep and datetime.now() < self._current_end:\n+            self._next_duration = (self._current_end - datetime.now()).total_seconds()\n+            return True\n+\n+        return False\n+\n     def _get_next_sleep_schedule(self):\n         now = datetime.now() + self.SCHEDULING_MARGIN\n         next_time = now.replace(hour=self.time.hour, minute=self.time.minute)\n", "issue": "Starting bot in sleep cycle\n### Actual\n\nWhen starting the bot while it should be in a sleep cycle, it waits for start of next sleep cycle.\n### Expected\n\nBot should 'continue' sleeping and wake up on normal time.\n\n", "before_files": [{"content": "from datetime import datetime, timedelta\nfrom time import sleep\nfrom random import uniform\nfrom pokemongo_bot.base_task import BaseTask\n\n\nclass SleepSchedule(BaseTask):\n    \"\"\"Pauses the execution of the bot every day for some time\n\n    Simulates the user going to sleep every day for some time, the sleep time\n    and the duration is changed every day by a random offset defined in the\n    config file\n    Example Config:\n    {\n        \"type\": \"SleepSchedule\",\n        \"config\": {\n          \"time\": \"12:00\",\n          \"duration\":\"5:30\",\n          \"time_random_offset\": \"00:30\",\n          \"duration_random_offset\": \"00:30\"\n        }\n    }\n    time: (HH:MM) local time that the bot should sleep\n    duration: (HH:MM) the duration of sleep\n    time_random_offset: (HH:MM) random offset of time that the sleep will start\n                        for this example the possible start time is 11:30-12:30\n    duration_random_offset: (HH:MM) random offset of duration of sleep\n                        for this example the possible duration is 5:00-6:00\n    \"\"\"\n    SUPPORTED_TASK_API_VERSION = 1\n\n    LOG_INTERVAL_SECONDS = 600\n    SCHEDULING_MARGIN = timedelta(minutes=10)    # Skip if next sleep is RESCHEDULING_MARGIN from now\n\n    def initialize(self):\n        # self.bot.event_manager.register_event('sleeper_scheduled', parameters=('datetime',))\n        self._process_config()\n        self._schedule_next_sleep()\n\n    def work(self):\n        if datetime.now() >= self._next_sleep:\n            self._sleep()\n            self._schedule_next_sleep()\n            self.bot.login()\n\n    def _process_config(self):\n        self.time = datetime.strptime(self.config.get('time', '01:00'), '%H:%M')\n\n        # Using datetime for easier stripping of timedeltas\n        duration = datetime.strptime(self.config.get('duration', '07:00'), '%H:%M')\n        self.duration = int(timedelta(hours=duration.hour, minutes=duration.minute).total_seconds())\n\n        time_random_offset = datetime.strptime(self.config.get('time_random_offset', '01:00'), '%H:%M')\n        self.time_random_offset = int(\n            timedelta(\n                hours=time_random_offset.hour, minutes=time_random_offset.minute).total_seconds())\n\n        duration_random_offset = datetime.strptime(self.config.get('duration_random_offset', '00:30'), '%H:%M')\n        self.duration_random_offset = int(\n            timedelta(\n                hours=duration_random_offset.hour, minutes=duration_random_offset.minute).total_seconds())\n\n    def _schedule_next_sleep(self):\n        self._next_sleep = self._get_next_sleep_schedule()\n        self._next_duration = self._get_next_duration()\n        self.emit_event(\n            'next_sleep',\n            formatted=\"Next sleep at {time}\",\n            data={\n                'time': str(self._next_sleep)\n            }\n        )\n\n    def _get_next_sleep_schedule(self):\n        now = datetime.now() + self.SCHEDULING_MARGIN\n        next_time = now.replace(hour=self.time.hour, minute=self.time.minute)\n\n        next_time += timedelta(seconds=self._get_random_offset(self.time_random_offset))\n\n        # If sleep time is passed add one day\n        if next_time <= now:\n            next_time += timedelta(days=1)\n\n        return next_time\n\n    def _get_next_duration(self):\n        duration = self.duration + self._get_random_offset(self.duration_random_offset)\n        return duration\n\n    def _get_random_offset(self, max_offset):\n        offset = uniform(-max_offset, max_offset)\n        return int(offset)\n\n    def _sleep(self):\n        sleep_to_go = self._next_duration\n        self.emit_event(\n            'bot_sleep',\n            formatted=\"Sleeping for {time_in_seconds}\",\n            data={\n                'time_in_seconds': sleep_to_go\n            }\n        )\n        while sleep_to_go > 0:\n            if sleep_to_go < self.LOG_INTERVAL_SECONDS:\n                sleep(sleep_to_go)\n                sleep_to_go = 0\n            else:\n                sleep(self.LOG_INTERVAL_SECONDS)\n                sleep_to_go -= self.LOG_INTERVAL_SECONDS\n", "path": "pokemongo_bot/cell_workers/sleep_schedule.py"}], "after_files": [{"content": "from datetime import datetime, timedelta\nfrom time import sleep\nfrom random import uniform\nfrom pokemongo_bot.base_task import BaseTask\n\n\nclass SleepSchedule(BaseTask):\n    \"\"\"Pauses the execution of the bot every day for some time\n\n    Simulates the user going to sleep every day for some time, the sleep time\n    and the duration is changed every day by a random offset defined in the\n    config file\n    Example Config:\n    {\n        \"type\": \"SleepSchedule\",\n        \"config\": {\n          \"time\": \"12:00\",\n          \"duration\":\"5:30\",\n          \"time_random_offset\": \"00:30\",\n          \"duration_random_offset\": \"00:30\"\n        }\n    }\n    time: (HH:MM) local time that the bot should sleep\n    duration: (HH:MM) the duration of sleep\n    time_random_offset: (HH:MM) random offset of time that the sleep will start\n                        for this example the possible start time is 11:30-12:30\n    duration_random_offset: (HH:MM) random offset of duration of sleep\n                        for this example the possible duration is 5:00-6:00\n    \"\"\"\n    SUPPORTED_TASK_API_VERSION = 1\n\n    LOG_INTERVAL_SECONDS = 600\n    SCHEDULING_MARGIN = timedelta(minutes=10)    # Skip if next sleep is RESCHEDULING_MARGIN from now\n\n    def initialize(self):\n        # self.bot.event_manager.register_event('sleeper_scheduled', parameters=('datetime',))\n        self._process_config()\n        self._schedule_next_sleep()\n        self._calculate_current_sleep()\n\n    def work(self):\n        if self._should_sleep_now():\n            self._sleep()\n            self._schedule_next_sleep()\n            self.bot.login()\n\n    def _process_config(self):\n        self.time = datetime.strptime(self.config.get('time', '01:00'), '%H:%M')\n\n        # Using datetime for easier stripping of timedeltas\n        duration = datetime.strptime(self.config.get('duration', '07:00'), '%H:%M')\n        self.duration = int(timedelta(hours=duration.hour, minutes=duration.minute).total_seconds())\n\n        time_random_offset = datetime.strptime(self.config.get('time_random_offset', '01:00'), '%H:%M')\n        self.time_random_offset = int(\n            timedelta(\n                hours=time_random_offset.hour, minutes=time_random_offset.minute).total_seconds())\n\n        duration_random_offset = datetime.strptime(self.config.get('duration_random_offset', '00:30'), '%H:%M')\n        self.duration_random_offset = int(\n            timedelta(\n                hours=duration_random_offset.hour, minutes=duration_random_offset.minute).total_seconds())\n\n    def _schedule_next_sleep(self):\n        self._next_sleep = self._get_next_sleep_schedule()\n        self._next_duration = self._get_next_duration()\n        self.emit_event(\n            'next_sleep',\n            formatted=\"Next sleep at {time}\",\n            data={\n                'time': str(self._next_sleep)\n            }\n        )\n\n    def _calculate_current_sleep(self):\n        self._current_sleep = self._next_sleep - timedelta(days=1)\n        current_duration = self._get_next_duration()\n        self._current_end = self._current_sleep + timedelta(seconds = current_duration)\n\n    def _should_sleep_now(self):\n        if datetime.now() >= self._next_sleep:\n            return True\n        if datetime.now() >= self._current_sleep and datetime.now() < self._current_end:\n            self._next_duration = (self._current_end - datetime.now()).total_seconds()\n            return True\n\n        return False\n\n    def _get_next_sleep_schedule(self):\n        now = datetime.now() + self.SCHEDULING_MARGIN\n        next_time = now.replace(hour=self.time.hour, minute=self.time.minute)\n\n        next_time += timedelta(seconds=self._get_random_offset(self.time_random_offset))\n\n        # If sleep time is passed add one day\n        if next_time <= now:\n            next_time += timedelta(days=1)\n\n        return next_time\n\n    def _get_next_duration(self):\n        duration = self.duration + self._get_random_offset(self.duration_random_offset)\n        return duration\n\n    def _get_random_offset(self, max_offset):\n        offset = uniform(-max_offset, max_offset)\n        return int(offset)\n\n    def _sleep(self):\n        sleep_to_go = self._next_duration\n        self.emit_event(\n            'bot_sleep',\n            formatted=\"Sleeping for {time_in_seconds}\",\n            data={\n                'time_in_seconds': sleep_to_go\n            }\n        )\n        while sleep_to_go > 0:\n            if sleep_to_go < self.LOG_INTERVAL_SECONDS:\n                sleep(sleep_to_go)\n                sleep_to_go = 0\n            else:\n                sleep(self.LOG_INTERVAL_SECONDS)\n                sleep_to_go -= self.LOG_INTERVAL_SECONDS\n", "path": "pokemongo_bot/cell_workers/sleep_schedule.py"}]}