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

« back to all changes in this revision

Viewing changes to ivle/date.py

  • Committer: William Grant
  • Date: 2009-02-26 02:08:28 UTC
  • Revision ID: grantw@unimelb.edu.au-20090226020828-0qrhe3llq9r5olmr
ivle-showenrolment: Swap year and semester, and show the role.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# along with this program; if not, write to the Free Software
16
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
17
 
18
 
'''Utilities for making nice, human readable dates.'''
 
18
# Module: Date utilities
 
19
# Author: William Grant
 
20
# Date: 16/07/2008
19
21
 
20
22
import time
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,
 
23
 
 
24
seconds_per_day = 86400 # 60 * 60 * 24
 
25
if time.daylight:
 
26
    timezone_offset = time.altzone
 
27
else:
 
28
    timezone_offset = time.timezone
 
29
 
 
30
def make_date_nice(seconds_since_epoch):
 
31
    """Given a number of seconds elapsed since the epoch,
33
32
    generates a string representing the date/time in human-readable form.
34
33
    "ddd mmm dd, yyyy h:m a"
35
34
    """
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,
 
35
    #return time.ctime(seconds_since_epoch)
 
36
    return time.strftime("%a %b %d %Y, %I:%M %p",
 
37
        time.localtime(seconds_since_epoch))
 
38
 
 
39
def make_date_nice_short(seconds_since_epoch):
 
40
    """Given a number of seconds elapsed since the epoch,
43
41
    generates a string representing the date in human-readable form.
44
42
    Does not include the time.
45
 
    """
46
 
 
47
 
    dt = get_datetime(datetime_or_seconds)
48
 
    now = datetime.datetime.now()
49
 
 
 
43
    This function generates a very compact representation."""
50
44
    # Use a "naturalisation" algorithm.
51
 
    delta = now - dt
52
 
 
53
 
    if delta.days <= 5:
 
45
    days_ago = (int(time.time() - timezone_offset) / seconds_per_day
 
46
        - int(seconds_since_epoch - timezone_offset) / seconds_per_day)
 
47
    if days_ago <= 5:
54
48
        # Dates today or yesterday, return "today" or "yesterday".
55
 
        if delta.days == 0:
 
49
        if days_ago == 0:
56
50
            return "Today"
57
 
        elif delta.days == 1:
 
51
        elif days_ago == 1:
58
52
            return "Yesterday"
59
53
        else:
60
 
            # Dates in the last 5 days, return "n days ago".
61
 
            return str(delta.days) + " days ago"
 
54
            return str(days_ago) + " days ago"
 
55
        # Dates in the last 5 days, return "n days ago".
62
56
    # Other dates, return a short date format.
63
57
    # If within the same year, omit the year (mmm dd)
64
 
    if dt.year == now.year:
65
 
        return dt.strftime("%b %d")
 
58
    if time.localtime(seconds_since_epoch).tm_year==time.localtime().tm_year:
 
59
        return time.strftime("%b %d", time.localtime(seconds_since_epoch))
66
60
    # Else, include the year (mmm dd, yyyy)
67
61
    else:
68
 
        return dt.strftime("%b %d, %Y")
69
 
 
70
 
def format_datetime_for_paragraph(datetime_or_seconds):
71
 
    """Generate a compact representation of a datetime for use in a paragraph.
72
 
 
73
 
    Given a datetime or number of seconds elapsed since the epoch, generates
74
 
    a compact string representing the date and time in human-readable form.
75
 
 
76
 
    Unlike make_date_nice_short, the time will always be included.
77
 
 
78
 
    Also unlike make_date_nice_short, it is suitable for use in the middle of
79
 
    a block of prose and properly handles timestamps in the future nicely.
80
 
    """
81
 
 
82
 
    dt = get_datetime(datetime_or_seconds)
83
 
    now = datetime.datetime.now()
84
 
 
85
 
    delta = dt - now
86
 
 
87
 
    # If the date is earlier than now, we want to either say something like
88
 
    # '5 days ago' or '25 seconds ago', 'yesterday at 08:54' or
89
 
    # 'on 2009-03-26 at 20:09'.
90
 
 
91
 
    # If the time is within one hour of now, we show it nicely in either
92
 
    # minutes or seconds.
93
 
 
94
 
    if abs(delta).days == 0 and abs(delta).seconds <= 1:
95
 
        return 'now'
96
 
 
97
 
    if abs(delta).days == 0 and abs(delta).seconds < 60*60:
98
 
        if abs(delta) == delta:
99
 
            # It's in the future.
100
 
            prefix = 'in '
101
 
            suffix = ''
102
 
        else:
103
 
            prefix = ''
104
 
            suffix = ' ago'
105
 
 
106
 
        # Show the number of minutes unless we are within two minutes.
107
 
        if abs(delta).seconds >= 120:
108
 
            return (prefix + '%d minutes' + suffix) % (abs(delta).seconds / 60)
109
 
        else:
110
 
            return (prefix + '%d seconds' + suffix) % (abs(delta).seconds)
111
 
 
112
 
    if dt < now:
113
 
        if dt.date() == now.date():
114
 
            # Today.
115
 
            return dt.strftime('today at %I:%M %p')
116
 
        elif dt.date() == now.date() - datetime.timedelta(days=1):
117
 
            # Yesterday.
118
 
            return dt.strftime('yesterday at %I:%M %p')
119
 
    elif dt > now:
120
 
        if dt.date() == now.date():
121
 
            # Today.
122
 
            return dt.strftime('today at %I:%M %p')
123
 
        elif dt.date() == now.date() + datetime.timedelta(days=1):
124
 
            # Tomorrow
125
 
            return dt.strftime('tomorrow at %I:%M %p')
126
 
 
127
 
    return dt.strftime('on %Y-%m-%d at %I:%M %p')
 
62
        return time.strftime("%b %d, %Y", time.localtime(seconds_since_epoch))