-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtelltime.py
executable file
·67 lines (53 loc) · 1.61 KB
/
telltime.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) Huoty, All rights reserved
# Author: Huoty <sudohuoty@163.com>
# CreateTime: 2020-04-28 09:39:25
import sys
import time
import logging
import datetime
if sys.version_info.major < 3:
import pyttsx
else:
import pyttsx3 as pyttsx
log = logging
get_now = datetime.datetime.now
def speak(text):
pyttsx.speak(text)
def start_timer():
now = get_now()
hour = now.hour
minute = now.minute
passed_minute = minute % 30
if (8 <= hour <= 12 or 14 <= hour <= 22) and passed_minute == 0:
current_time = now.strftime('%H:%M')
text = "现在时间 {}".format(current_time)
log.info("Telling time, current time: %s", current_time)
speak(text)
time.sleep(60)
else:
next_clock = now.replace(second=0, microsecond=0)
if 0 < minute < 30:
next_clock = next_clock.replace(minute=30)
elif 30 < minute:
next_clock = next_clock.replace(minute=0)
next_clock += datetime.timedelta(hours=1)
rest_of_time = (next_clock - now).total_seconds()
if rest_of_time < 0:
rest_of_time = 60
else:
log.info("Next clock: %s", next_clock)
log.info("Sleeping, the rest of time: %s",
datetime.timedelta(seconds=rest_of_time))
time.sleep(rest_of_time)
def main():
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
log.info("Starting timer, in loop")
while True:
start_timer()
if __name__ == "__main__":
main()