Last active
May 10, 2017 14:17
-
-
Save b4tman/52294326e263f100d2d2af00f0fa8869 to your computer and use it in GitHub Desktop.
для суммирования времени (например ".5:15 + 1:: + 30: = 01:30:45")
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
""" | |
# for python2 | |
from __future__ import print_function | |
input = vars(__builtins__).get('raw_input',input) | |
def ask_times(): | |
while (True): | |
try: | |
time = input(' + ') | |
except (EOFError, KeyboardInterrupt): | |
break | |
if 0<len(time): | |
yield time | |
else: | |
break | |
def sec_from_time(time): | |
parts = time.split(':') | |
parts.reverse() | |
level = 1 | |
ret = 0 | |
for part in parts: | |
if len(part): | |
ret += float(part) * level | |
level *= 60 | |
return ret | |
def time_from_sec(sec): | |
parts = [] | |
sec_remain = float(sec) | |
while(sec_remain): | |
if 60 > sec_remain: | |
part = sec_remain | |
sec_remain = 0 | |
else: | |
part = float(sec_remain % 60) | |
sec_remain = float(int(sec_remain/60)) | |
parts.append(part) | |
parts.reverse() | |
return ':'.join(map(lambda p: '{:02.4n}'.format(p), parts)) | |
sum_sec = 0 | |
for time in ask_times(): | |
sum_sec += sec_from_time(time) | |
print(' = ', time_from_sec(sum_sec)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment