[Tutor] time(duration) formats & basic time math query

Dustin J. Mitchell python-tutor at v.igoro.us
Thu Jul 6 08:00:57 CEST 2006


kevin parks wrote:
> I have been handed a huge number of documents which have hundreds of  
> pages of times and durations, all calculated and notated by several  
> different people over the course of many years. Sadly, no made any  
> guidelines at all about how this work would proceed and all the  
> documenters had their own ideas about how times/durations would be  
> specified so the doc are a mess. Furthermore the person i work for  
> changes her mind every 15 minutes so i have no idea what she will  
> want at any given moment. This sounds to me like a job for Python  
> <hee hee>
> 
> Essentially, I am trying to do 2 things:
> 
> move fluidly between different duration formats (take any, specify  
> and display any). Such as:
> 
> pure miliseconds
> seconds. msec
> mm:ss.msec
> hh:mm:ss.msec
> 
> So the input doc would be grepped for times and i could just  
> uncomment the line i need and get he format my boss wants at this  
> particular moment.
> So a recording that is 71 minutes and 33 seconds could be printed as:
> 4293 seconds
> 71:33.0000
> 01:11.33.0000      or whatever....
> 
> also i need to be able to adjust start times, durations, and end  
> times which means doing a little time math.
> Like if a recording is 71 minute and 33 seconds.. but there are  
> several sonic events in that recording and i
> need to specify the duration, or start time etc of those individual  
> events... then i need to subtract... Additionally i
> know that i will need to subtract pure minutes from an hh:mm format....
> 
> I having a real hard time getting my head round the labyrinthian  
> datetime module in the docs (i am guessing
> that this is what i should use). I wonder if anyone could give me a  
> hand and point me in the right direction.

datetime may do the trick for you, but I would use it only for its
conversional abilities -- it sounds like this is a good time for you to
implement your own "Duration" class, with some kind of flexible system
for parsing (possibly even recognizing) different time formats, and
producing different time formats, e.g.,

class Duration:
  def __init__(self, duration_string, format='guess'):
    if format == 'guess': format = self.guess_format(duration_string)
    self.format = format

    parse, output = self.format_fns[self.format]
    self.seconds = parse(self, duration_string)

  def output(self, format='original'):
    if format == 'original': format = self.format
    parse, output = self.format_fns[format]
    return output(self)

  def parse_milliseconds(self, duration_string): ...
  def output_milliseconds(self): ...
  def parse_fracseconds(self, duration_string): ...
  def output_fracseconds(self): ...

  format_fns = {
    'milliseconds' : (parse_milliseconds, output_milliseconds),
    'fracseconds' : (parse_fracseconds, ouput_fracseconds),
  }

Then you can just parse the thing up like this:

  durations = [ Duration(line.strip()) for line in f.xreadlines() ]

and print it out like this:

  for dur in durations:
    print dur.output('milliseconds')

Dustin


More information about the Tutor mailing list