~ubuntu-branches/ubuntu/trusty/miro/trusty

« back to all changes in this revision

Viewing changes to portable/displaytext.py

  • Committer: Daniel Hahler
  • Date: 2010-04-13 18:51:35 UTC
  • mfrom: (1.2.10 upstream)
  • Revision ID: ubuntu-launchpad@thequod.de-20100413185135-xi24v1diqg8w406x
Merging shared upstream rev into target branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Miro - an RSS based video player application
2
 
# Copyright (C) 2005-2009 Participatory Culture Foundation
 
2
# Copyright (C) 2005-2010 Participatory Culture Foundation
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
5
5
# it under the terms of the GNU General Public License as published by
36
36
from miro.gtcache import gettext as _
37
37
from miro.gtcache import ngettext
38
38
 
39
 
def strftime_to_unicode(bytes):
 
39
def strftime_to_unicode(nbytes):
40
40
    """Convert the value return by strftime() to a unicode string.
41
41
 
42
42
    By default, it's in whatever the default codeset is.
43
43
    """
44
44
    if gtcache.codeset is None:
45
 
        return bytes
 
45
        return nbytes
46
46
    else:
47
 
        return bytes.decode(gtcache.codeset)
 
47
        return nbytes.decode(gtcache.codeset)
48
48
 
49
49
def download_rate(rate):
50
50
    if rate >= (1 << 30):
71
71
        return ngettext('%(num).0f hr', '%(num).0f hrs', t_hr, {"num": t_hr})
72
72
    if secs >= 60:
73
73
        t_min = secs * 1.0 / 60
74
 
        return ngettext('%(num).0f min', '%(num).0f mins', t_min, {"num": t_min})
 
74
        return ngettext('%(num).0f min', '%(num).0f mins', t_min,
 
75
                        {"num": t_min})
75
76
 
76
77
    return ngettext('%(num)d sec', '%(num)d secs', secs, {"num": secs})
77
78
 
78
 
def size_string(bytes):
79
 
    # when switching from the enclosure reported size to the downloader
80
 
    # reported size, it takes a while to get the new size and the downloader
81
 
    # returns -1.  the user sees the size go to -1B which is weird....
82
 
    # better to return an empty string.
83
 
    if bytes == -1 or bytes == 0:
 
79
def size_string(nbytes):
 
80
    # when switching from the enclosure reported size to the
 
81
    # downloader reported size, it takes a while to get the new size
 
82
    # and the downloader returns -1.  the user sees the size go to -1B
 
83
    # which is weird....  better to return an empty string.
 
84
    if nbytes == -1 or nbytes == 0:
84
85
        return ""
85
86
 
86
 
    # FIXME this is a repeat of util.formatSizeForUser ...  should
 
87
    # FIXME this is a repeat of util.format_size_for_user ...  should
87
88
    # probably ditch one of them.
88
 
    if bytes >= (1 << 30):
89
 
        value = "%.1f" % (bytes / float(1 << 30))
 
89
    if nbytes >= (1 << 30):
 
90
        value = "%.1f" % (nbytes / float(1 << 30))
90
91
        return _("%(size)s gb", {"size": value})
91
 
    elif bytes >= (1 << 20):
92
 
        value = "%.1f" % (bytes / float(1 << 20))
 
92
    elif nbytes >= (1 << 20):
 
93
        value = "%.1f" % (nbytes / float(1 << 20))
93
94
        return _("%(size)s mb", {"size": value})
94
 
    elif bytes >= (1 << 10):
95
 
        value = "%.1f" % (bytes / float(1 << 10))
 
95
    elif nbytes >= (1 << 10):
 
96
        value = "%.1f" % (nbytes / float(1 << 10))
96
97
        return _("%(size)s kb", {"size": value})
97
98
    else:
98
 
        return _("%(size)s b", {"size": bytes})
 
99
        return _("%(size)s b", {"size": nbytes})
99
100
 
100
101
def expiration_date(exp_date):
101
102
    offset = exp_date - datetime.datetime.now()
133
134
                        math.ceil(offset.seconds/60.0),
134
135
                        {"count": math.ceil(offset.seconds/60.0)})
135
136
 
136
 
def release_date(release_date):
137
 
    if release_date > datetime.datetime.min:
138
 
        # figure out the date pieces, convert to unicode, then split it on "::" so
139
 
        # we can run gettext on it allowing translators to reorder it.
140
 
        # see bug 11662.
141
 
        m, d, y = strftime_to_unicode(release_date.strftime("%B::%d::%Y")).split("::")
142
 
        return _("%(month)s %(dayofmonth)s, %(year)s", {"month": m, "dayofmonth": d, "year": y})
 
137
def release_date(rdate):
 
138
    """Takes a date object and returns the "month day, year"
 
139
    representation.
 
140
 
 
141
    If the rdate is below the minimum date, then this returns an
 
142
    empty string.
 
143
    """
 
144
    if rdate > datetime.datetime.min:
 
145
        # figure out the date pieces, convert to unicode, then split
 
146
        # it on "::" so we can run gettext on it allowing translators
 
147
        # to reorder it.  see bug 11662.
 
148
        m, d, y = strftime_to_unicode(rdate.strftime("%B::%d::%Y")).split("::")
 
149
        return _("%(month)s %(dayofmonth)s, %(year)s",
 
150
                 {"month": m, "dayofmonth": d, "year": y})
143
151
    else:
144
152
        return ''
145
153
 
146
 
def release_date_slashes(release_date):
147
 
    if release_date > datetime.datetime.min:
 
154
def release_date_slashes(rdate):
 
155
    """Takes a date object and returns the "MM/DD/YYYY"
 
156
    representation.
 
157
 
 
158
    If the rdate is below the minimum date, then this returns an
 
159
    empty string.
 
160
    """
 
161
    if rdate > datetime.datetime.min:
148
162
        # note: %x is locale-appropriate
149
 
        return strftime_to_unicode(release_date.strftime("%x"))
 
163
        return strftime_to_unicode(rdate.strftime("%x"))
150
164
    else:
151
165
        return ''
152
166