~ubuntu-branches/ubuntu/trusty/gnome-btdownload/trusty

« back to all changes in this revision

Viewing changes to src/formattime.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2008-07-18 15:55:43 UTC
  • mfrom: (0.1.17 hardy)
  • Revision ID: james.westby@ubuntu.com-20080718155543-69jiy8rhpbg8mf7h
Tags: 0.0.30-2.1
NMU. Rebuild to move files to /usr/share/pyshared. Closes: #490471.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Copyright (c) 2003-2007, Paul Varga and other contributors
 
3
All rights reserved.
 
4
 
 
5
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 
6
 
 
7
    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 
8
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 
9
    * Neither the name of the project nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
 
10
"""
 
11
 
 
12
# Gettext
 
13
import gettext
 
14
_ = gettext.gettext
 
15
 
 
16
# Converts a number of seconds into a short displayable string.
 
17
def succinct(seconds):
 
18
        seconds = int(seconds)
 
19
        
 
20
        hours    = seconds / (60 * 60)
 
21
        seconds -= hours * (60 * 60)
 
22
        
 
23
        minutes  = seconds / 60
 
24
        seconds -= minutes * 60
 
25
        
 
26
        return '%0.2d:%0.2d:%0.2d' % (hours, minutes, seconds)
 
27
 
 
28
# Converts a number of seconds into a precise, but verbose displayable string.
 
29
def lengthy_precise(seconds):
 
30
        seconds = int(seconds)
 
31
        
 
32
        days     = seconds / (60 * 60 * 24)
 
33
        seconds -= days * (60 * 60 * 24)
 
34
        
 
35
        hours    = seconds / (60 * 60)
 
36
        seconds -= hours * (60 * 60)
 
37
        
 
38
        minutes  = seconds / 60
 
39
        seconds -= minutes * 60
 
40
        
 
41
        def create_listing(items):
 
42
                def append_listing(listing, singular, plural, count, index, length):
 
43
                        initial = not listing
 
44
                        
 
45
                        if listing or count != 0 or index+1 == length:
 
46
                                listing += gettext.ngettext(singular, plural, count) % (count,)
 
47
                                
 
48
                                if index+1 < length:
 
49
                                        listing += ', '
 
50
                        
 
51
                        return listing
 
52
                
 
53
                listing = ''
 
54
                
 
55
                for index, item in zip(range(0,len(items)), items):
 
56
                        listing = append_listing(listing, item[0][0], item[0][1], item[1], index, len(items))
 
57
                
 
58
                return listing
 
59
        
 
60
        return create_listing((
 
61
                (('%i day', '%i days'), days),
 
62
                (('%i hour', '%i hours'), hours),
 
63
                (('%i minute', '%i minutes'), minutes),
 
64
                (('%i second', '%i seconds'), seconds)
 
65
        ))
 
66
 
 
67
# Converts a number of seconds into a rough estimate
 
68
def lengthy_estimate(seconds):
 
69
        seconds = int(seconds)
 
70
        
 
71
        days     = seconds / (60 * 60 * 24)
 
72
        seconds -= days * (60 * 60 * 24)
 
73
        
 
74
        hours    = seconds / (60 * 60)
 
75
        seconds -= hours * (60 * 60)
 
76
        
 
77
        minutes  = seconds / 60
 
78
        seconds -= minutes * 60
 
79
        
 
80
        if days >= 1:
 
81
                return gettext.ngettext('About %i day', 'About %i days', days) % days
 
82
        elif hours >= 1:
 
83
                return gettext.ngettext('About %i hour', 'About %i hours', hours) % hours
 
84
        elif minutes >= 1:
 
85
                return gettext.ngettext('About %i minute', 'About %i minutes', minutes) % minutes
 
86
        else:
 
87
                return gettext.ngettext('About %i second', 'About %i seconds', seconds) % seconds
 
88
 
 
89
lengthy = lengthy_estimate