ocarina/src/core/ct/times.py

79 lines
1.2 KiB
Python

#! /usr/bin/python
# To change this template, choose Tools | Templates
# and open the template in the editor.
__author__="bjschuma"
__date__ ="$Mar 16, 2010 7:36:48 PM$"
def ftime(time):
time = int(time)
#print time
# Find hour
length = ""
if time >= 3600:
hour = time/3600
time = time - (hour * 3600)
if hour > 0:
length=str(hour)+":"
# Find minute
if time >= 60:
min = time/60
time = time - (min * 60)
if min < 10:
length+="0"
length+=str(min)+":"
else:
length+="00:"
# Remainder is seconds
sec = time
if sec < 10:
length+="0"
length+=str(sec)
return length
def ms2str(ms):
# Convert ms to s
time = int(ms) / 1000000000
return ftime(time)
def hms2sec(hms):
if hms == None:
return 0
split = hms.split(":")[::-1]
sec = int( split[0] )
if len(split) >= 2:
sec += int( split[1] ) * 60
if len(split) == 3:
sec += int( split[2] ) * 60 * 60
return sec
def sec2text(s):
def fmt(time, field):
if time <= 0:
return ""
if time > 1:
field += "s"
return str(time) + " " + field + " "
sec = s
day = sec / 86500
sec -= day*86500
hour = sec / 3600
sec -= hour*3600
min = sec / 60
sec -= min*60
st = fmt(day,"day") + fmt(hour,"hour") + fmt(min,"minute") + fmt(sec,"second")
return st