timedelta の正規化
次のコードを実行したときの出力として、正しいものを1つ選べ。
from datetime import timedelta
t = timedelta(days=1, hours=25)
print(t.days, t.seconds, t.total_seconds())
- A
1 90000 176400.0 - B
2 3600 176400.0 - C
2 1 176400.0 - D
2 3600 3600.0
正解と解説を見る
正解:B
timedelta は、内部で日・秒・マイクロ秒に正規化される。seconds は日を除いた部分、total_seconds() は全体の秒数。
| 順 | 実行する行 | 実行後の変数 |
|---|---|---|
| 1 | `from datetime import timedelta` | — |
| 2 | `t = timedelta(days=1, hours=25)` | — |
| 3 | `print(t.days, t.seconds, t.total_seconds())` | — |
| 結果 | 実際の出力 | 2 3600 176400.0 |
- A
誤り。25 時間は日に繰り上がる。
- B
正しい。25 時間は 1 日と 1 時間に繰り上がる。
secondsは日を除いた秒の部分で、total_secondsは全体の秒数。 - C
誤り。
secondsは秒の単位。 - D
誤り。
total_secondsは全体の秒数。
このサイトの問題は、各試験の公式に示された出題範囲に沿って作ったオリジナルの模擬問題です。本試験の問題ではありません。一般社団法人Pythonエンジニア育成推進協会の公式サービスではありません。