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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# IVLE
# Copyright (C) 2008 The University of Melbourne
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

'''Utilities for making nice, human readable dates.'''

import time
import datetime

def get_datetime(datetime_or_seconds):
    '''Return the given datetime, or convert the given seconds since epoch.'''
    if type(datetime_or_seconds) is datetime.datetime:
        return datetime_or_seconds
    return datetime.datetime.fromtimestamp(datetime_or_seconds)

def make_date_nice(datetime_or_seconds):
    """Generate a full human-readable representation of a date and time.

    Given a datetime or number of seconds elapsed since the epoch,
    generates a string representing the date/time in human-readable form.
    "ddd mmm dd, yyyy h:m a"
    """
    dt = get_datetime(datetime_or_seconds)
    return dt.strftime("%a %b %d %Y, %I:%M %p")

def make_date_nice_short(datetime_or_seconds):
    """Generate a very compact human-readable representation of a date.

    Given a datetime or number of seconds elapsed since the epoch,
    generates a string representing the date in human-readable form.
    Does not include the time.
    """

    dt = get_datetime(datetime_or_seconds)
    now = datetime.datetime.now()

    # Use a "naturalisation" algorithm.
    delta = now - dt

    if delta.days <= 5:
        # Dates today or yesterday, return "today" or "yesterday".
        if delta.days == 0:
            return "Today"
        elif delta.days == 1:
            return "Yesterday"
        else:
            # Dates in the last 5 days, return "n days ago".
            return str(delta.days) + " days ago"
    # Other dates, return a short date format.
    # If within the same year, omit the year (mmm dd)
    if dt.year == now.year:
        return dt.strftime("%b %d")
    # Else, include the year (mmm dd, yyyy)
    else:
        return dt.strftime("%b %d, %Y")

def format_datetime_for_paragraph(datetime_or_seconds):
    """Generate a compact representation of a datetime for use in a paragraph.

    Given a datetime or number of seconds elapsed since the epoch, generates
    a compact string representing the date and time in human-readable form.

    Unlike make_date_nice_short, the time will always be included.

    Also unlike make_date_nice_short, it is suitable for use in the middle of
    a block of prose and properly handles timestamps in the future nicely.
    """

    dt = get_datetime(datetime_or_seconds)
    now = datetime.datetime.now()

    delta = dt - now

    # If the date is earlier than now, we want to either say something like
    # '5 days ago' or '25 seconds ago', 'yesterday at 08:54' or
    # 'on 2009-03-26 at 20:09'.

    # If the time is within one hour of now, we show it nicely in either
    # minutes or seconds.

    if abs(delta).days == 0 and abs(delta).seconds <= 1:
        return 'now'

    if abs(delta).days == 0 and abs(delta).seconds < 60*60:
        if abs(delta) == delta:
            # It's in the future.
            prefix = 'in '
            suffix = ''
        else:
            prefix = ''
            suffix = ' ago'

        # Show the number of minutes unless we are within two minutes.
        if abs(delta).seconds >= 120:
            return (prefix + '%d minutes' + suffix) % (abs(delta).seconds / 60)
        else:
            return (prefix + '%d seconds' + suffix) % (abs(delta).seconds)

    if dt < now:
        if dt.date() == now.date():
            # Today.
            return dt.strftime('today at %I:%M %p')
        elif dt.date() == now.date() - datetime.timedelta(days=1):
            # Yesterday.
            return dt.strftime('yesterday at %I:%M %p')
    elif dt > now:
        if dt.date() == now.date():
            # Today.
            return dt.strftime('today at %I:%M %p')
        elif dt.date() == now.date() + datetime.timedelta(days=1):
            # Tomorrow
            return dt.strftime('tomorrow at %I:%M %p')

    return dt.strftime('on %Y-%m-%d at %I:%M %p')