2017-12-24 37 views
0

現在、私はPythonで目覚まし時計のsciptを書いています。私は、現在時刻とアラーム時刻の両方から時間と分を解析し、アラーム時刻から現在時刻を差し引くように秒数に変換するのに役立つ必要があります。ここでPythonの目覚まし時計で時間を差し引く

enter image description here

+0

あなたは/あなたの現在のコードの試みとあなたの質問を提供することができます問題?そうでなければ、これは設計上の自由な質問であり、SOには本当に適切ではありません。 – Miket25

+0

私はちょうどあなたの編集を見て、私は私の答えが問題に合っていると信じています。あなたはもう助けが必要ですか? – oBit91

+0

ありがとう!私はそれが私の問題を解決する必要があると思う、私は今試してみる:) – Maximus

答えて

1

はPythonで日時を使用する方法の例です。

from datetime import datetime as dt 
import time 
format = '%Y-%m-%d %H:%M:%S' 
currTime = dt.now() 
alarmTime = dt.strptime('2017-12-25 08:00:00', format); 
print('current time:\t', currTime) 
print('alarm time:\t', alarmTime) 
currTimeUnix = time.mktime(currTime.timetuple()) 
alarmTimeUnix = time.mktime(alarmTime.timetuple()) 
diff = alarmTimeUnix - currTimeUnix 
print('seconds diff: \t', diff) 

出力は次のようになります。

current time: 2017-12-24 22:40:30.842519 
alarm time:  2017-12-25 08:00:00 
seconds diff: 33570.0 

スリープ例:

s = 3; 
print(dt.now()) 
time.sleep(s) 
print('I\'ve slept for', s, 'seconds') 
print(dt.now()) 

はになります:

2017-12-24 22:44:24.275121 
I've slept for 3 seconds 
2017-12-24 22:44:27.276324 
関連する問題