def _duration_string_short(duration): """ Format a "short" duration string without seconds precision. This is intended to fit better in smaller spaces on a graph. :returns: a string of the form XhXm. """ h, m, s = duration_parts(duration) return '{}h{}m'.format(h, m)
def _duration_string_ms(duration): """ Format a "short" duration string with only minutes and seconds. This is intended to fit better in smaller spaces on a graph. :returns: a string of the form Xm. """ h, m, s = duration_parts(duration) return '{}m{}s'.format(m, s)
def seconds(duration): """ Return the "seconds" portion of a duration. :param duration: a timedetla instance. :returns: an integer representing the number of seconds in duration. """ if not duration: return 0 try: h, m, s = utils.duration_parts(duration) return s except (ValueError, TypeError): return 0
def minutes(duration): """ Return the "minutes" portion of a duration. :param duration: a timedetla instance. :returns: an integer representing the number of minutes in duration. """ if not duration: return 0 try: h, m, s = duration_parts(duration) return m except (ValueError, TypeError): return 0
def test_duration_parts(self): duration = timezone.timedelta(hours=1, minutes=30, seconds=45) self.assertEqual(duration_parts(duration), (1, 30, 45)) self.assertRaises(TypeError, lambda: duration_parts('1 hour'))