Convert a number of seconds into readable days, hours, and minutes
343474
def readable_time_delta(seconds):
    
    days = seconds // 86400
    seconds -= days * 86400
    hours = seconds // 3600
    seconds -= hours * 3600
    minutes = seconds // 60

    m_suffix = 's' if minutes != 1 else ''
    h_suffix = 's' if hours != 1 else ''
    d_suffix = 's' if days != 1 else ''

    retval = u'{0} minute{1}'.format(minutes, m_suffix)

    if hours != 0:
        retval = u'{0} hour{1} and {2}'.format(hours, h_suffix, retval)

    if days != 0:
        retval = u'{0} day{1}, {2}'.format(days, d_suffix, retval)

    return retval
1032727
def _pretty_time_delta(td):
    
    seconds = td.total_seconds()
    sign_string = '-' if seconds < 0 else ''
    seconds = abs(int(seconds))
    days, seconds = divmod(seconds, 86400)
    hours, seconds = divmod(seconds, 3600)
    minutes, seconds = divmod(seconds, 60)
    d = dict(sign=sign_string, days=days, hours=hours, minutes=minutes, seconds=seconds)
    if days > 0:
        return '{sign}{days}d{hours:02d}h{minutes:02d}m:{seconds:02d}s'.format(**d)
    elif hours > 0:
        return '{sign}{hours:02d}h{minutes:02d}m:{seconds:02d}s'.format(**d)
    elif minutes > 0:
        return '{sign}{minutes:02d}m:{seconds:02d}s'.format(**d)
    else:
        return '{sign}{seconds:02d}s'.format(**d)
1234423
def format_seconds(seconds, hide_seconds=False):
    
    if seconds <= 60:
        return str(seconds)
    output = ""
    for period, period_seconds in (
        ('y', 31557600),
        ('d', 86400),
        ('h', 3600),
        ('m', 60),
        ('s', 1),
    ):
        if seconds >= period_seconds and not (hide_seconds and period == 's'):
            output += str(int(seconds / period_seconds))
            output += period
            output += " "
            seconds = seconds % period_seconds
    return output.strip()
209756
def timedelta_to_str(aTimedelta):
    
    days = aTimedelta.days
    temp_seconds = aTimedelta.seconds
    hours = int(temp_seconds / 3600)
    minutes = int((temp_seconds - hours * 3600) / 60)
    seconds = temp_seconds - hours * 3600 - minutes * 60
    return '%d %02d:%02d:%02d' % (days, hours, minutes, seconds)
1179617
def stringToDurations(s, strict=False):
    
    def makeTimedelta(sign, week, day, hour, minute, sec):
        if sign == "-": sign = -1
        else: sign = 1
        week = int(week)
        day = int(day)
        hour = int(hour)
        minute = int(minute)
        sec = int(sec)
        return sign * datetime.timedelta(weeks=week, days=day, hours=hour,
                                         minutes=minute, seconds=sec)

    def error(msg):
        if strict:
            raise ParseError(msg)
        else:
            raise ParseError(msg)

    # vars which control state machine
    charIterator = enumerate(s)
    state  = "start"

    durations = []
    current = ""
    sign = None
    week = 0
    day = 0
    hour = 0
    minute = 0
    sec = 0

    while True:
        try:
            charIndex, char = next(charIterator)
        except:
            char = "eof"

        if state == "start":
            if char == '+':
                state = "start"
                sign = char
            elif char == '-':
                state = "start"
                sign = char
            elif char.upper() == 'P':
                state = "read field"
            elif char == "eof":
                state = "error"
                error("got end-of-line while reading in duration: " + s)
            elif char in string.digits:
                state = "read field"
                current = current + char  # update this part when updating "read field"
            else:
                state = "error"
                error("got unexpected character {0} reading in duration: {1}"
                      .format(char, s))

        elif state == "read field":
            if (char in string.digits):
                state = "read field"
                current = current + char  # update part above when updating "read field"
            elif char.upper() == 'T':
                state = "read field"
            elif char.upper() == 'W':
                state = "read field"
                week    = current
                current = ""
            elif char.upper() == 'D':
                state = "read field"
                day     = current
                current = ""
            elif char.upper() == 'H':
                state = "read field"
                hour    = current
                current = ""
            elif char.upper() == 'M':
                state = "read field"
                minute  = current
                current = ""
            elif char.upper() == 'S':
                state = "read field"
                sec     = current
                current = ""
            elif char == ",":
                state = "start"
                durations.append(makeTimedelta(sign, week, day, hour, minute,
                                               sec))
                current = ""
                sign = None
                week = None
                day = None
                hour = None
                minute = None
                sec = None
            elif char == "eof":
                state = "end"
            else:
                state = "error"
                error("got unexpected character reading in duration: " + s)

        elif state == "end":  # an end state
            if (sign or week or day or hour or minute or sec):
                durations.append(makeTimedelta(sign, week, day, hour, minute,
                                               sec))
            return durations

        elif state == "error":  # an end state
            error("in error state")
            return durations

        else:
            state = "error"
            error("unknown state: '{0!s}' reached in {1!s}".format(state, s))
