~azzar1/unity/add-show-desktop-key

1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
1
# IVLE
2
# Copyright (C) 2008 The University of Melbourne
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
1172 by William Grant
Let functions in ivle.date take datetimes, and deal in datetimes internally.
18
'''Utilities for making nice, human readable dates.'''
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
19
20
import time
1172 by William Grant
Let functions in ivle.date take datetimes, and deal in datetimes internally.
21
import datetime
22
23
def get_datetime(datetime_or_seconds):
24
    '''Return the given datetime, or convert the given seconds since epoch.'''
25
    if type(datetime_or_seconds) is datetime.datetime:
26
        return datetime_or_seconds
27
    return datetime.datetime.fromtimestamp(datetime_or_seconds)
28
29
def make_date_nice(datetime_or_seconds):
30
    """Generate a full human-readable representation of a date and time.
31
32
    Given a datetime or number of seconds elapsed since the epoch,
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
33
    generates a string representing the date/time in human-readable form.
34
    "ddd mmm dd, yyyy h:m a"
35
    """
1172 by William Grant
Let functions in ivle.date take datetimes, and deal in datetimes internally.
36
    dt = get_datetime(datetime_or_seconds)
37
    return dt.strftime("%a %b %d %Y, %I:%M %p")
38
39
def make_date_nice_short(datetime_or_seconds):
40
    """Generate a very compact human-readable representation of a date.
41
42
    Given a datetime or number of seconds elapsed since the epoch,
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
43
    generates a string representing the date in human-readable form.
44
    Does not include the time.
1172 by William Grant
Let functions in ivle.date take datetimes, and deal in datetimes internally.
45
    """
46
47
    dt = get_datetime(datetime_or_seconds)
48
    now = datetime.datetime.now()
49
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
50
    # Use a "naturalisation" algorithm.
1172 by William Grant
Let functions in ivle.date take datetimes, and deal in datetimes internally.
51
    delta = now - dt
52
53
    if delta.days <= 5:
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
54
        # Dates today or yesterday, return "today" or "yesterday".
1172 by William Grant
Let functions in ivle.date take datetimes, and deal in datetimes internally.
55
        if delta.days == 0:
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
56
            return "Today"
1172 by William Grant
Let functions in ivle.date take datetimes, and deal in datetimes internally.
57
        elif delta.days == 1:
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
58
            return "Yesterday"
59
        else:
1172 by William Grant
Let functions in ivle.date take datetimes, and deal in datetimes internally.
60
            # Dates in the last 5 days, return "n days ago".
61
            return str(delta.days) + " days ago"
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
62
    # Other dates, return a short date format.
63
    # If within the same year, omit the year (mmm dd)
1172 by William Grant
Let functions in ivle.date take datetimes, and deal in datetimes internally.
64
    if dt.year == now.year:
65
        return dt.strftime("%b %d")
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
66
    # Else, include the year (mmm dd, yyyy)
67
    else:
1172 by William Grant
Let functions in ivle.date take datetimes, and deal in datetimes internally.
68
        return dt.strftime("%b %d, %Y")