295415
def _humanize_time(amount, units):
    """"""
    intervals = (1, 60, 60 * 60, 60 * 60 * 24, 604800, 2419200, 29030400)
    names = (
        ("second", "seconds"),
        ("minute", "minutes"),
        ("hour", "hours"),
        ("day", "days"),
        ("week", "weeks"),
        ("month", "months"),
        ("year", "years"),
    )

    result = []
    unit = [x[1] for x in names].index(units)
    # Convert to seconds
    amount = amount * intervals[unit]
    for i in range(len(names) - 1, -1, -1):
        a = int(amount) // intervals[i]
        if a > 0:
            result.append((a, names[i][1 % a]))
            amount -= a * intervals[i]
    return result
1342478
function toSeconds() {
      var seconds = this.seconds + 60 * this.minutes + 3600 * this.hours +
                    86400 * this.days + 7 * 86400 * this.weeks;
      return (this.isNegative ? -seconds : seconds);
    }
708994
def _format_time(seconds):
    

    # Always do minutes and seconds in mm:ss format
    minutes = seconds // 60
    hours = minutes // 60
    rtn = u'{0:02.0f}:{1:02.0f}'.format(minutes % 60, seconds % 60)

    #  Add hours if there are any
    if hours:

        rtn = u'{0:d}h {1}'.format(int(hours % 24), rtn)

        #  Add days if there are any
        days = int(hours // 24)
        if days:
            rtn = u'{0:d}d {1}'.format(days, rtn)

    return rtn
1316487
def duration(seconds):
    """"""
    if seconds < 1:
        return 'less than 1 sec'
    seconds = int(round(seconds))
    components = []
    for magnitude, label in ((3600, 'hr'), (60, 'min'), (1, 'sec')):
        if seconds >= magnitude:
            components.append('{} {}'.format(seconds // magnitude, label))
            seconds %= magnitude
    return ' '.join(components)
936040
private static String formatTime(int length, long time) {
        Calendar cal = Calendar.getInstance();
        if (time > -1) cal.setTimeInMillis(time);
        StringBuilder sb = new StringBuilder(length);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int seconds = cal.get(Calendar.SECOND);
        sb.append(cal.get(Calendar.YEAR)).append('-');
        if (month < 10) sb.append('0');
        sb.append(month).append('-');
        if (day < 10) sb.append('0');
        sb.append(day).append(' ');
        if (hour < 10) sb.append('0');
        sb.append(hour).append(':');
        if (minute < 10) sb.append('0');
        sb.append(minute).append(':');
        if (seconds < 10) sb.append('0');
        sb.append(seconds);
        if (length > 20) {
            int millis = cal.get(Calendar.MILLISECOND);
            sb.append('.');
            if (millis < 10) {
                sb.append("00");
            } else if (millis < 100) {
                sb.append('0');
            }
            sb.append(millis);
        }
        return sb.toString();
    }
368186
def to_minutes(days=0, hours=0, minutes=0, seconds=0, milliseconds=0,
               round_to_int=True):
    
    total = float(days)*24*60 + float(hours)*60 + float(minutes) + \
        float(seconds)/60 + float(milliseconds)/1000/60
    return int(round(total)) if round_to_int else total
226419
def format_time( sec ):
    ''' 
    '''
    import time
    if sec < 864000:
       # Idea from:   http://stackoverflow.com/a/1384565
       return time.strftime('%H:%M:%S', time.gmtime(sec))
    else:
       days = int(sec / 864000)
       secs = sec % 864000
       return str(days)+'d, '+time.strftime('%H:%M:%S', time.gmtime(secs))
1097444
def _days_to_seconds(cls, val, **kwargs):
        '''
        
        '''
        zero_value = kwargs.get('zero_value', 0)
        if val is not None:
            if val == 0:
                return zero_value
            return val * 86400
        else:
            return 'Not Defined'
758247
def parsetime(text):
    '''
    

    '''
    mins, maxs = text.split('-', 1)
    minv = s_time.parse(mins)
    maxv = s_time.parse(maxs, base=minv)
    return minv, maxv
266595
private String makeMillisReadable(double lMillis) {
        int hours = 0;
        int minutes = 0;
        int seconds = 0;
        int millis = 0;
        String sTime;
        seconds = (int) (lMillis / 1000) % 60;
        millis = (int) (lMillis % 1000);
        if (seconds > 0) {
            minutes = (int) (lMillis / 1000 / 60) % 60;
            if (minutes > 0) {
                hours = (int) (lMillis / 1000 / 60 / 60) % 24;
                if (hours > 0) {
                    sTime = hours + "h " + minutes + "m " + seconds + "s " + millis + "ms";
                } else {
                    sTime = minutes + "m " + seconds + "s " + millis + "ms";
                }
            } else {
                sTime = seconds + "s " + millis + "ms";
            }
        } else {
            sTime = millis + "ms";
        }
        return sTime;
    }
494770
def jid_to_time(jid):
    '''
    
    '''
    jid = six.text_type(jid)
    if len(jid) != 20 and (len(jid) <= 21 or jid[20] != '_'):
        return ''
    year = jid[:4]
    month = jid[4:6]
    day = jid[6:8]
    hour = jid[8:10]
    minute = jid[10:12]
    second = jid[12:14]
    micro = jid[14:20]

    ret = '{0}, {1} {2} {3}:{4}:{5}.{6}'.format(year,
                                                months[int(month)],
                                                day,
                                                hour,
                                                minute,
                                                second,
                                                micro)
    return ret
377001
def get_formatted_time(seconds: float) -> str:
    
    seconds = round(seconds)
    m, s = divmod(seconds, 60)
    h, m = divmod(m, 60)
    return '{:d}:{:02d}:{:02d}'.format(h, m, s)